Fun with Arduino and AtTiny!

Lately I’ve been playing with the Arduino microcontroller and I have to say I really like it.  You can find it at Radio Shack for less than $40, and it comes with the board and a USB cable.  There is a free IDE that makes it fast and easy to get started blinking LEDs, no soldering required.

My first (interesting) project was to create laser tag guns.  We went to a laser tag arena about two weeks ago, and I thought this would be an interesting project.  There are several examples of arduino laser tag, from a commercial site to and IBM project walkthrough, but I decided to go cheap and build it myself.

And when I say cheap, I mean CHEAP! I found some paired-down chips that can be programmed with the arduino at the AniBit.com site for a total of 2 bucks each.  Radio Shack has emitter/receiver pairs for $4, and my kids made lego gun cases for free (and they let me help).

I found some small breadboards on Ebay for $1.25 including shipping, and I had some resistors and LEDs laying around for misc. parts.

I’m still learning about programming the Attinys but I’ve got the process down and the parts, so I think I just need some time to put it together.

One of the interesting problems I found is that the Arduino can communicate back and forth to the IDE via the Serial Monitor.  Values on the chip can be sent to the screen, so you can tell what’s going on.

With the AtTiny is that it doesn’t have a connection to the PC, except through the Arduino, so it’s hard to debug what’s going on.  I used a couple of work around to get the serial interface to go from the Tiny to the Arduino to the computer.

1.  Connect the arduino.  Disconnect the capacitor to the reset (if connected).

2.  Upload ArduinoISP

3.  Connect the capacitor to reset, and pins 10-13.

4.  Load the code you want to test on the tiny.  I cobbled together this
#include <SoftwareSerial.h>

SoftwareSerial mySerial(1,0); // RX, TX
const int led = 4;

void setup()
{
mySerial.begin(9600);
pinMode(led, OUTPUT);
}

void loop()
{
while(true)
{
for (int i = 0; i <= 5; i++)
{
mySerial.println (i); //Send i to Rx Arduino
digitalWrite(led, HIGH);
delay(100);
digitalWrite(led, LOW);
delay(100);
}
delay(1000);
}
}

5.  Disconnect the capacitor and pins 10-13.

6.  Change the board to the Arduino Uno

7.  Upload the following code.

/*
Software serial multple serial test

Receives from the hardware serial, sends to software serial.
Receives from software serial, sends to hardware serial.

The circuit:
* RX is digital pin 10 (connect to TX of other device)
* TX is digital pin 11 (connect to RX of other device)

Note:
Not all pins on the Mega and Mega 2560 support change interrupts,
so only the following can be used for RX:
10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69

Not all pins on the Leonardo support change interrupts,
so only the following can be used for RX:
8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).

created back in the mists of time
modified 25 May 2012
by Tom Igoe
based on Mikal Hart’s example

This example code is in the public domain.

*/
#include <SoftwareSerial.h>

SoftwareSerial mySerial(11, 12); // RX, TX
int led = 13;

void setup()
{
delay(5000);
// Open serial communications and wait for port to open:
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

Serial.println(“Goodnight moon!”);

// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
//mySerial.println(“Hello, world?”);
}

void loop() // run over and over
{
if (mySerial.available())
Serial.write(mySerial.read());
if (Serial.available())
mySerial.write(Serial.read());
}

 

8.  Open the serial window (ctrl-shift-M) and watch the communication work!