Embedded Tiny Basic: build apps for your projects

Embedded Tiny Basic: build apps for your projects

I, recently, embarked on building a useful if not glitzy digital clock. My first idea involved something retro: using four, seven segment, LED’s to look like something from the late 1970’s.  At the same time, I was also playing around with 8×8 LED Arrays, using the MAX7219 chip.  While playing with that, I connected one of my 1307 RTC’s to the array and loaded up a sketch to show the date and time on the arrays.  Then, it hit me…this is a much cooler clock and I can do more with it.

So, I built a prototype using one of my Half-Byte Console boards, minus the video, audio and Nunchucky connector.  Hardware wise, it was really simple…connect the arrays to pins 10, 11 and 12, the RTC to A4 and A5 and, for extra coolness, a BMP180 temp and humidity sensor to the sca and scl on the RTC.  Viola! modify the code to handle the BMP180 and Presto! A cool clock.

wp_20170121_01_55_22_pro

Prototype clock for developing Embedded Tiny Basic

I decided to build a second one to take to work.  I thought it might be useful to be able to throw up a custom message to scroll for when, say, I was in a meeting. The more I thought, the more I was convinced this thing needed some kind of control program that would be easily modified from, say, a smartphone and Bluetooth.

Well, I already had the genesis of that control program: Half Byte Tiny Basic.

So, I looked at the source and made a copy.  I then went in and removed stuff I would not need, like all of the graphics statements and functions. I removed the TVOut library, and  all of the video handling code. And TONE.  I miss TONE.

I did not need the Nunchuck code, so it was gone as well.  What was left was a nice, small shell of my control program.  I added the libraries for the 8×8 LED Array (MAXMATRIX) and added statements to manipulate the arrays: SCROLL to display text, SET to turn on or off individual LEDs, and DIRECTION to tell the arrays  which way to scroll the text.  I already had code to handle the DHT-11 temperature sensor, so I left that in (and decided to use it instead of the BMP180) and added T

wp_20170122_21_13_11_pro

Random dots on the clock

EMP and HUMID to scroll those values across the arrays, and added code for the RTC.I was going to add a mechanism that would interrupt the running program if a signal from the serial device was detected, but, there was already one there…the IN(0) function. So I left it. I now had a decent little programming language for the clock.  With HB Tiny Basic’s ability to autorun whatever is in the EEPROM, if it lost power, th

wp_20170119_15_23_11_pro

Clock, in action

e clock would just start running on its own. Sweet!

 

The HC-06 Bluetooth module works very nicely. It connects to the serial pins and communicates as if the device were connected directly to the controlling device-a PC or smartphone.  Everything fits nicely into these cheap pencil boxes I picked up from Wal-Mart for a buck each. They are just big enough for the HB Console board and are wide enough for the LED Arrays.  They don’t look all that impressive, until you power up the clock…the bright LED’s shine through the translucent plastic nicely.

Embedded Tiny Basic is useful for giving some level of intelligence to other wise dumb devices.  While you only have about 1K of RAM to use for Embedded Tiny Basic, I think that will be adequate for most things.  There is, currently, no motor control, but it could be quickly and easily added.  The functionality that is there is probably going to be good for quite a few projects.  If not, it can be modified quickly.  I am already seeing where it can be modified, even for just this particular project.  I have a few others in mind, so stayed tuned for those.

In the mean time, below is a list of the additions and a release date for the language is forthcoming.  I need to clean up the code-a bunch-before releasing it and, as well, making sure I have all you will need in order to compile and use Embedded Tiny Basic.

 New statements and function:
  • SETTIME hours,minutes,seconds,day,month,year
    • sets the time and date for the RTC
  • SET col,row,on or off
    • Turn on or off the LED at column, row
  • X=HOUR(0)
    • Get the current hour
  • X=MINUTE(0)
    • Get the current minute
  • X=MONTH(0)
    • Get the current month
  • X=DAY(0)
    • Get the current day
  • SCROLL var or “text”
    • Scrolls whatever is in the quotes
    • If there are no quoted strings, a variable value or number is displayed
  • DIRECTION 1-4 (1 is left, 2 is right, 3 is up and 4 is down)
    • Specifies the direction of the scrolling text, 1 is the default.
  • TIME (sends the date and time to the LED array)
    • Scrolls the current date and time
  • HUMID (sends humidity to LED array)
    • Scrolls the current humidity
  • Temp (sends the temp to LED array)
    • Scrolls the current temperature
  • X=TEMP(0 or 1)
    • Get the current temperature and put it in variable 'x'
    • A zero means use Celsius, a one means Farenheit
  • X=HUMID(0)
    • Returns the humidity to the variable 'x'

Below is the listing for the current “HELLO” app:

100 PRINT “Welcome to Half-Byte LED Programmable Clock”
110 SCROLL ” HALF-”
111 SCROLL “BYTE Clock…..”
112 IF HOUR(0)<12 SCROLL ” Good Morning! ”
114 IF HOUR(0)>11 IF HOUR(0)<18 SCROLL ” Good Afternoon! ”
116 IF HOUR(0)>17 IF HOUR(0)<=23 SCROLL ”  Good Evening!  ”
120 TIME
125 IF IN(0)<>-1 GOTO 600
130 SCROLL “.   Temp is ”
140 TEMP
145 SCROLL “F  ”
150 SCROLL “Humidity is ”
155 SCROLL “%”
160 HUMID
170 SCROLL ”  Hello!  ”
180 IF IN(0)<>-1 GOTO 600
190 IF RND(99)>50 GOTO 110
200 SCROLL ”       ”
205 O=MINUTE(0)
210 W=15
220 H=7
230 X=RND(W)
240 Y=RND(H)
250 P=RND(W)
260 Q=RND(H)
265 IF IN(0)<>-1 GOTO 600
300 SET 16+(X),Y,1
310 SET 16+(X),H-Y,1
320 SET 16+(W-X),Y,1
330 SET 16+(W-X),H-Y,1
340 IF IN(0)<>-1 GOTO 600
350 SET 16+(P),Q,0
360 SET 16+(P),H-Q,0
370 SET 16+(W-P),Q,0
380 SET 16+(W-P),H-Q,0
390 IF IN(0)<>-1 GOTO 600
400 K=MINUTE(0)
410 IF K-O>1 GOTO 110
590 GOTO 230
600 SCROLL “DONE  “

A note about how the arrays are referenced, using SET.  Embedded Tiny Basic is setup to handle up to four arrays. My clock only uses two, but Basic does not know this, so individual LED addressing has to be offset by 16.  Normally, with four arrays, the upper left LED would be 0,0. In my clock, it would be 16,0 since I am only using the RIGHT MOST TWO arrays, each are 8 across and 8 down.

Some other things to note from the listing above…the line IF IN(0)<>-1 GOTO 600 that is sprinkled through out the program will poll the serial port to see if there is a key press. If there is a keypress, we want to stop execution and goto line 600.  In this case, it just says ‘Done’ and stops. Lines 205, 400 and 410 make up a timer. Lines 200 through 590 just displays a random dot pattern, as a distraction.  We don’t want this all day, so we only let it run for about a minute.  Line 205 records the minute it started and line 400 gets the minute after each pattern is displayed.  Line 410 evaluates the elapsed time by subtracting the start time from the end time. If the result is greater than a one, it goes back to the clock routine.  Otherwise, it displays a new pattern. Lines 112 through 116 determines if it is morning, afternoon or evening.

The Scroll statement is lacking and somewhat buggy. I am working on fixing it, and adding more functionality to it, to make it more versatile, like PRINT currently is. I also want to add a mechanism to the language to allow it to receive messages, via Bluetooth, from, say, a smartphone.  Limited gameplay may also show up in a future release.

Embedded Tiny Basic will be released soon.

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
120 RESET X,Y
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
380 RESET X,Y
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”
20  ?”FOR HB TINY BASIC”
99  # Converted to TINY BASIC by George Gray
100 # HURKLE – PEOPLE’S COMPUTER COMPANY, MENLO PARK CA
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 ?””
730 RETURN

More type in goodness…Half-Byte Tiny Basic type in game, Zapp the Moon Man, take 2

zappthemoonmanRelease three of Half-Byte Tiny Basic ate up about eight more bytes of memory than the previous release, so there are now 938 bytes free for user code to reside.  My last version of Zapp the Moon Man—previously unpublished—featured the Moon man’s ability to move down the screen and attack as well as the user’s ability to move the cannon back and forth.  Sadly, for this release, I’ve had to remove the downward mobility of the Moon Man, but I have left in the user’s ability to move and also made the ‘hit box’ better, resulting in a somewhat easier game play.

This game shows off just how versatile Tiny Basic can be, how speedy the ATMega 328 is and how quickly Tiny Basic can interpret your code.

At any one time in the game, both your cannon and the Moon Man can be moving as well as the torpedo you are shooting at the moon man.  Three objects to track on the screen. In interpreted BASIC. Running on a microcontroller that was meant for turning relays on and off, not playing video games. And, it does it rather smoothly.  The jerkiness that is there is there by design, to mimmick those games from the 1970’s.

The game is pretty primitive. It resembles Space Invaders, but there is only one ‘invader’, the Moon Man, and there are no protective shields…heck, the Moon Man does not even shoot at you…yet.  It does feature some primitive, character based, animated graphics.  The Moon Man sort of looks like a Space Invader. As it moves back and forth, its antennae move and its ‘feet’ swivel side to side.  You use a Wii Nunchuck’s thumb stick to move and the Z button to fire your torpedo. The game keeps a score…10 points for every Moon Man you destroy.  You hear a launch tone when you fire and, when you hit a Moon Man, you see a little explosion like effect. And…that’s it.  Simple and not earth shattering (that will be in a future update.)

So, with out any further delay…(One note: when typing in the code, do not put in extra spaces.  Use one space between the line number and the code, and one space before line numbers in things like GOTO or GOSUB.  The listing below inserted additional spacing, you can ignore it.)

100 CLS:ECHO 0
110 A=0:B=0:O=75
120 X=10:Y=10:Z=5:F=0:D=1:S=0
140 LINE 0,48,80,48,1
150 GOSUB 700
160 GOSUB 900
170 P=PAD(3):Q=PAD(0)
180 IF P=1 F=1:TONE 200,100
190 IF F=1 GOSUB 1000
192 IF Q>200 GOSUB 600
194 IF Q<100 GOSUB 600
200 A=A+D
210 IF A>15 D=-1
220 IF A<3 D=1
230 GOSUB 1200
290 GOTO 140
600 CURSOR X,Y:?”  “;
610 IF Q>200 I=1
620 IF Q<200 I=-1
630 X=X+I
640 IF X<2 X=2
650 IF X>17 X=17
660 GOSUB 900
690 RETURN
700 IF D=1 CURSOR A,B:?CHR(152);:DELAY O:CURSOR A,B:?CHR(153);:DELAY O:CURSOR A,B:?”  “;
710 IF D=-1 CURSOR A,B:?CHR(153);:DELAY O:CURSOR A,B:?CHR(152);:DELAY O:CURSOR A,B:?”  “;
790 RETURN
900 CURSOR X,Y:?CHR(150);
990 RETURN
1000 CURSOR X,Z:?”|”;:DELAY 20:CURSOR X,Z:?” “;:DELAY 20
1050 Z=Z-1
1060 IF Z=0 IF A=X Z=5: GOTO 1100
1070 IF Z=0 IF A=X+1 Z=5: GOTO 1100
1080 IF Z=0 Z=5:F=0
1090 RETURN
1100 CURSOR A,B
1110 ?”***”;:DELAY 180:CURSOR A,B:?”XXX”;:DELAY 170:CURSOR A,B:?”   “;:A=0:B=0:F=0
1190 S=S+10
1200 CURSOR 0,5:?”SCORE:”,S;
1290 RETURN

 

If you come up with any improvements, optimization, etc., please let us know.

Oh, one big caveat…as it does use up all but 15 bytes of RAM, your keyboard buffer is limited to 15 bytes…Tiny Basic does not set aside a dedicate memory for keyboard input. It dwindles as you use up memory.  So, keep in mind that you may have to delete a long line and split it up—which will use at least three bytes plus the content of the line.

UPDATE:  Since this was posted, I have made a few improvements to the game AND saved even more RAM, about 90 bytes total.  Among the improvements: a random speed for the moon man, the ability of the moon man to descend on you and…you can lose the game.

The updated code is below.

100 CLS
110 A=0:B=0:O=RND(100)
120 X=10:Y=8:Z=5:F=0:D=1:S=0
140 LINE 0,48,80,48,1
150 GOSUB 700
160 GOSUB 900
170 P=PAD(3):Q=PAD(0)
180 IF P=1 F=1:TONE 200,100
190 IF F=1 GOSUB 1000
192 IF Q>200 I=1:GOSUB 600
194 IF Q<100 I=-1:GOSUB 600
200 A=A+D
210 IF A>16 D=-1:B=B+1:if b>=Y CLS:?”You lose!”:Delay 3000:goto 100
220 IF A<1 D=1
230 GOSUB 1200
290 GOTO 140
600 CURSOR X,Y:?”  “;:X=X+I
640 IF X<2 X=2
650 IF X>17 X=17
660 GOSUB 900
690 RETURN
700 CURSOR A,B:?CHR(152);:DELAY O:CURSOR A,B:?CHR(153);:DELAY O:CURSOR A,B:?”  “;
790 RETURN
900 CURSOR X,Y:?CHR(150);
990 RETURN
1000 CURSOR X,Z:?”|”;:DELAY 20:CURSOR X,Z:?” “;:DELAY 20
1010 Z=Z-1
1060 IF X=A IF Z=B GOTO 1100
1070 IF X=A+1 IF Z=B GOTO 1100
1080 IF Z=0 Z=5:F=0
1090 RETURN
1100 CURSOR A,B
1110 ?”***”;:DELAY 180:CURSOR A,B:?”XXX”;:DELAY 170:CURSOR A,B:?”   “;:A=0:B=0:F=0
1120 Z=5
1190 S=S+10:O=RND(100)
1200 CURSOR 0,5:?”SCORE:”,S;
1290 RETURN

More type in goodness…Half-Byte Tiny Basic type in game, Zapp the Moon Man, take 2

zappthemoonmanRelease three of Half-Byte Tiny Basic ate up about eight more bytes of memory than the previous release, so there are now 938 bytes free for user code to reside.  My last version of Zapp the Moon Man—previously unpublished—featured the Moon man’s ability to move down the screen and attack as well as the user’s ability to move the cannon back and forth.  Sadly, for this release, I’ve had to remove the downward mobility of the Moon Man, but I have left in the user’s ability to move and also made the ‘hit box’ better, resulting in a somewhat easier game play.

This game shows off just how versatile Tiny Basic can be, how speedy the ATMega 328 is and how quickly Tiny Basic can interpret your code.

At any one time in the game, both your cannon and the Moon Man can be moving as well as the torpedo you are shooting at the moon man.  Three objects to track on the screen. In interpreted BASIC. Running on a microcontroller that was meant for turning relays on and off, not playing video games. And, it does it rather smoothly.  The jerkiness that is there is there by design, to mimmick those games from the 1970’s.

The game is pretty primitive. It resembles Space Invaders, but there is only one ‘invader’, the Moon Man, and there are no protective shields…heck, the Moon Man does not even shoot at you…yet.  It does feature some primitive, character based, animated graphics.  The Moon Man sort of looks like a Space Invader. As it moves back and forth, its antennae move and its ‘feet’ swivel side to side.  You use a Wii Nunchuck’s thumb stick to move and the Z button to fire your torpedo. The game keeps a score…10 points for every Moon Man you destroy.  You hear a launch tone when you fire and, when you hit a Moon Man, you see a little explosion like effect. And…that’s it.  Simple and not earth shattering (that will be in a future update.)

So, with out any further delay…(One note: when typing in the code, do not put in extra spaces.  Use one space between the line number and the code, and one space before line numbers in things like GOTO or GOSUB.  The listing below inserted additional spacing, you can ignore it.)

100 CLS:ECHO 0
110 A=0:B=0:O=75
120 X=10:Y=10:Z=5:F=0:D=1:S=0
140 LINE 0,48,80,48,1
150 GOSUB 700
160 GOSUB 900
170 P=PAD(3):Q=PAD(0)
180 IF P=1 F=1:TONE 200,100
190 IF F=1 GOSUB 1000
192 IF Q>200 GOSUB 600
194 IF Q<100 GOSUB 600
200 A=A+D
210 IF A>15 D=-1
220 IF A<3 D=1
230 GOSUB 1200
290 GOTO 140
600 CURSOR X,Y:?”  “;
610 IF Q>200 I=1
620 IF Q<200 I=-1
630 X=X+I
640 IF X<2 X=2
650 IF X>17 X=17
660 GOSUB 900
690 RETURN
700 IF D=1 CURSOR A,B:?CHR(152);:DELAY O:CURSOR A,B:?CHR(153);:DELAY O:CURSOR A,B:?”  “;
710 IF D=-1 CURSOR A,B:?CHR(153);:DELAY O:CURSOR A,B:?CHR(152);:DELAY O:CURSOR A,B:?”  “;
790 RETURN
900 CURSOR X,Y:?CHR(150);
990 RETURN
1000 CURSOR X,Z:?”|”;:DELAY 20:CURSOR X,Z:?” “;:DELAY 20
1050 Z=Z-1
1060 IF Z=0 IF A=X Z=5: GOTO 1100
1070 IF Z=0 IF A=X+1 Z=5: GOTO 1100
1080 IF Z=0 Z=5:F=0
1090 RETURN
1100 CURSOR A,B
1110 ?”***”;:DELAY 180:CURSOR A,B:?”XXX”;:DELAY 170:CURSOR A,B:?”   “;:A=0:B=0:F=0
1190 S=S+10
1200 CURSOR 0,5:?”SCORE:”,S;
1290 RETURN

 

If you come up with any improvements, optimization, etc., please let us know.

Oh, one big caveat…as it does use up all but 15 bytes of RAM, your keyboard buffer is limited to 15 bytes…Tiny Basic does not set aside a dedicate memory for keyboard input. It dwindles as you use up memory.  So, keep in mind that you may have to delete a long line and split it up—which will use at least three bytes plus the content of the line.

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.

EXAMPLE:

20 for I=0 to 9

30 numled I

40 delay 1000

50 next I

60 goto 20

BMP

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:

CMD

Pattern
(0 is black;
1 is white)

CMD

PATTERN
(0 is black;
1 is white)

0
1
2
3
4
5
6
7

0000
0001
0010
0011
0100
0101
0110
0111

8
9
A
B
C
D
E
F

1000
1001
1010
1011
1100
1101
1110
1111

 

EXAMPLE:

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.

EXAMPLE:

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.