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

Retro Computer: Arduino Pro Mini stuffed in a keyboard

WP_20150325_16_25_02_ProSo, during the Half-Byte Console project, I purchased quite a few PS/2 style PC keyboards.  Since the project is pretty much over, I have a few of these keyboards still. They are actually fairly decent keyboards for the price.  Well, I had envisioned a future Half-Byte Computer living in something like a keyboard, like the old Commodore 64 or Vic 20.  So, I set out to build one.

I wanted to use one of my Half-Byte Console boards, alas, though, it is too big. Since I ordered a truckload of Arduino Mini Pro’s, and they are really small, I used one of them.

The first thing I had to do was figure out what I wanted in this thing and where to put the parts.WP_20150325_15_44_57_Pro

I grabbed one of the keyboards and took it apart. It is really simple, a standard membrane matrix with full travel keys.  However, even though it is a full size keyboard. there isn’t much room inside.  However, there’s enough for the mini pro, wires and not much else.  I had to cut holes for the power connector and video out.

WP_20150325_15_44_49_ProWhat I was not going to do this iteration was include sound, nunchuck or FTDI connectors.  That can all come later. I’m putting HB Tiny Basic  on the this one, so I won’t need to be programming it often. The nunchuck will be easy enough to add later, as will sound. Just not that industrious at the moment.

I cut the cable from the keyboard as the pc board inside is marked and easily accessible.  I wired it directly to the pro mini: data to pin 5 and clock to pin 4.

I decided to put the video jack on the left side,  but first had to cut a hole for the connector.WP_20150325_16_24_37_Pro  Next, I put the power jack on the bottom, where the keyboard’s cable came out. I had to enlarge that opening and super glue the connector.  Next, I wired up the video out jack and the two resistors (470 ohm and 1kohm) to the pro mini (470 ohm to pin  9 and 1k to pin 7 see my previous postings on the video out subject.)  Then the power was wired up and I tested the whole thing.  Amazingly, it worked.  It took two attempts at laying out the wire and board to get the case to  go back together and for all of the keys to work. 

WP_20150325_16_20_05_ProI now have my true retro computer, complete with Tiny Basic and blocky graphics.  Truly, an awe inspiring device.  Yep. Awe inspiring.

NOTE: yes, the wiring  is a bit sloppy, but I’m no Ben Heckendorn and this was for me.  I will clean it up when I add the other connectors.

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

Hacking the Half-Byte Console and Tiny Basic v2 (or, making Tiny Basic tell me the temperature)

WP_20140826_22_19_55_ProI had one main goal in mind when I designed the Half-Byte Console: to bring together parent and child in a learning experience. Now that the console is a reality and a few are out in the wild, I want it to do other things.  So, I thought ‘what can this do that isn’t expensive and would be easy to add to the Tiny Basic as well?’

Looking around my office, I see a DHT-11 temperature and humidity sensor. Ah ha! These are cheap, just a few dollars each.  They are also easy to access in code and, with only three pins, easy to connect.  So, this is the Half-Byte Console’s first hack: measuring indoor environment.

The DHT-11 has three pins: +5, data and Ground (-).  I chose to use D5 on the console as it is safe to use and won’t interfere with video or the keyboard.  Plus, it is easy to get to on the board. I loaded the example sketch and changed the pin reference to make sure it worked. It did. WP_20140826_22_20_22_Pro

Next, I added support for the sensor to Tiny Basic.  I am working on Version 2 and this support will be part of that release (which should be ready very soon.)

Support comes in the form of two functions:

  • x=Temp(1)
  • x=Humidity (1)

The parameter for Temp actually has meaning: if the parameter is a zero, the temperature is returned as Celsius. If it is a 1, it is returned as Fahrenheit. Any non-zero parameter defaults to Fahrenheit.WP_20140826_22_20_46_Pro

So, now the console can do something useful.  I’m anxious to get the release of Tiny Basic out and see what you all can do with this new functionality.  I am going to post more on the new features of Tiny Basic (hint…more graphics, LIST is fixed…)

In the mean time, if you have any suggestions for Tiny Basic, please let me know in the comments.

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.

Half-Byte Tiny Basic is now available!

101_3346Half-Byte Tiny Basic is now complete. It supports 80×48 graphic resolution, 20 by 8 lines of text using a 4 x 6 bit character set. Audio generation via 16384 tones from a single channel. Graphical support in the form of Line, Box, Set, Reset, Circle and Shift statements and the Get function. 16 bit integer math is supported. Direct hardware access via the DWrite, AWrite statements and ARead and DRead functions. Serial port access via the SPrint and Echo statements as well as the IN function. You have access to all of the I/O pins of the 328p controller chip, the heart of the Arduino UNO and the Half-Byte Console.  The Wii Nunchuck and Classic Controller are supported via the PAD function.  LImited string handling can be accomplished (but there is no native string support.)

WP_20140701_018 (2)Functions include ABS, CHR, RND and INKEY.

Half-Byte Tiny Basic harkens back to the early days of home computing when memory was low and very expensive. Video capability was primitive (as it is, admittedly, here) and mass storage was non-existent. You only have one thousand bytes memory available for your Basic program. Half-Byte Tiny Basic is based on a version of 68000 Tiny Basic written by Mike Field. This version is optimized and specifically developed for on board development using a PS/2 keyboard and the TVOut Arduino library. And it is ideal for teaching the basics of computer programming. It is easy to learn and easy to use.WP_20140701_007

Since the Half-Byte Console is a very simple device, many shortcuts (as in the early days) had to be taken. For instance, memory is at a premium and that is why the resolution is low and the font is cramped. There is no fancy integrated development environment, heck, there isn’t a real editor: you make a mistake on a line, you type it in again. The LIST WP_20140701_027 (2)command is very primitive: LIST will type out the whole program, LIST <line number> begins typing out the program starting at <line number> while LIST <line number>- will type out JUST that line.  You can only SAVE one program as there is currently no mass storage (but, I am working on a better solution, stay tuned!)

Half-Byte Tiny Basic is not perfect, but is works well and is a great tool for teaching. Best of all, you can download it for free.  A short document explaining the commands,statements and functions is included. You get the source code and the documentation all for free.  A nicer book/manual will soon be available for a small cost. The book written so that someone with no experience can pick it up, read and follow it to gain a basic understanding of how to write a program and get it to work. This book will also be part of the Programmer’s kit for the Half-Byte Console.

SAMPLE CODE

Here are a few pieces of sample code:

Kaliedoscope 3

100 CLS
110 X=RND(79)
120 Y=RND(47)
130 P=RND(79)
140 Q=RND(47)
150 SET X,Y
160 SET 79-X,Y
170 SET 79-X,47-Y
180 SET X,47-Y
190 RESET P,Q
200 RESET 79-P,Q
210 RESET 79-P,47-Q
220 RESET P,47-Q
230 Z=RND(100)
240 R=RND(20)
250 IF Z>92 CIRCLE 40,24,R,1
260 IF Z>92 FOR I=1 TO R
270 IF Z>92 CIRCLE 40,24,I,0
280 IF Z>92 NEXT I
290 GOTO 110

Half-Byte Demo

100 CLS
110 FOR K=0 TO 4
120 CURSOR K,3
130 PRINT ” Half-“
140 CURSOR 14-K,3
150 PRINT “Byte “
152 DELAY 1000
160 NEXT K
170 FOR R=1 TO 20
180 CIRCLE 38,21,R,1
184 DELAY 50
186 CIRCLE 38,21,R-1,0
190 NEXT R
195 CIRCLE 38,21,R-1,0
197 DELAY 1000
199 X=7
200 A=67:GOSUB 900
210 A=79:GOSUB 900
220 A=78:GOSUB 900
230 A=83:GOSUB 900
240 A=79:GOSUB 900
250 A=76:GOSUB 900
260 A=69:GOSUB 900
400 GOTO 100
900 FOR Y=0 TO 6
910 CURSOR X,Y
920 PRINT CHR(A);
930 DELAY 250
940 CURSOR X,Y
950 PRINT ” “;
960 NEXT Y
970 CURSOR X,7
980 PRINT CHR(A);
985 X=X+1
990 RETURN

You can download a PDF of the Half-Byte Tiny Basic Guide here.

You can download a ZIP file with the Half-Byte Tiny Basic source, Guide and some example code from here.

You can download the Half-Byte serial terminal for Arduino here.

The serial terminal runs on another Half-Byte Console or Arduino that has the Video and Audio output modifications and runs TVOut. Connect the two devices using TX,RX and GND pins. This allows you to use the serial terminal as an output device, providing a second screen to your console. The terminal provides 128×96 graphics and 22×16 lines of text. The terminal software interprets special code for clear screen, set pixel, reset pixel and box.

PLEASE NOTE:

Half-Byte Tiny Basic Copyright (c) 2014 George Gray

Arduino Tiny Basic Copyright © 2011 Mike Field

TVOut Library Copyright© 2010 Myles Metzer

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be  included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

From design to built: the Half-Byte Console

IMG_4376For years, I yearned to build my own computer and, recently, as I have written here, I did. Only, this time, the computer I built was not someone else’s design, but my own. Now, for some, that’s not a big deal. For me, not being an engineer, it is big. 

Last fall, I had built an Arduino, copying someone’s schematic. It worked and worked well. I got to playing around with Arduino more and more, discovering the video and audio capability and the ability to code for this thing, rather easily, just got me more and more intrigued.

I worked on a port of Tiny Basic, nearly rewriting the wonderful Mike Field Tiny Basic for Arduino. I’ve added a ton of features-graphics, audio (ok, beeps), hardware support…etc. And I’ve optimized it to not only work with a PS/2 Keyboard and video, but it also gives you nearly 1k of RAM to use for your Tiny Basic programs. That may not sound like much, but there is a surprising amount of things you can with ‘only a k’ of RAM.

So, now I have this nifty little computer, that is not only a throwback to the home computers of the 1970’s, it is also fun to build and program.

As I am using the TVOut Arduino library un altered, pretty much any program or game that uses this library and a 328p compatible Arduino will work on the console.

So, how did I design this thing?

hbc1I downloaded the Fritzing schematic/pc board/breadboard designer package. It is free and you can generate industry acceptable ‘gerber’ files that can then be used to create a professional look printed circuit board.

I studied the basic circuitry for a 328p based Arduino compatible microcontroller. Turns out, it’s really simple. With encouragement from my wife, son and friends, I set out to design something and then turn into a product.

I had to decide what was going into the computer, what features. Well, the idea I had was that this be a fun project for parents and children to do together. Mom or Dad do thbackpice soldering and the kids help with inserting the parts and then programming the computer.  Games had to be a priority. So, the Wii Nunchuck became the controller. Unfortunately, only one is supported, due to the way it works.  (I have since discovered a way to allow more than one. That will be in the next version.) Video and sound have to be there. We need to be small and somewhat expandable.

BLACK_6_5_2014 6_03_05 PMMy first prototype looked great. I built it up on a proto board and it worked fine.  My proto, however, didn’t fully replicated my design.  After building it, I made changed to the design, but did not reflect them all on the proto. I realized, later, I should have.

So, I finish up my design, run the rules check and it passes. I study it for several days before sending it off to Fritzing to have one board made. That was expensive…forty dollars for the one board. Wow, there’s no way I can ‘mass’ produce these things at that price.  So, I search and search for a cheap fab house.  That search lasted a couple of months.  In the mean time, the board arrives, I photograph it and then put it together.  I solder the last connector and plug in the 328 chip.  Insert the power and…viola! NOTHING.  Scratching my head, I check my work and try again. Nothing happens.  I study the power circuit. I check voltage. NOTHING.XANDER - WIN_20140326_212243

Hmmm…So, I check polarity of the connector and…BINGO! The pins are backward.  This is where my inexperience with Fritzing comes in.  I misread the symbol. Damn. So, I cut the traces, solder jumper wire and try again.

This time, the Tiny Basic welcome message displays. Success! So, I wire up the keyboard because I realized that I also had the PS/2 connector wrong.  So, after correcting my mistakes with Fritzing, I get down to ‘burning in’ the board.  I run battery life tests (12 hours running constant random graphics, not too bad as it is also generating video) and communications test. Everything working great, even the Wii Nunchuck works fine.  Now, I have to find cheap components and a fab house.

Finding the components was easy, for the most part. No one dealer  had everything.  Weird how PS/2 keyboard connectors and board mounted RCA phone jacks are hard to find now.

101_3326During my experimenting and eBay purchasing, I struck up a relationship with a Chinese vendor.  I inquired about fabbing the boards and they could do it really inexpensively. So, that made this possible.  I finished up revising and cleaning up my design. I checked, rechecked and double checked. It’s going to be perfect.  Off it went to the fab.  I buy parts.  The parts begin showing up. Not too long after the parts started to roll in, but the boards did too.

My, were they pretty.

101_3346I grabbed one of the blank boards and photograph it. Then, as with the first, I build it up. Plug it in and…it worked! First time. Whew!  Finally, it works. Well…not so fast.  Everything worked but the bloody Nunchuck. My heart sinks.  Upon close examination, I see that, while I had put the labels to the connector pins correctly, I neglected to re-arrange the ground and 3.3 volt pins.  Man. I have hundreds of these boards. What the hell am I going to do?

Ah…I’ll correct the issue on the Solarbotics made Nunchucky adaptor board.  So, I fix the problem on the adapter and try. It works!  Awesome. IMG_4386

Now, I have a nice design that works. It has audio, video, game controller (that you can unplug from the main board) and it can talk, via serial, to other devices. 

I have a ton of ideas for follow up accessories, the first of which is the PS/2 keyboard and Tiny Basic programmers kit.  The standard kit is just the console, Nunchuck adapter and a 9volt battery clip. The programmers kit is the standard kit, plus a PS/2 connector, Tiny Basic in the 328, A Learn to Program with Tiny Basic manual and either a PS/2 Keyboard OR a USB to PS/2 keyboard adapter. I am also working on a real time clock, Bluetooth connectivity and an experimenters kit. 

This has been a fun project and one I thought I’d never do.  I’m a programmer,not a hardware engineer. And, anything more complicated will, likely, require a real engineer. But, in the mean time, I have more projects like this. 

IMG_4394The console and Tiny Basic will be licensed as the original works are, I will post that later.  I will post the source code for Tiny Basic, the schematic, parts list and Fritzing files.  I will be selling the blank boards and the two aforementioned kits with printed documentation very soon.

In the mean time, if you want a blank board, I am selling them now.  Go to the Half-Byte eBay store and pick one up.