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

Build your own ATtiny85 programmer using an UNO

attiny85programmerAs part of my ATTiny 85 learning adventure, I had to create some way to actually load code on to the 85 itself. My first go ’round was a breadboard monster. The thing I hate about bread boarding are the bloody wires.  What a mess. Once I got my game working, I set out for a better way to program the chip.  I could have bought one, but what’s the fun in that? As it turns out, it is simple to build, provided you have an Arduino UNO handy.

I decided to make a shield for the UNO. I wasn’t concerned with passing through all of the pins, so only the ones I needed are exposed.  This is something I won’t do often, so I made no attempt to pretty it up either.  The whole thing consists of an 8 pin socket, one six pin header and one ten pin header and a 10 uf capacitor. Oh, a small perf board to mount it all.

Wiring is tedious and made the same mistake as I did with the game (see my last post) as I got the four pins (5 to 8) reversed. I know, I know.

Mistake aside, it took about a half hour for me to wire it up.

One thing that was a bit problematic for me, as I have a vision impairment, was getting the pins lined up to the proper UNO pins for the headers.  That took a few tries, but I got it.

The connections are below for using an UNO:

UNO Pins

ATTiny 85 Pins (actual pin)

+5v Vcc (8)
Gnd Gnd (4)
Pin 13 pb2 (7)
Pin 12 pb1 (6)
Pin 11 pb0 (5)
Pin 10 reset pb5 (1)

In the table above, the left is the Uno, the right is the ATTiny 85 socket. You need a 10uf cap between the Uno gnd and reset.

Before you can do anything with the Arduino IDE and the 85, you must first install the support…

By default Arduino IDE doesn’t support ATtiny85 so we should add ATtiny boards to Arduino IDE. Open File -> Preferences and in the Additional Boards Manager URLs give this url Arduino IDE Attiny support. Then, open Tools -> Board -> Board Manager, scroll down the list where it says “attiny by Davis A. Mellis”. Click toinstall it. Once installed, you can select ATtiny as the board type. You will also have to select chip type (45,85,etc.) Along with the processor, you will also need to specify the speed.  1 Mhz is the default, I used 8 Mhz for the code I downloaded for the game. Adjust to suit your needs.

You also need to upload the ‘ArduinoISP’ sketch to the UNO before programming the ATTiny 85. Once you upload this sketch, the UNO will pass along to the 85 what ever you send.

For my little handheld, I had to burn the bootloader first, then upload the game sketch. Don’t forget to  set the Arduino IDE to ‘Programmer ‘Arduino as ISP’. Once the bootloader is burned to ATtiny, you are ready to upload your code.

You should be good to go.  Here is a site (from Arduino) that goes into far greater detail.

An ATTiny85 based handheld game

WP_20161228_21_29_19_Pro (2)Yes, I love gaming.  And there is nothing more satisfying, to me, than building, sometimes coding and playing something I made.  Now, I don’t always WRITE the code, after all, time is a premium these days, but I don’t mind taking something someone else did and making it work with what I built.  For this project, I was very lazy: the design is also someone else’s.  I really wanted to do something with the ATTiny85, but have not really done anything outside of playing with the Adafruit Trinket or Digispark.

So, for this little project, I wanted to also use one of my cool little ssd1306 OLED screens.  While perusing the net, I came across Webboggles.com.  Here, they are selling a nifty little kit called the ATTiny Arcade Keychain. It looks to be of high quality and the author (Ilya Titov) goes through much detail in the design and build.  There are several posts about it and the games.  The game code and schematic have been made readily available. The first of the games was breakout and that is where I started. 

To build the little game, you will need the following:

  • Attiny85 + dip8 socket
  • SSD1306 OLED screen
  • 3x push buttons
  • 2x resistors (10kOhm optimal)
  • Piezo speaker
  • 3V 2032 coin cell battery
  • perf or vero board
  • I used a little speaker out of a toy cell phone instead of the piezo. I would also recommend socketing the screen instead of soldering it directly, you don’t have to, but I wish I had now.attiny85game_schem

    One other thing to keep in mind, you will need a way to program the ATtiny 85 chip, which I will describe in a follow up post. I actually built two programmers: one on breadboard and a quasi shield for the UNO.  I like that better.

    As you can see from the schematic, it is really simple. Even so, I made a few mistakes at first.  Not paying attention to the chip pinout, I got the pins reversed from pin 8 to pin 5. I, for whatever the reason, assumed the actual pin 8 was pin 5, instead of going from pin 4 to pin 5 at the bottom of the chip. Once I figured that out (I had yet to apply power) the rest was easy. I also got SCL and SDA backward (hey, I’m old).  Once I got my mistakes corrected, I was amazed that this simple circuit was now a little game machine.  Now, you aren’t going to play Call of Duty or even Doom, but you can play many classics on the devices.  I am going to build one or two more as this was a blast. I would also encourage ordering a kit from Webboggles as well.

    My next post will discuss creating an Attiny 85 programmer for the UNO.

    WP_20161231_15_20_15_Pro (2)

    A Programming Language for a Portable Development System

    Prototyping with Arduino and compatibles is fairly easy, especially when it comes to the hardware.  A breadboard simplifies things quite a bit.  A few months ago, I realized that I did not have any, so I purchased one, in a kit, on Amazon from a company called Elagoo. The kit, for about sixteen dollars (US) contained a lot of parts and the breadboard. Well, the board is fairly small, so I decided to create a portable workspace and mount the breadboard, an Arduino UNO R3 clone, a 2 x 16 LCD and some cord organizers.  It works great, and I can take my project around. Nice.  Problem, though, is that I still need to be tethered to the computer in order to write code.

    WP_20161001_18_38_08_Pro_LI (2)This got me thinking…could I come up with a small but easy to use interface language that could be coded with nothing more than a 12 key keypad?

    The answer is yes.  So, I have come up with an initial set of opcodes for programming with nothing more than what is on my workspace. 

    This language would more resemble CHIP-8 than, say, the Arduino language.  Commands, statements and functions all use a single byte but can have one or more subsequent values for parameters.

    The tables below outline the main features. The keypad I am using (because it was less than a buck) does not have enough keys for full hexadecimal, so I had to improvise. Still working on a scheme to allow alphanumeric entry without connecting a full ASCII keyboard.  For now, the language will be limited to reading sensors, accepting decimal (though integer only) numbers. No video, serial out to the 2×16 LCD or a Bluetooth module.

    For the tables, the first column is the opcode, second is what the opcode does, third is any parameter( s ) necessary and the last is a description.

    Assignment:

    01

    Let

    Var (00-0F)

    Value (00-FF)

    Conditionals:

    02

    IF

    Var (00-0F)

    01 is equal, 02 is <, 03 is >, 04 is <>

    03

    Jump if true

    Addr (00-FF)

     

    Program Flow:

    04

    Goto

    Addr (00-FF)

    Transfer control to address

    05

    Call

    Addr (00-FF)

    Call a subroutine

    06

    Return

       

    07

    End

     

    Ends program

    Input/Output

    10

    Inkey

    Var (00-0F)

    Gets input from the keyboard

    11

    Out

    Var (00-0F)

    Outputs a value

    12

    Temp

    Var (00-0F)

    Gets a reading from the temperature sensor

    13

    Pinset

    00-FF

    Send a value to pin

    14

    Pinread

    00-FF

    Get a value from pin

    15

    Xfer Pin

    Var (00-0F)

    Transfers value from read pin to variable

    I would envision the interpreter being fairly small, so it may be possible to integrate several libraries for the more popular sensors, like DHT-11 temp sensor and others.

    So, what do you think?  Is this something of interest? Please post your thoughts in the comments below.

    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

    64 bit graphical hand held

    I read a post on Hackaday about a ‘64 bit graphics’ hand held game system powered by the atmel 328…an Arduino Uno like handheld.  Well, the headline was a bit deceptive, the 64 bit part was correct, as there is an 8×8 LED matrix being use for the ‘screen’. 

    WP_20160702_03_18_57_Pro (2)This is actually something I had toyed with building for quite a while now.  I had seen several (Super Pixel Brothers being the first) and, after seeing this particular build, I thought…why not?  I have a lot of those matrices lying about, so I took my last unbuilt matrix (it was a ‘kit’) and used it as the basis of my console.

    I used my previous handheld build as the basis for this one, which was also very similar to the Hackaday build.  I guess there aren’t many different to do this.

    My build has four buttons: Reset, left, right and action.  I deviated from the article’s build and used a battery pack that has three AA batteries.  The unit ran nearly 24 hours on those three batteries.

    While I have the hardware complete, I am working on software. The article’s build has six games: space invaders, pong, racing and three others.

    So, I now have a piece of nice hardware and nothing running on it.  I’m thinking about adapting the Super Pixel Brothers to the handheld or doing a Space Invaders type game.  Not sure yet. Heck, I WP_20160630_21_10_14_Promay just put several pre-programmed messages and use the buttons to select them. 

    What would you do?  Thoughts on software/games?  Leave them in the comments below.

     

    Link to the Hackaday article: http://hackaday.com/2016/06/25/this-arduino-console-has-64-bit-graphics/

    Using a 4 digit, 7 segment LED with Arduino

    WP_20160316_22_25_00_Raw_LIDisplays.  I love them.  Pretty much anything that lights up is cool, but things that can display numbers, letters and other symbols are just awesome and making them work, even better.  One of the coolest things I remember from my childhood are seeing those red, seven segment displays. They became cool (and cheap) and showed up everywhere.  Newspapers and magazines would emulate the look in articles and stories and the ads. Science Fiction movies and tv shows ate them up.  They were everywhere.  Then…sometime in the late eighties…they sort of went away, in favor of LCD and other display tech.  Or, worse…they changed color!  But, for me, its’ those RED ones.

    I just had to make something with those uber cool RED seven segments.  But, what? Well, why not a clock? Yeah, a clock.

    So, I ordered one, four digit, seven segment display bar.  A Catalex.  Wow.

    Well, the thing arrived and it looked cool. But, there was a problem…how the hell do I send it data?

    It uses four pins: GND, VCC, DIO and CLK.

    DIO and CLK can be any digital pins, that wasn’t the issue. My problem was how to do so in CODE.

    A little bit of digging revealed a nice little library that worked great with the display. And, you can download it here.WP_20160316_22_24_45_Raw_LI

    The trickiest part of the library is figuring out which digits go where.  Fortunately, it easy:

    display.showNumberDec(number, show leading zero (true or false), number of digits, position)

    Example:

    display.showNumberDec(now.hour(),true,2,0);
    display.showNumberDec(now.minute(),true,2,2);

    The first line puts the hours in the left most digits and hours on the right most digits, starting at digit 2 (the numbering starts at zero) and with leading zeros, so 8:03 would display as 08 03.

    The one thing I have not yet figured out is how to display the colon. It is there, but I have yet to turn the bloody thing on.

    This is really nifty little display and was cheap…about two US dollars if you shop. I did not and paid nearly four bucks, but, it did not matter, I got it in a day, thanks to Amazon Prime.

    Below is my early attempt at a clock.

    // Date and time functions using a DS1307 RTC connected via I2C and Wire lib
    ‪#‎include‬ <Wire.h>
    #include “RTClib.h”
    #include <Arduino.h>
    #include <TM1637Display.h>

    RTC_DS1307 rtc;

    // Module connection pins (Digital Pins)
    ‪#‎define‬ CLK 2
    #define DIO 3

    // The amount of time (in milliseconds) between tests
    #define TEST_DELAY 2000

    TM1637Display display(CLK, DIO);

    void setup () {

    if (! rtc.begin()) {
    Serial.println(“Couldn’t find RTC”);
    }

    if (! rtc.isrunning()) {
    Serial.println(“RTC is NOT running!”);
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
    }
    uint8_t data[] = { 0xff, 0xff, 0xff, 0xff };
    display.setBrightness(0x0f);

    // All segments on
    display.setSegments(data);
    }

    void loop()
    {
    DateTime now = rtc.now();

    display.showNumberDec(now.hour(),true,2,0);
    display.showNumberDec(now.minute(),true,2,2);

    delay(5000);

    display.showNumberDec(now.month(),true,2,0);
    display.showNumberDec(now.day(),true,2,2);

    delay(5000);

    }