Tiny Basic Programming Lesson: talking to the real world

12419231_974683622601393_320731550894605287_oUsing HalfByte Tiny Basic to interface with the external world is pretty easy to do. There are already built in interfaces for temperature and humidity, but, using AREAD, AWRITE, DREAD and DWRITE, we can access a variety of sensors and modules that do not need specialized libraries in order to talk to them.  Sensors such as the touch sensor, IR reader and the light sensor I talked about in previous posts can easily be used by HB Tiny Basic.  The key to using them is to know the range of values they return.  Since documentation for them can be difficult to obtain, the best way to figure it out is to connect them and experiment.  Once you get a feel for the range of values and what they mean, you can easily incorporate them into a project.

Lets consider the following code:

100 CLS
110 A=AREAD(0)
120 CURSOR 0,2
130 PRINT “Light level: “;
140 IF A=0 PRINT”Pitch Dark”:GOTO 500
150 IF A>0 IF A<100 PRINT “Dark    “:GOTO 500
160 IF A>100 IF A<300 PRINT “Low     “:GOTO 500
170 IF A>300 IF A<600 PRINT “Moderate”:GOTO 500
180 IF A>500 PRINT “Bright”:GOTO 500
500 GOTO 110

All this code does is read the sensor, determine a range for light level and output the results.  300 and above is about what you have in a living room in the even with one or two lamps turned on.  Over 500 and you are talking some bright light.  The higher the value, the brighter the light.  Zero, of course, is a total lack of light.

Looking at the code, you will notice that we are reading the A0 analog pin.  That’s the pin the light sensor is connected to. In HB Tiny Basic, A0 to A7 is represented by the integer portion-0,1,2 and so on.  To designate that you want to use the Analog pin, use AREAD.  The same rules apply for the Digital pins…use DREAD and 0 to 13 for the pins.  Line 110 above, A=AREAD( 0 ) to read A0 into the variable ‘A’.  That value, in variable A, becomes static until the pin is read again. This allows us to use the value, as captured at that moment, to do what ever we want.  In this case, we use it to determine if the light level is pitch dark to bright. 

Because HB Tiny Basic lacks such operators as ‘and’ and ‘or’, we have to use multiple IF statements. So, for line 150, instead of something like:

150 IF A>0 AND A<100 PRINT “Dark    “:GOTO 500

we have to use:

150 IF A>0 IF A<100 PRINT “Dark    “:GOTO 500

All it is saying is ‘if the value in A is greater than zero AND less than 100, then print the Dark to the output device and skip to line 500’.

Once we are done  evaluating the light level, we just go back and do it again.  A real world use of this could be like ‘if the light level goes below 300, set pin 10 to high so it turns on a light; if the level goes above 600, set the pin to low to turn off the light’.

There are maximum and minimum values and they are determined by both Tiny Basic, which allows for –32767 to 32767, and the sensor itself.  Some will return a non zero value to indicate an ‘on’ value and a zero for ‘off’.  Or, as in the case of the touch sensor, the values switch between 22 and 1023.  Your mileage may vary.

I wrote a short little game, loosely based on Flappy Bird, that uses the light sensor as the controller. The code is posted here and shows a real world use for using AREAD to retrieve data from an external source.  You could take the code and fancy and add code to trigger a buzzer when the bit hits a pipe (using either AWRITE or DWRITE.) 

There all kinds of things you can do with these four simple functions and statements.  A future modification to the language might be the ability to auto run code from eeprom, which would allow HB Tiny Basic to then be used as a control language.  You can do a lot with few resources.

Advertisements

Bluetooth Communication for the Half-Byte Console (or how to make your HC-06 work)

WP_20140619_007Wireless transmission of data is mostly preferable to using physical connections and, in the Arduino world, it is no different. If you want a network of sensors or need to have several microcontrollers communicate with each other or a PC, you may not be able wire them together. Fortunately, there are several wireless alternatives that are simple and inexpensive.

While scouting eBay and other sources for parts for the Half-Byte Console, I found these cheap, simple and cute little Bluetooth modules. They also happen to work with the serial connection on your Arduino. If you decide to use the standard serial pins (RX/TX) then coding the module is a no brainer: just use Serial.Write, Serial.Print, etc. to send data or Serial.Read(), etc. to get data. If you need to use other pins, use SoftwareSerial.  It really is that simple.

The particular module I have is the JY-MCU BT-Board v1.02pro (HC-06).  It was $4.99 on eBay.

Wiring it up was simple: WP_20140619_014

  • RX->TX
  • TX->RX
  • GND->GND
  • VCC->+5v

When the module is powered up, and found by another Bluetooth enabled device, it shows as ‘HC 06’.  The key is ‘1234’.  Once the device pairs with the HC-06, you are now ready to communicate. On a PC, you’ll need some kind of terminal app that can access a virtual serial port or Bluetooth directly. I found a Windows 8 Modern Appbtserialterm called Bluetooth Serial Terminal and it works great (it is also free from the Windows App Store.) The application is very simple, there’s no real setup as it detects BT devices and configures itself accordingly.

The module defaults to 9600baud, which is a bit slow, but you can reconfigure it. I chose to leave it at the default since the particular application I have in mind will not require more than that.  I’ll spare the dirty details of configuring the module and leave it to ‘Stan’ at the 42bots blog.

Since the module is serial, it works with Arduino Tiny Basic.  As long as you keep it on the RX/TX pins, you can use the built-in Tiny Basic serial statements to talk to and send data over bluetooth.

SPRINT “Hey ma, I’m on Bluetooth now!”

will send that sentence over Bluetooth and be displayed on the BT enabled device. You can IN( 0 ) to receive data from the module. It is very simple.

WP_20140619_011This little module, coupled with an Arduino Pro Mini and some sensors would make a cool little environmental (one use) device. No screen would be needed, which brings cost way down. Instead, you could use your BT enabled phone or tablet and when it gets near the device, they auto pair and you can open up your terminal or, perhaps, dedicated app and get the data streamed to you. I envision, under Windows 8 or Windows Phone 8, having a live tile that would display this data for you. 

It also opens up some cool game ideas. Awhile back, I wrote of using Basic Stamps to create a meshed Star Trek like game.  These little BT modules could make that really easy to do.  There are always possibilities.

I am really impressed with this little module. It was super simple and really cheap and, it just works.

For a cheap, simple and effective means for wireless data in your Arduino projects, these little gems seem to be the way to go.