Using Half-Byte Embedded Tiny Basic to Teach

HBPortableLabIt is 2017 and we have a slew of low cost or free tools available that teach anyone how to program a computer.  Just for Windows, we have something called Small Basic, from Microsoft. It is free and has a bunch of material you can use to teach anyone, especially children, how to code. There is also Python, Minecraft and a host of other, modern tools.

So, why use something as crude as Tiny Basic? One that requires a terminal? Well, there are a few reasons you may want to do this.

Cost, for one. 

It is free. It runs on Arduino and Arduino clones.  You can use it to also teach basic electronics.

And, that is what I am doing…using it to teach not only programming, but also how computers work.  It is really more for the latter as Small Basic cannot manipulate sensors and other hardware like Tiny Basic can.  Since Tiny Basic includes instructions for reading temperature sensors and a real time clock, it is perfect for teaching things like turning on something on if the temperature gets above a given number or it if is 5 o’clock, turn off something.

I recently started doing this with my step son.  We used Embedded Tiny Basic on my ‘portable’ lab, which contains an Arduino UNO clone, a 2 x 16 LCD, breadboard and voltmeter.  We first made one green LED blink, then added a second, red LED blink.  I used Tiny Basic to explain how to talk to the LED’s and used the DELAY instruction to make the LED’s blink at a constant interval.  I also took the opportunity to teach him binary.  We had discussed it previously, but I don’t think he really got it. Until now.  Using the DWRITE statement, which takes two parameters…pin number and a zero for off or 1 for on.  Having him use that code got him to understand the concept.  Small steps.

His mind is wandering now…’I can build a robot…a game…something to tell me when Xander is coming down the hall…’ Xander is his four year old brother. 

There are those of you out there who are thinking that this is a terrible idea, using Tiny Basic, that is.  Well, no, not really.  He is getting real instruction with a more object oriented and modern language while using Tiny Basic to learn the nitty gritty of the hardware.  You do not need a modern, object oriented language to blink an LED. 

I will post future updates on our progress as well as sample code.  Below is the code we used to blink the LED’s.

110 FOR X=1 TO 50
120 DWRITE 3, 1
140 DWRITE 3,0
160 NEXT X

(For single LED-it was on digital pin 3)


Type in Game: PONG! (or, something close)

WP_20160911_21_48_56_Pro (3)Today’s type in game for HB Tiny Basic is a PONG! variant.  I cannot take full credit for this one, I found the original on a Japanese educational site devoted to teaching microcontroller programming, using Half-Byte’s Tiny Basic(!) (a variation of it, anyway) and for basic electronics.  The original was written in a variant of HB Tiny Basic and also used a 10k potentiometer for the controller.  I fixed a couple of bugs, got it to work with Nunchuk AND squeezed into a somewhat smaller memory footprint.

The game has a little bit of intelligence, it does a decent job of trying to guess where the ball will go, but, it is not perfect and it is possible to win the game.  There are some nice uses of the language, such as trying to include something like an OR statement when figuring out where the ball is going and takes advantage of an undocumented ‘feature’ of LINE: if you specify ‘2’ as the ‘color’ parameter, it simply inverses the pixels in the line.  This eliminates the need for multiple statements to draw and erase the paddles.  Quite clever.

Gameplay is super simple: the computer ALWAYS serves, the score goes to nine and stops. You are always on the right. You use the thumb stick up and down to control your paddle.

Weird things are likely to happen, it is not perfect and there’s no more room for improvement (challenge?)

Anyway, have fun!

10 CLS:A=0:B=0:W=48:H=32
30 BOX 0,0,W,H,1
40 U=H/2-3:V=U
50 LINE W-5,U,W-5,U+5,2:LINE 4,V,4,V+5,2
60 CURSOR 8,1:? A:IF A=9 STOP
70 CURSOR 3,1:? B:IF B=9 STOP
80 D=1:E=1:IF (U+V)&1 E=-1
90 X=5:Y=V+3:SET X,Y
100 C=50
110 IF C>0 C=C-1:GOTO 240
130 X=X+D
140 IF X=0 A=A+1:GOTO 60
150 IF X=W B=B+1:GOTO 60
160 IF X=W-6 IF Y>=U IF Y<=U+6 D=-D:TONE 440,100
170 IF X=5 IF Y>=V IF Y<=V+6 D=-D:TONE 440,100
180 Y=Y+E
190 IF Y=1 E=-E
200 IF Y=H-1 E=-E
210 IF X=W-6 IF Y=U IF E=1 E=-1
220 IF X=W-6 IF Y=U+5 IF E=-1 E=1
230 SET X,Y
240 LINE W-5,U,W-5,U+5,2
250 U=H-2-PAD(1)/8
260 IF U<0 U=0
270 IF U>H-6 U=H-6
280 LINE W-5,U,W-5,U+5,2:LINE 4,V,4,V+5,2
300 IF D=1 GOTO 370
310 IF X>=28 GOTO 370
320 IF X=27 IF A<=B GOTO 370
330 IF E=1 Q=Y+X-4:IF Q>=H Q=32-H
340 IF E=0 Q=Y-X+4:IF Q<0 Q=-Q
350 IF Q<V+3 IF V>1 V=V-1
360 IF Q>V+3 IF V<25 V=V+1
370 LINE 4,V,4,V+5,2
400 DELAY 20:GOTO 110

HB Tiny Basic Type in Game: Hurkle

For those of you who are old enough to know and remember the TRS-80, Cromemco or Altair will remember the game of Hurkle.

WP_20160908_23_16_10_Pro (2)A Hurkle is a legendary beast that, even today, remains highly elusive creature.  So elusive, in fact, that few have seen a Hurkle and lived to tell about it.  Of course, you, our intrepid adventurer, are different.  For, you, you have HALF-BYTE’S Tiny Basic and an Arduino or compatible microcontroller at your disposal.  An arsenal worthy of such of hunt.

Our Hurkle adventure takes place on a 10 by 10 grid.  You have to find the creature by deducing its where abouts on the 10 by 10 grid. Unfortunately for you, you will have from five to twenty moves in which to find the creature. Each time your adventure begins, your time is recalculated. This makes the level of difficulty even higher. You will, of course, through the use of the microcontroller, be told which direction you must travel.  Your grid follows a North-South, East-West pattern.  The X axis is West to East and Y axis is North to South. 

This simple game is rather difficult to play.  Sure, there is a way to cheat, but I’ll let you figure that out. And, once you do, you should just type NEW and move on to something else.

This game was originally published by the People’s Computer Company in Menlo Park California. I have adapted it from the Big Book of Computer Games, published in the early 1970’s.

NOTE: I had originally posted a version of the game, as part of a sample code page. The listing was broken and the game did not work correctly, as published.  This one does.  Apologies for that.

Below is the HB Tiny Basic listing.

10  CLS: ?”HURKLE”
99  # Converted to TINY BASIC by George Gray
110 N=RND(10)+5
120 G=10
210 ?
220 ? “A hurkle is hiding on a “,G,” by “,G,” grid.”
230 A = RND(G)
240 B = RND(G)
310 FOR K=1 TO N
320 ? “Guess #”,K
330 ?”X=”;: INPUT X
335 ?”Y=”;: INPUT Y
340 IF ABS(X-A)+ABS(Y-B)=0 GOTO 500
350 # ? INFO
360 GOSUB 610
380 NEXT K
420 ? “Sorry, that’s “,N,” guesses.”
430 ? “The hurkle is at “,A,”,”,B
450 ? “Let’s play again. Hurkle is hiding.”
470 GOTO 285
500 ? “You found him in “,K,” guesses!”
530 FOR I=1 TO 10
532 TONE 1000,75
534 NEXT I
540 GOTO 440
610 ? “Go “;
620 IF Y=B GOTO 670
630 IF Y<B GOTO 660
640 ? “South “
650 GOTO 670
660 ? “North “
670 IF X=A GOTO 720
680 IF X<A GOTO 710
690 ? “West “
700 GOTO 720
710 ? “East “
720 ?””

IT’s HERE! Half-Byte Tiny Basic 3!

randomdotsOne of the things I have really enjoyed since embarking on my Arduino journey and this blog, is seeing others take things I’ve worked on or created and expand upon it. This is especially true with Half-Byte Tiny Basic, something that started out as the work of Mike Field, who, himself used the work of another person. HB Tiny Basic is an iterative work, built on the work of others as well as myself.  For HB Tiny Basic 3, I have incorporated the work of others to make it better.  I would love to take credit for these changes, but, I cannot.  This release is strictly due to Hill Satoshi of the Hirosaki University Faculty of Education and someone named ‘Koyama’.  A Big thanks to them.

Please visit Hill Satoshi’s page. There are a lot of great ideas, basic electronics information and some code snippets…be careful, some features of the basic there were not incorporated in HB Tiny Basic, like the motor control and PLOT (which is the same as Set and Reset.)

Among the additions and changes are an auto load and auto run feature (requested by many of you) and a better eSave and eLoad feature.  In fact, I like much of what was done to the language and may incorporate more at a later date.

So, what’s new?

Two new statements have been added: NUMLED and BMP.  A new function, TREAD.  The aforementioned auto load and auto save feature.  Three new operators: %, & and |. % returns the remainder of a division, & is logical AND and | is a logical OR.

What’s changed?

The code to do a eSave or eLoad has changed and should be more reliable.  The startup code has changed a little, dynamically calculating the amount of ram that is available.  Ability to read in characters from the serial input as if it were the keyboard. This means you can connect to a serial terminal and enter and run Tiny Basic code without a PS/2 Keyboard attached. And a few minor tweaks here and there.

New Statements

The first new statement is NUMLED.  If you connect a seven segment LED directly to the device you are running HB Tiny Basic on, you can output directly to the LED.  The syntax is:

NUMLED x  where x is a digit from 0 to 9.


20 for I=0 to 9

30 numled I

40 delay 1000

50 next I

60 goto 20


BMP allows you to draw a bitmap on the screen.  It is very much like the DRAW statement in old Microsoft Basics. It feature its own mini design language for you to draw on the screen. A minimum of three parameters are required: x,y,string.  Where x and y are the start points and string is the definition.

Table of Commands for Mini Language:


(0 is black;
1 is white)


(0 is black;
1 is white)







10 cls 
20 bmp0,0,"ffffc00000007fff","ffffc00000001fff", "fffe0000000003ff","fffc0000000000ff", "fff80000000000ff","fff000000000007f", "ffe000000000003f","ff0000000000003f" 
30 bmp0,8,"ff0000000000001f","ff2f00000000000f", "ff1fc0000000000f","ff000c000000000f", "ff003f000000000f","ff0020400000000f", "fc0000fc1800000f","fc00000f7f3c000f" 
40 bmp0,16,"f80000037f7e000f","f8000000007e0003", "f01c00c0001cf7c3","f03e03c00000efcf", "f03e07c00e00000f","c03f1ff01f000003", "c03ffff03f800003","cf3ffff87fc380c3" 
50 bmp0,24,"ce3fffffffc3c0c3","c03fc03fffe7f1cf", "c03f003ffffff3c0","ff3fff3fff0fff88", "ff1ffffffc0fff1c","ff9ffffff043ff3c", "ff8fffc03ff3ff38","ff8fffc03ff3fe00" 
60 bmp0,32,"ffcfffff3ffffc01","ffc7fffffffffcff", "ffc3fffffffff8ff","fff3ff007ffff1ff", "fff1ff0c7fffc3ff","fff0ff0c7fffc7ff", "fffc7f0c7fff8fff","fffe3f807ffe1fff"
70 bmp0,40,"ffff3fc0fffe3fff","ffff07c3fff07fff", "fff023ffffe1ffff","ffe0203fff83ffff", "ff07e00000003fff","fc0fe3ffff8f0fff", "fc3fe3ffff8f0fff","f0ffe3ffff8fc1ff" 
80 shift 1,3:delay 99:goto 80


Aviary Photo_131159334768733501

The example code above produces this bitmap.

New Function:

TREAD is an alternate method for reading a temperature sensor. It does not require any additional libraries and reads the sensor directly. Usage is: Var=TREAD(pin)  where pin is A0 to A7.


100 a=TREAD(A3)

The example reads in a value between –30 and 50 degrees Celsius.

Auto Load and Auto Save

HB Tiny Basic will now load the saved program in EEPROM.  You have three seconds to press a key or the loaded program will auto run.  If you press a key in those three seconds, you will get the normal prompt as in the past.  This feature is useful if you want to use HB Tiny Basic for embedded use or simply to restart if the device loses power.  There are a lot of uses for this feature.

Other small changes have been made, but are not worth discussing as they are mostly cosmetic or code optimizations.

In reviewing my code, I see just how sloppy it has become. I am going to clean it up, but, until I do, please feel free to offer up any suggestions you may have for features or changes.  As always, the code is free and open.  I ask that you leave the regular header intact, at the top of the code. I want all involved to be recognized.

Thanks and let us know what you do with Half-Byte Tiny Basic.

Download Link: Half-Byte Tiny Basic 3

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.

The ESP8266 WiFi Module–how to get it to work with an Arduino

WP_20160106_15_27_46_Pro_LIFinally got around to playing with the ESP8266 WiFi Module with an Arduino UNO.  I am using the UNO simply because it has a steady, 3.3v output while the HalfByte Console isn’t as steady and the module is, from everything I have read, is not tolerant of much more than the 3.3 volts.  Until I fix the console’s 3.3volt output, I will use the UNO.

Before I go on, I have to say, this little board isn’t very reliable. It is only connecting about a third of the time.

The documentation is spotty, even though it has been out for quite sometime now.  So, I hope to help anyone who just wants to use it as a WiFi module and not reprogram it to play Tic Tac Toe.

First, it is important to know which module you have as there are quite a few variations.  You go here to figure that out. The board I have is an ESP-01 Rev 2.  The Rev is important as you have a couple of extra steps from the rev 1 board.

From the wiki, here’s the pin out for the rev 2 board:


In order for this thing to work, you MUST jumper the CHIP_EN (or CHIP_PD) pin to the VCC pin, pulling the pin high.  This enables the whole thing to, you know, work. 

One other important detail…the baud rate is 115,200.  Most documentation I read said it was 9600, but, at least on mine, it is 115,200 baud.

Those two things were the key for me to get my module to connect and work.

Wiring it up is easy:image

UTX RX (pin 0)
URX TX (pin 1)
VCC 3.3V
RST Reset

The code is straightforward enough. I copied an example from the wiki and modified it to work with my module and took out some extraneous junk.WP_20160106_15_59_14_Pro_LI (2)


#include <SoftwareSerial.h>
#define SSID        “xxx”
#define PASS        “xxx”
#define DST_IP      “”    //
SoftwareSerial dbgSerial(10, 11); // RX, TX
void setup() 
    // Open serial communications and wait for port to open:
    dbgSerial.begin(9600);  //can’t be faster than 19200 for softserial
    dbgSerial.println(“ESP8266 Demo”);
    //test if the module is ready
        dbgSerial.println(“Module is ready”);
        dbgSerial.println(“Module have no response.”);
    //connect to the wifi
    boolean connected=false;
    for(int i=0;i<5;i++)
      connected = true;
    if (!connected){while(1);}
      //set the single connection mode
void loop()
  String cmd = “AT+CIPSTART=\”TCP\”,\””;
  cmd += DST_IP;
  cmd += “\”,80″;
  if(Serial.find(“Error”)) return;
  cmd = “GET / HTTP/1.0\r\n\r\n”;
    dbgSerial.println(“connect timeout”);
  while (Serial.available())
  char c =;
  if(c==’\r’) dbgSerial.print(‘\n’);
boolean connectWiFi()
  String cmd=”AT+CWJAP=\””;
    dbgSerial.println(“OK, Connected to WiFi.”);
    return true;
    dbgSerial.println(“Can not connect to the WiFi.”);
    return false;

Because I am using the serial I/O on the UNO for the module, I used softwareserial to talk to a HalfByte Console running the HalfByte graphical terminal sketch so I could see the output of the module.  Normally, you would, likely, not have any kind of output on the Arduino as you’d be using the WiFi module for your I/O. I’m guessing.

I may order a few more of these things to play with reprogramming them and running code directly on them.  There is a Basic Language interpreter, LUA and Javascript for them, so I may play with that.  For now, IF I can get it working reliably, I may pair one with an ATTINY85, like a Trinket or DigiSpark, and a DHT11 temp sensor and set up a network of wireless thermometers in the house. 

I can see the potential, but the reliability is an issue.

PROGMEM issues with Arduino 1.6.x and how to fix them

WP_20140826_22_20_46_ProThere is a new version of the Arduino IDE out, version 1.6.1. If you do not have, you can go to the Arduino web site and grab yourself a copy.  It is much faster than the older versions.  Overall, it seems to be better in most aspects. Except for one…the new compiler breaks some of your code.

I installed it and then tried to compile Half-Byte Tiny Basic.  Expecting a clean compile, I was surprised by the errors it generated.  Upon investigation, I found that the references to PROGMEM was the cuprit. Further research revealed a fairly easy remedy, but one that was difficult to find, so I thought I’d make it easier.

Error:  variable ‘message’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))

So, this was the first error, which led to a second error that isn’t really an error (it goes away when you fix this) so I’m not going to talk about it, it is one that is safe to ignore.  Now, simply changing ‘Static’ to ‘Const’ does not actually fix the problem. No, like the error says, you need to specify the SECTION to put it in.  Look at the old way below, then check out the new way.

// Keyword table and constants – the last character has 0x80 added to it
static unsigned char keywords[] PROGMEM = {
// Keyword table and constants – the last character has 0x80 added to it
static unsigned char __attribute((section(““))) keywords[] = {
‘L’, ‘I’, ‘S’, ‘T’ + 0x80,
‘L’, ‘O’, ‘A’, ‘D’ + 0x80,
‘N’, ‘E’, ‘W’ + 0x80,
‘R’, ‘U’, ‘N’ + 0x80,
‘S’, ‘A’, ‘V’, ‘E’ + 0x80,


Fixing this killed the second error that was showing up.  BUT…

ANOTHER error (third overall) reared its head:

Error: <variable> causes a section type conflict with <section>

Specifying the attribute did the trick, simply using PROGMEM causes issues. While the original error went away (along with that secondary error), the section type error appeared.  This one, though, was simple. I was stuffing two TYPES into the same section and that is a no-no in the new world.  Just adding the code below AND specifying a different section of PROGMEM did the trick. This way allows you to segment your data as well.

// Workaround for
‪#‎ifdef‬ PROGMEM
‪#‎undef‬ PROGMEM
‪#‎define‬ PROGMEM __attribute__((section(“.progmem.vars”)))

Once I made these changes, my code compiled and uploaded just fine.  I hope this saves you some time.