A Spark of Life: More DigiSpark fun

WP_20151019_22_42_59_Pro__highresI recently wrote about the cool little DigiSpark ATTiny 85 controller board. This little USB wonder, like the AdaFruit Trinket, is based on the ATTiny85 Microcontroller and has limited I/O, memory and is cheap, very cheap.

One thing it is not short on, however, is uses. This little thing can be used for a variety of things, including games and control applications.  For any use, though, you will need some kind of input and some kind of output.  In a previous post, I postulated about porting my ‘Battlestar Galactica’ game to the DigiSpark from the Trinket.  So, here are the results of that endeavor and some other things as well.

For the backstory of the game and why I wrote it, check out this post. I’ll wait.

Read it?  Cool.WP_20151019_22_44_34_Pro__highres

Ok, so there are few differences between the Trinket and the DigiSpark, other than size.  The way you program it is very similar and the pins are nearly identical.  The way you talk to things, is, however, a little different.

The Trinket needed SoftwareSerial. We don’t here, we just use DigitalRead and DigitalWrite and Serial.write to read the button and write to the screen.

The display is a 16×2 Serial LCD from Parallax.  The button is actually a ‘controller’ I built a couple of years ago for my game console. I did not use it for that, opting for the Wii Nunchuck instead.  So, the controller has three buttons that would be used for left, right and ‘fire’ or action.  Here, they just return a 0 if pressed and a 1 when ‘open’.  The code periodically polls the controller for a press and then acts on the press to evaluate a hit or miss.

WP_20151019_22_44_06_Pro__highres

Now, one of the problems I had was the lack of GROUND pins. So, I made a ground bus, a four pin strip with a wire connecting all four pins. I then connected this to ground on the DS and the button and the LCD as well are connected to this bus. I have one free ground pin.  Well, what I did not realize was that you can use any pin for ground.  While I did not change my connections, I find this pretty cool.

TIP! To use a free pin as ground:

(From the wiki)

You can use an I/O pin as a ground/+5V if you have some left and the current that flow in it is low (a few milliamps if you want the pin to stay around 0V/+5V. If you don’t care about the exact voltage you can go up to 40mA per pin according to the datasheet but it’s good to keep a safety margin).

Configure the pin as an output and set its value to either LOW or HIGH (for ground or +5V

#define button 0
#define gnd 2
 
void setup() {                
  pinMode (button, INPUT);
  digitalWrite (button, HIGH); // enable pullup
  pinMode(gnd, OUTPUT);
  digitalWrite (gnd, LOW); // use this pin as a ground
}

For much more information and tips, go to the wiki, located here.

The game code is below.

Come back for more on this cool little device.  Since it is capable of serial i/o, I’m thinking of using the Bluetooth Serial module and connecting the DS to one of my HalfByte Consoles running the graphical serial software for video display from the DS.

WP_20151019_22_45_35_Pro__highresWP_20151019_22_43_27_Pro__highres

Game Code:

#include <TinyPinChange.h>

 // globals
 int  cylonpos;
 int  oldcylonpos;
 int  cyDirection;
 int  button;
 int  posStart;
 int  lin2Start;
 int  bsgpos;
 
 // special characters
 int  bsg;
 int  cylon;
 
 // total number of torpedos the ship has left
 int  tShots=10;
 
 // the setup routine runs once when you press reset:
 void waitforbutton();
 void moveCylon();
 
 void setup() {  // initialize the digital pin as an output.  
   Serial.begin(9600); 
   Serial.write(12);
   Serial.write(17);
   
   //define the ship character
   Serial.write(249);
   Serial.write(0x4);
   Serial.write(0xE);
   Serial.write(0xE);
   Serial.write(0x15);
   Serial.write(0x1F);
   Serial.write(0x15);
   Serial.write(0xE);
   Serial.write(0xE);
   
   // baddies
   Serial.write(251);

   Serial.write(uint8_t(0x0));//to handle a goofy issue with '0'
   Serial.write(uint8_t(0x0));
   Serial.write(0xE);
   Serial.write(0x15);
   Serial.write(0x1F);
   Serial.write(uint8_t(0x0));
   Serial.write(uint8_t(0x0));
   Serial.write(uint8_t(0x0));
 
 // the button is on pin 0  
   pinMode(0,INPUT);
   
 // the initial direction is right
   cyDirection=1;
   
 } // the loop routine runs over and over again forever:
 void loop() {
  int score;
  score=0;
  
  Serial.write(12); // clear the screen and show the instructions
  Serial.println("Shoot the Cylon\n");
  Serial.println("press btn strt");
  
  // wait for the button
  waitforbutton();
  
  // define somethings before we start the game loop
  tShots=10;
  cylon=3;
  bsg=1;
  lin2Start=148; // the baddies appear on line 1
  posStart=128;  // the good guys on line 2
  randomSeed(analogRead(0)); // gen a random seed
  
  bsgpos = 8;  // our ship starts in the middle of the sector
  cylonpos=random(0,15); // baddies warp in at a random spot
  oldcylonpos=cylonpos;
  
  Serial.write(12);  // clear screen
  Serial.write(17);  // turn on backlight so we can see
  Serial.write(posStart + cylonpos);  // uh oh...the Cylon appeared!
 
  Serial.write(cylon);
  Serial.write(lin2Start+bsgpos);
  Serial.write(bsg); // and here we are
  
  // begin the game 
  while(1){
    /* Game loop */
    int button;
    Serial.write(22); // kill the cursor
    moveCylon();
    delay(200);
    button=digitalRead(0);  // check the button
    
    if(button==0){ // torpedo fired?
      if(cylonpos==bsgpos){ // Hit?
        Serial.write(132); // center 'Hit' on screen
        Serial.print("** HIT **");
        score ++;  // update score
        Serial.write(213); // noise (C at half note, 3rd scale)
        Serial.write(215); // play the note
        Serial.write(224);
        Serial.write(225);
        if(score==10){  // did we win?
           Serial.write(12); // clear screen
           Serial.write(128);// start at beginning
           Serial.print("You have saved\nthe Galactica.");
           delay(2000);
           loop();  // start game again
        }
        delay(3000); // did not win yet
        Serial.write(12); // clear screen
        Serial.write(lin2Start); // move cursor to beginning of line
        Serial.write("Score:");  // put up score
        Serial.print(score);
        Serial.write(lin2Start+bsgpos); // Since we shot the Cylon, we have to leave the sector, so...
        Serial.write(0x20); // go into hyperdrive and erase the BSG
        bsgpos=random(9,15);  // calculate new position
        Serial.write(lin2Start+bsgpos); //...and, we are here
        Serial.write(bsg);
        Serial.write(posStart+cylonpos);
        Serial.write(0x20);
        cylonpos=random(0,15); // Damn, they found us!
        oldcylonpos=cylonpos;
        moveCylon();
      } 
      
      tShots --; // decrement our torpedo inventory
      if(tShots<1){ // out of ammo?
        Serial.write(12);  // clear screen
        Serial.write(posStart); // start at beginning of line
        Serial.print("The Cylons win.\n");
        int count=1;
        // show animation of our ship exploding
        while (count<10){
          Serial.write(lin2Start+bsgpos);
          Serial.write(bsg);
          delay(500);
          Serial.write(lin2Start+bsgpos);
          Serial.write('*');
          delay(500);
          count++;
        }        
        loop(); // Play again
      }
    }  
  }
}
void moveCylon(){
  // sneaky Cylons...they show up anywhere!
  Serial.write(posStart+oldcylonpos);
  Serial.write(0x20);
  if (random(0,6)>3){
      //Cylon warped
      cylonpos=random(0,15);
      oldcylonpos=cylonpos;
  }
  
  cylonpos += cyDirection; // figure out which way to move
  if (cylonpos>15){
   cylonpos=15;
   cyDirection=-1;
  }
  
  if (cylonpos<0){
    cylonpos=0;
    cyDirection=1;
  }
  // erase from old spot
  Serial.write(posStart+oldcylonpos);
  Serial.write(0x20);
  Serial.write(posStart+cylonpos);
  Serial.write(cylon);
  oldcylonpos=cylonpos;
} 

void waitforbutton(){
   
  int button;
  button=digitalRead(0);
  while(button==1){     //wait for button press
    button=digitalRead(0);
    delay(1000);
  }  
  Serial.write(1);  
  delay(1000);
  Serial.write(12); 
}

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!

Developing a handheld console with Arduino

IMG_3163Since my first exposure to computers and video games, I have wanted to design and build my own. Lacking, however, the resources and knowledge, I thought it would just be a pipedream. Well, fortunately for me, that is not the case.  The technology is to the point where just about anyone can do so and for just a little money and the internet.

Microcontrollers are the key and companies like SparkFun and AdaFruit can supply the bits. If you are really tight on funds or just cheap (like me) you can save a bit by shopping eBay. 

So, what can these microcontrollers do for you? Well, anything you want. And, yes, they can play games.

My original plan was to use something like AdaFruit’s Trinket for the brains and the Nokia 5110 LCD for the screen. Well, the Trinket is just too limited (and, frankly, difficult to program.)  During a lengthy eBay search, I found a bunch of Arduino UNO clones that fit in about the same space as the Trinket, only with more I/O and more memory. I ordered a few of them and then waited almost a month to get them. In the mean time, I used my UNO to start writing code.

IMG_3182Along the way, I discovered the Arduino’s were capable of video, so I spent a few weeks playing with that and have built a game console that I will revisit once my plate is clear. Adafruit and other vendors sell interfaces for the Wii Classic Controller and the Nunchuk…both of which are very easy to code.

Back to the handheld. The Trinket filled in, physically, for my board layout. When the Mini Pro finally arrived, I socketed it and began to wire it up. A strip for the 5110, connector for the speaker and daughter board for the audio amplifier. I wrote up a short demo that married the audio to the graphics library and produced a static Mario splash screen and beginning of the game.  It plays the Super Mario Brothers 2 theme upon startup.1398742_562906223779137_852686949_o

My next tasks include a controller (a matrix of four buttons: Left, Right, Shoot, Start/Stop) and a case. The actual game code will be last. I want the hardware nailed down before I do all of the code.

The system will run off of three 1.5 volt AA batteries. I wanted to use a rechargeable battery, but the added complexity is more than I want to deal with for now.

1425448_569760583093701_1031881671_oThe case is my biggest headache. I was going to use Lego’s, but the cost is more than I want to pay. I have an old Mattel JuiceBox that I will attempt to use. It looks big enough and already contains a battery compartment, holes for the controls and a speaker bay. It’s just really ugly.

I am also building version two of the handheld concurrently. This one will be for myself (the other is for my step son) so it doesn’t need to be as friendly and compact.  At some future point, I may take lessons learned from both and make a third version.  My version will also output video and has a PS/2 keyboard connector.  I have a version of Tiny Basic that will work with this hardware and this will be my portable computer and game console.1267690_572617716141321_1112089022_o

I have, so far, been pretty successful with my creations, even though I have a very rudimentary knowledge of electronics. I know what resistors and capacitors do and an understanding of transistors. More importantly, however, I can read a schematic and everything I’ve done has come from schematics found on the internet. In some cases, I’ve only borrowed parts of the schematic, in other cases, I used a basic schematic from a datasheet (talk about boring) and have put this stuff together on my own.  AdaFruit and SparkFun have terrific user forums and they have been a big help as well.  But, there is one site in particular: Arduino-Info. This site has code, schematics and really easy to understand explanation about each project. It covers basic things like LCD’s, keypad’s, servo motors and basic Arduino. It explains many electronics concepts and has lots of examples. It has been invaluable.

The Nokia 5110 LCD is a low cost and easy to code panel. It is low res, 84 pixels wide by 48 down. It is challenging (I have an even better appreciation for developers in the ‘70s and early ‘80s) to come up with graphics that look good in such low res. My Mario character looks more like Mega Man. It is a great choice, however, because it has a low memory footprint: 504 bytes for the frame buffer. This still gives about 1.4K to work with, a gold mine.  You can get them from AdaFruit or Sparkfun for under ten dollars or on eBay from $1.88 to about $3.00.

That brings me to parts buying. If you buy them on eBay, it will likely be from an overseas vendor.  I ordered parts from several Chinese vendors and, in all but one case, I got the items within two weeks and free shipping. I would highly recommend getting them from North American vendors. If you look, you can get pricing that is close to the Chinese vendors and still get free shipping. I got my Mega and one of my UNO’s from vendors located in North America. I had those items in just a few days, so it was worth the extra dollar or two.  I also buy from AdaFruit, SparkFun and Jameco. Some things, like the UNO and Mega, cost two to three times the eBay price. I paid ten bucks for the UNO clone and AdaFruit wants $23 and JameCo had one for nearly fifty. Radio Shack sells the UNO for $34.00. As for the Chinese vendors, I bought a bunch of small parts: joysticks, Mini Pro’s, keypads, LCD panels, sensors, etc.  The smallest order was $1.88 and the biggest was $16.  In all but one case, I got the parts in about two weeks, one took a month and one never arrived, but they did issue a rebate and an apology. I would buy from them again, That was the $1.88 order.

I will post more about my project at a later date

Follow Half-Byte on Facebook for more frequent updates on my projects and other tech news.

A Video Game on my Trinket, take 2

Several posts ago, I wrote about writing a video game for the Trinket from Adafruit. Well, since then, I have added scoring, sound and a bit more animation (very crude, mind you, I can only do so much in the small memory and limited screen.)

The setup uses the Trinket, Parallax’s 2×16 LCD panel and a switch connected to Pin 0. The LCD panel is connected to Pin 2.  You will need to import the SoftwareSerial library for the LCD.

The premise of the game is based on the Battlestar Galactica TV show from 2003-2010.  The Galactica was engaged in heavy battle and barely managed to escape before certain destruction. All of her gun turrets, save the forward torpedo launcher, are damaged and non-functional, meaning she can only fight head on.  The Viper squads are still engaging the enemy.  A group of Cylon raiders have found the mighty Battlestar and are taking her on, one by one. Oh, you only have ten torpedo’s left, so use them wisely.

The game ends when you use up your ten torpedo’s.

Below is the revised listing for the game.  Apologies to Glen Larsen.

 

Listing 1.

#include <SoftwareSerial.h>

SoftwareSerial s(0,2);
 
// globals
int  cylonpos;
int  oldcylonpos;
int  cyDirection;
int  button;
int  posStart;
int  lin2Start;
int  bsgpos;
 
// special characters
int  bsg;
int  cylon;
 
// total number of torpedos the ship has left
int  tShots=10;
 
// the setup routine runs once when you press reset:
void waitforbutton();
void moveCylon();
 
void setup() {  // initialize the digital pin as an output. 
   s.begin(9600);
   s.write(12);
   s.write(17);
  
   //define the ship character
   s.write(249);
   s.write(0x4);
   s.write(0xE);
   s.write(0xE);
   s.write(0x15);
   s.write(0x1F);
   s.write(0x15);
   s.write(0xE);
   s.write(0xE);
  
   // baddies
   s.write(251);

   s.write(uint8_t(0x0));//to handle a goofy issue with ‘0’
   s.write(uint8_t(0x0));
   s.write(0xE);
   s.write(0x15);
   s.write(0x1F);
   s.write(uint8_t(0x0));
   s.write(uint8_t(0x0));
   s.write(uint8_t(0x0));
 
// the button is on pin 0 
   pinMode(0,INPUT);
  
// the initial direction is right
   cyDirection=1;
  
} // the loop routine runs over and over again forever:
void loop() {
  int score;
  score=0;
 
  s.write(12); // clear the screen and show the instructions
  s.println("Shoot the Cylon\n");
  s.println("press btn 2 strt");
 
  // wait for the button
  waitforbutton();
 
  // define somethings before we start the game loop
  tShots=10;
  cylon=3;
  bsg=1;
  lin2Start=148; // the baddies appear on line 1
  posStart=128;  // the good guys on line 2
  randomSeed(analogRead(0)); // gen a random seed
 
  bsgpos = 8;  // our ship starts in the middle of the sector
  cylonpos=random(0,15); // baddies warp in at a random spot
  oldcylonpos=cylonpos;
 
  s.write(12);  // clear screen
  s.write(17);  // turn on backlight so we can see
  s.write(posStart + cylonpos);  // uh oh…the Cylon appeared!
 
  s.write(cylon);
  s.write(lin2Start+bsgpos);
  s.write(bsg); // and here we are
 
  // begin the game
  while(1){
    /* Game loop */
    int button;
    s.write(22); // kill the cursor
    moveCylon();
    delay(200);
    button=digitalRead(0);  // check the button
    if(button==HIGH){ // torpedo fired?
      if(cylonpos==bsgpos){ // Hit?
        s.write(132); // center ‘Hit’ on screen
        s.print("** HIT **");
        score ++;  // update score
        s.write(213); // noise (C at half note, 3rd scale)
        s.write(215); // play the note
        s.write(224);
        s.write(225);
        if(score==10){  // did we win?
           s.write(12); // clear screen
           s.write(128);// start at beginning
           s.print("You have saved\nthe Galactica.");
           delay(2000);
           loop();  // start game again
        }
        delay(3000); // did not win yet
        s.write(12); // clear screen
        s.write(lin2Start); // move cursor to beginning of line
        s.write("Score:");  // put up score
        s.print(score);
        s.write(lin2Start+bsgpos); // Since we shot the Cylon, we have to leave the sector, so…
        s.write(0x20); // go into hyperdrive and erase the BSG
        bsgpos=random(9,15);  // calculate new position
        s.write(lin2Start+bsgpos); //…and, we are here
        s.write(bsg);
        s.write(posStart+cylonpos);
        s.write(0x20);
        cylonpos=random(0,15); // Damn, they found us!
        oldcylonpos=cylonpos;
        moveCylon();
      }
     
      tShots –; // decrement our torpedo inventory
      if(tShots<1){ // out of ammo?
        s.write(12);  // clear screen
        s.write(posStart); // start at beginning of line
        s.print("The Cylons win.\n");
        int count=1;
        // show animation of our ship exploding
        while (count<10){
          s.write(lin2Start+bsgpos);
          s.write(bsg);
          delay(500);
          s.write(lin2Start+bsgpos);
          s.write(‘*’);
          delay(500);
          count++;
        }       
        loop(); // Play again
      }
    } 
  }
}
void moveCylon(){
  // sneaky Cylons…they show up anywhere!
  s.write(posStart+oldcylonpos);
  s.write(0x20);
  if (random(0,6)>3){
      //Cylon warped
      cylonpos=random(0,15);
      oldcylonpos=cylonpos;
  }
 
  cylonpos += cyDirection; // figure out which way to move
  if (cylonpos>15){
   cylonpos=15;
   cyDirection=-1;
  }
 
  if (cylonpos<0){
    cylonpos=0;
    cyDirection=1;
  }
  // erase from old spot
  s.write(posStart+oldcylonpos);
  s.write(0x20);
  s.write(posStart+cylonpos);
  s.write(cylon);
  oldcylonpos=cylonpos;
}

void waitforbutton(){
  SoftwareSerial s(0,1);
 
  int button;
  button=digitalRead(0);
  while(button==LOW){
    button=digitalRead(0);
    delay(1);
  } 
  s.write(1); 
  delay(1000);
  s.write(12);
}

The Trinket: small, versatile and cute

IMG_2603I’ve become a fan of microcontrollers. These little marvels can do a lot: control motors, lights, LED’s,LCD’s, servo mechanisms, keyboards, mice…you name it, it probably has one of these things in it.  Some of them, such as the PIC24, are as powerful as yesterday’s microprocessor and can even run real operating systems or video games (the PIC24 is the heart of the XGS PIC 16 bit board I’ve been porting BASIC-the same BASIC will also run on the Arduino line of microcontroller based development boards.)

Adafruit Industries is one of the leaders in the burgeoning hobby that has sprung up around IMG_2631these little beasts.  They recently introduced the Trinket. The Trinket, based on the ATtiny85 controller, is about an inch long by half inch wide (approx.) and contains a sole USB connection and five I/O lines.

The Trinket is easy to use and program. Using Arduino IDE (free from the Arduino web site) you write using C++ and then upload the resulting code to the device. This is the only thing about the device that I have issue with: You have a very short time in which the device is programmable. The IDE is rather slow, and the interval with which the device will program is about ten seconds. You have to repeatedly press the reset button on the device and hope that you have time to program it, otherwise, you have to start over and the IDE does not appear to be smart enough to know that the source has already been compiled. 

IMG_2637The upload process aside, everything else is rather straight forward. There are a number of guides on the Adafruit site to explain how to set to set up the IDE.  Basically, download and install the IDE, then USBTiny, the driver, then there are two config files you need to download and copy into directories under Arduino IDE and in the Documents directory. Complete instructions are here. I suggest you read through the entire article and then do the install.

Once installed, you would code as if it were an Arduino.  Keep in mind, there is no Serial port, but there is a library that ‘maps’ one to one of the I/O pins. It works, great. I used it to talk to one of the Parallax 2×16 LCD panels I picked up from Radio Shack. See my source code snippet below for more.

One of the really nice features is that you can power this thing using three 1.5 volt AAA batteries. Input voltage can be from 3.3 to 16 volts.  The device comes in 3v and 5v flavors, meaning you have to be careful about what you connect to the device. I got two 3v boards as the 5v boards were sold out. I tried with both a 9v battery and three 1.5 volt AAA cells. Of course, you can also power it from the USB connections. And anything connected will, likely, need its own power source.

At $7.95, you cannot really go wrong.  Check out Adafruit’s site for project ideas. I have a couple in mind, including that Star Trek game I wrote of, several posts ago.  In a future post, I will share what I have done with these as well as how my homebrew PIC16 project turns out. And look for more on that Tiny Basic port to the XGS.

#include <SoftwareSerial.h>

SoftwareSerial s(0,1); //receive on "0", and transmit on "1" aka "PB1" aka pin 6

void setup() {   s.begin(9600);// set baud rate to 9600 baud   }    void loop() {   s.write(12);// clear screen   s.write(17);// turn on backlight   s.println("Adafruit Trinket");   s.println("2x16LCD Parallax");      }