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.

100 PRINT “INTERVAL”;: INPUT I
110 FOR X=1 TO 50
120 DWRITE 3, 1
130 DELAY I
140 DWRITE 3,0
150 DELAY I
160 NEXT X

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

Advertisements

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

Half-Byte Tiny Basic 2: code examples, random dots

Trandomdotsiny Basic can be a great way to learn how to program. It is primitive enough to be easy to learn, yet powerful enough to complex things in a small amount of memory.  For today’s example, I’m going to show you how to create the random dot pattern I love to show off.

As the resolution on the Half-Byte Computer Console is really low, heck, it is barely there, it does not take much to make a compelling demo.  We have 80 by 48 dots to play with.  Not much, but more than some very early home computers had. But, today, we are only going to use 80 by 46. (Only because I typed the wrong value into the Y=RND statement and was too lazy to fix it.)

Our demo creates four random numbers, one set to turn on the dots and one set to turn them off.  We will use the x, y, p and q variables for the pixels. A fifth number will be generated, using the variable u, and we will discuss that later.

Our demo will create a four quadrant screen to light up the pixels. This will give you a kaleidoscope effect.  So, that means we have to restrict our work to just one quarter of the screen and then mirror it to the other three. Sounds complicated, but it isn’t.

Have a look at the code below:

100 CLS
110 X=RND(40): Y=RND(23)
120 P=RND(40): Q=RND(23)
130 SET X,Y
140 RESET P,Q
150 SET X,43-Y
160 RESET P, 43-Q
170 SET 79-X,Y
180 RESET 79-P, Q
190 SET 79-X,43-Y
200 RESET 79-P,43-Q
210 U=RND(99)
290 GOTO 110

Lines 110 and 120 generate our random numbers, one pixel, to light up and one pixel to turn off.  Lines 130 through 200 calculate the four quadrant locations to turn on or off.  SET will turn the pixel on, RESET will turn them off.  We figure out where to turn them on or off by using offsets. In our case, the offsets are the width (0 to 40) and height (0 to 23). Study what these lines are doing and you can figure out where on the screen each dot will go.  Take a sheet of graph paper and make an 80 by 46 grid.  Use RANDOM.ORG to generate 2 random numbers, one will be from 0 to 40 and the other will be from 0 to 23. Use the SET statements and figure out which cells on your graph paper to color in and then do so.  After you color in the four squares, you can see just how the code works.  Do that a few more times and you get an interesting design.  Now, key the code into Tiny Basic 2 and run it.  Let it go for a while and you’ll see very interesting patterns emerge. After a while, though, the screen gets busy and ceases to be interesting.  So, we need to do something about it.

We need to clear the screen every now and then and let the patterns regenerate.

So, how do we do this? Well, you can it do it a number of ways…poll the keyboard for a keypress, read the Wii Nunchuck, etc.  The easy way, though, is to just do it randomly.  Good thing we have line 210. Line 210 has already created a random number, we just have not done anything with the number. So…lets do something.

220 IF U=93 GOTO 100

That’s all we need to do. Evaluate the value of U and, if it matches our magic number (which can be anything from 0 to 99 as dictated by line 210.  You can put 10000 in there if you like. The random number limit and the number after the equal sign are entirely arbitrary.  But, the higher the numbers, the longer it could take to hit that random number.  It doesn’t matter. For our demo, I chose 99 and 93.  Go ahead, break the program if it is still running and then type in line 220 above. Re-run the program. You will see it switch patterns frequently.

Pretty cool, huh?

Play with the numbers a little. You can limit the pattern to just one quadrant. Change the x and y values in 110 to 20 and 12 (you will need to change line 120 as well.) Then, in the code, everywhere you see 40, change it 20 and change all of the 23 to 12. Run the demo.  You should see the same thing as before, only smaller and in one corner of the screen.  Experiment with this, what do you have to do to put it in, say, the lower right of the screen?  Hint: you will need to offset your set and reset locations.  Look at lines 190 and 200 for a clue. Post your solution in the comments.

I have posted a video here.

In an upcoming example, we will use the Wii Nunchuck to control the drawing.

It’s here! Half-Byte Tiny Basic 2 for Arduino and compatibles

It’s back! Better than ever. Yes, that’s right, Half-Byte Tiny Basic 2 for Arduino.  The first was so good, there just had to be a follow up.  And this version has even more fun stuff like new graphics functionality like ARC and CROSSHAIRS.  New math in SIN and COS.  Enhanced LIST statement and more.  Read on for more on the new goodies.

Tiny Basic weather?

WP_20140826_22_20_46_ProWell, yes and no. Yes in that support for the DHT-11 temperature and humidity sensor has been included. No, it won’t generate satellite images or predict snow storms. But, if you have a project where you need to capture the temperature and/or humidity, this will work.  Temperature is returned in either Celsius or Fahrenheit.  TEMP( 0 ) will return the temperature in Celsius and TEMP( 1 ) will return it in Fahrenheit. HUMIDITY with any value parameter will return the relative humidity.

EXAMPLE:

100 CLS
110 PRINT “Temperature: “, TEMP( 0 ),”C”
120 PRINT “Relative Humidity: “,HUMIDITY( 0 ),”%”
130 DELAY 4000
140 GOTO 100

New Graphics Functions

POLY

Poly will draw an abnormal circle. It is abnormal because the sides will not be ‘normal’. They will be straight lines IF the radius is small. As the radius gets larger, the circle looks more normal, then it begins to ‘explode’. Poly gives nice random like patterns or explosions for games.

POLY start_x, start_y, radius, points, color
Where start_x and start_y are the screen position to draw the poly;
Radius is width of the circle;
Points is the size of the ‘sides’ of the circle;
Color is black or white
100 CLS: CENTER: PRINT “EXPLODING”
110 R=RND(20): P=RND(15)
120 POLY 40,20,R,P,1
130 DELAY 100
150 POLY 40,20,R,P,0
160 DELAY 100
170 GOTO 110

 

INVERT

One thing you can do to get a user’s attention is to flash the screen. Tiny Basic allows you to do this quickly, by using the INVERT statement. It takes no parameters and its syntax is simply:

INVERT
100 CLS
110 CURSOR 3,2
120 PRINT “TO CONTINUE WITH”
130 CURSOR 3,3
140 PRINT “PROCESSING, PRESS C”;
150 A=INKEY(0)
160 IF A=67 GOTO 210
170 INVERT
180 DELAY 500
190 GOTO 150

210 #CONTINUE…

Starting at line 150, we wait for a key to be pressed. If it is C, goto 210 else, we invert the screen, wait a half second and do it again. If there is no delay, the screen would be just obnoxious. Inserting a delay slows it down a bit.

 

CROSSHAIRS

Crosshairs draws a graphical ‘t’ on screen. The ‘t’ can be controlled to be tight and small or apart and big. This is useful for creating crosshairs on screen for shoot them up game, driving game or some other type of game. Or, use it for art.

 

ARC

ARC draws a partial circle, a PIE piece.
ARC has eight parameters:
Start x, start y, radius, angle, end radius, color, pie, fill
Start x and y are center point
Radius is just that
Angle is the angle of the arc
End radius is the end point
Color is 0 or 1 for black or white
Pie is 0 or 1
Fill is 0 or 1 and will fill the arc or leave it open

WP_20140930_22_18_20_Pro

CENTER

CENTER will start PRINTing at the center point of a line

LIST

The LIST statement has been enhanced.  You can now list a single line, five lines or the entire program.

LIST by itself lists the whole program
LIST number- will list the program starting at number and go for five lines
LIST number. will list just that line.

MATH

COSine and SINe have been added.

a=COS( x )
b=SIN( y )
The functions were contributed by reader Jim F – Calgary Alberta Canada , thanks Jim!

OTHER STUFF

The overall interpreter is a bit speedier as there have been some optimizations and I was able to cut a lot of redundant code out. Because the functions added some RAM overhead, I lost about 30 bytes, so usable memory is around 970 or so bytes.  This is plenty for small games, control applications and for learning. It is also enough for great demos too.  If you use this on a Mega, then you will have a lot more memory. Just remember, you will need to change the pins used at the top of the code (for nunchuck, TV OUT, sound and the DHT-11 if you use that.)

To run Tiny Basic on your Arduino, download the package below. Put the Half-Byte Tiny Basic files in your Arduino directory. The TV OUT and DHT-11 libraries need to be imported into your IDE. Please follow the procedure for importing libraries. The Fonts need to go in the the TVOut font directory.  Next, open the Half-Byte Tiny Basic in your IDE and then compile and upload to your Arduino.

DOWNLOAD IT

You can download all files to install Tiny Basic 2 on your Arduino here. Tiny Basic 2  manual is here.

101_3353

Half-Byte Console, now available

We have kits and an assembled and tested unit for sale on our eBay store.

For information on the Programmer’s Kit, click here.

For information on Half-Byte Tiny Basic, click here.

For sample HB Tiny Basic code, click here.

Happy Birthday, BASIC, you wonderful language you!

I remember when I was ten or so years old. I asked my father a question that changed both of our lives. My Dad was an engineer. He was also what we would consider now to be a nerd. A geek.  Above all, though, he was very smart and he was MY Dad.  He could build anything out of what seemed to be random little bits of stuff.  To a ten year old, it was akin to magic.

So, what was that question?

“Dad, what’s a computer and how does it work?”

He didn’t give me a whole answer right away. If I remember, it was something like ‘a machine that can think, sort of.’ ‘Lets find out.’

Weeks later, he started getting packages. Those packages contained the magic wand, magic dust and … paper. Manuals, to be specific.

Out of those packages, my Dad built the Mark IV Mini-Computer, as written about in Radio-Electronics Magazine.  Now, this computer was primitive, even then. Something like 128 bytes of memory, which he expanded to 1K or something like that.  Anyway, that computer didn’t  answer our question.

So, he built another. And another.  That third computer, based on the Signetics 2650 microprocessor, was THE ticket.  This computer, had lots of memory (4K?) and, most importantly, had a keyboard and tv like screen. Oh, this thing called ‘Tiny Basic’.

I was hooked.  I eagerly soaked in all I could about this ‘Tiny Basic.’  I quickly learned that there were LOTS of Tiny Basics and even something just called ‘BASIC.’  So, what was this ‘BASIC’?

Simply put, BASIC was (well, IS) a computer programming language. And, during the 1970’s, it was pretty much the only way someone like myself could interact with the computer. It was easy to learn and use. I was typing in programs from my Dad’s books and magazine, having to alter them to work with the primitive version I had to use.  One day, I decided to start writing my own.  Finally, we had our answer. Collectively, we figured out what these computers were and what one could do with them. My Dad tackled the hardware, I tackled the software and, in the process, laid the foundation for my career.

And, it was all because of BASIC.

BASIC, developed in 1964 at Dartmouth University by John Kemeny and Thomas Kurtz. BASIC was designed for teaching the fundamentals of programming and to make the computer more accessible to non-programmer types.  It slowly grew out of Dartmouth and, by 1970, had spread all over the country.  It gained so much popularity, that it caught the eye and wrath of many professional who held it high in disdain. No matter, the genie was out of the bottle.

In the early 1970’s, when microprocessors became affordable and home computers took off, others were writing dialects of the language to run on these tiny machines.  The most notable being Micro-Soft Basic, co-developed by Bill Gates and Paul Allen and sold by MITS for the Altair computer. Unfortunately, it was priced so high that a black market for the language began, prompting a whiny ‘don’t copy our software’ letter from Bill Gates.  Whiny, but necessary. Software piracy was a huge deal at the time.

Around 1975-76, another dialect of the language came out and this one was affordable. Tiny Basic was a grass roots effort to develop the language so that it would work in very small memory footprints and on pretty much any computer.  Numerous version were released by companies and individuals.  Tom Pittman’s Tiny Basic was probably the most notable, followed by Lawrence Livermore Labs.  Oh, and the one that Steve Wozniak wrote for the Apple ][.

Kemeny and Kurtz hated the versions of their language for microcomputers, especially those of the ‘Tiny’ variety and anything out of Micro-Soft.  See, Micro-Soft, soon to be Microsoft, supplied a version of the language for pretty much all of the big commercial home computers: Apple, Atari, Tandy, Commodore, TI, Mattel and more.  Kemeny and Kurtz wanted to reel it back in with ‘True Basic’. Unfortunately, they discovered it was not that easy to do.

As time went on, however, the language grew and became a powerhouse for professional developers thanks to the efforts of Microsoft.

Microsoft included QBasic in its DOS product. They also had QuickBasic, which could compile code into true executables. For professional development, they had the Professional Development System, sort of a forerunner to todays Visual Studio product, only it was DOS based and character mode.

In the early nineties, they introduced Visual Basic, a Windows programming environment.  Shortly afterwards, Visual Basic for MSDOS was released. I had the opportunity to develop with this version for about two years. It was such a difference from what I was used to: object oriented and event driven. Suddenly, I had to think in terms of USER control of the application and not vice-versa.  It really opened my eyes as to how truly interactive computers could be.  Its overlapping text mode windows, mouse and relational database allowed me to create some really nice front end software for the video rental chain that employed me. It was a treat.

VBDOS didn’t live long.  Time and technology quickly outdated it.  VB for Windows evolved.  VB6 became THE development platform.  VB.NET was the future. Or, so we thought.

Microsoft’s run in with Sun Microsystems over Java led to the creation of C#: a modern programming language that was modeled after java with roots in C.  Interesting combo, but it wasn’t going to go anywhere. Microsoft was just trying to rattle Sun’s chains. That was my thinking. Boy, was I wrong.  Soon, the call for C# was on the rise while VB waned.

While BASIC no longer has the draw or the need, it is still there.  There’s a growing rank of people who are using as a hobby language again.  VBScript and VBA continue to dominate in the scripting arena. You really cannot beat it to do quick, repetitive tasks. It is great for automating mundane things.

And, Tiny Basic has made a come back of sorts. It is used on microcontrollers like the BasicStamp, Arduino and the PIC family of controllers.

While many probably wished it had died fifty years ago on May 1, it is far from death. In fact, this post is a celebration of not only its birthday, but the life it gave to an entire industry and its active future. 

I can’t say how much I owe this terrific language, it is truly immense. My life has been driven by many things, but I’d say BASIC was right up there with my parents and family as the most influential thing in my life.

Happy Birthday, BASIC!