Jeopardy! style buzzer game for two players with ATtiny85 chip

My latest project is a Jeopardy! style buzzer for two players. Players press the buttons on either side of the board to activate the buzzer and light the respective LED. Once a player has buzzed in, their LED remains lit for approximately five seconds, during which the other player cannot buzz in. Once the five seconds have expired (and the LED turns off), either player is free to buzz in again.

Image

This project is unique from my previous projects because I have replaced the Arduino Uno board with a low cost ATtiny 85 chip. This chip is similar to the ATMega chip that is the brains of the Arduino Uno, but scaled down. The ATtiny has far fewer digital input/outputs, less memory and runs slower. With a price tag of $2.85 this chip is the perfect replacement for the $25 Arduino Uno, assuming that you using 5 or less inputs/outputs. Additionally, the ATtiny can be powered from a small 3.3 volt coin cell battery, as seen in the above photo.

Here are the materials I used:

(1) ATtiny 85 microcontroller chip

(1) Breadboard

(1) Coin cell battery holder for CR2032 battery

(1) CR2032 battery

(2) Mini push button switches, breadboard compatible

(2) 10mm LEDs (I used 1 red and 1 green)

(2) 10k ohm resistors

(2) 330 ohm resistors

(1) buzzer

Various jumper wires

Here’s a link to the complete list of parts at Sparkfun Electronics: https://www.sparkfun.com/wish_lists/44673. They can be had for around $20.

You’ll need to program the ATtiny85 chip with an Arduino board. The instructions for doing so can be found at the MIT High-Low Tech club site, found here: http://hlt.media.mit.edu/?p=1695. Basically, you configure your Arduino to pass code on to the ATtiny chip.

Here’s the code I wrote the project:

// Jeopardy! style, two person buzzer game for ATtiny85 chip.
// by Joel Swenson, joelswenson at me dot com

#define LED1 1 //LED 1 (red) connected to pin 1 on ATtiny
#define LED2 3 //LED 2 (green) connected to pin 3
#define BUTTON1 2 //button #1 red connnected to pin 2
#define BUTTON2 4 //button #2 green connected to pin 8
#define BUZZ 0 // buzzer to pin 0

int val1 = 0; //stored val for #1
int val2 = 0; //stored val for #2

int old_val1 = 0; //previous stored val for #1
int old_val2 = 0; //previous stored val for #2

int state1 = 0; // initial state of button #1
int state2 = 0; // initial state of button #2

// Setting up input/output for LEDs and buttons

void setup()
{
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(BUTTON1, INPUT);
pinMode(BUTTON2, INPUT);
pinMode(BUZZ, OUTPUT);
}

void loop()
//check button 1 and light 1 if pressed
{
val1=digitalRead(BUTTON1); // detect if the button was pressed
if ((val1 == HIGH) && (old_val1 == LOW))
{
state1 = 1 – state1;
delay(10);
}
old_val1 = val1;
if (state1 == 1) // if the button is pressed
{
digitalWrite(BUZZ, HIGH); //activate buzzer
delay(20);
digitalWrite(LED1, HIGH); //light LED
digitalWrite(BUZZ, LOW); //deactivate buzzer
delay(500); // wait 5 seconds to lock out all buttons
digitalWrite(LED1,LOW); // LED off
state1 = 0; // return the button state to unpressed
} else // if button is not pressed. this part might be unnecessary
{
digitalWrite(LED1,LOW); // turn off light
}
//check button 2 and light 2 if pressed
// same code except varibles, button and LED changed for #2
val2=digitalRead(BUTTON2);
if ((val2 == HIGH) && (old_val2 == LOW))
{
state2 = 1 – state2;
delay(10);
}
old_val2 = val2;
if (state2 == 1)
{ digitalWrite(BUZZ, HIGH);
delay(20);
digitalWrite(LED2, HIGH);
digitalWrite(BUZZ, LOW);
delay(500);
digitalWrite(LED2, HIGH);
state2 = 0;
} else
{
digitalWrite(LED2,LOW);
}
}

If you have any specific questions or suggestions about the project please let me know in the comments.

Tagged ,

Twinkle LEDs- Update

In the previous post, I set up nine LEDs to randomly twinkle. While watching my LEDs twinkle, I noticed that one of them wasn’t lighting up. I checked all of the jumper wire connections and they seemed to be fine. After watching them twinkle for a few minutes, I noticed the LED in question did, in fact, work. Instead of waiting patiently, I came up with the following idea to test the LEDs. When the device first turns on, I could have it blink the LEDs in order down the line. Then, you would immediately know if one of them is faulty. Here’s the I came up with:

//Light each LED individually 
 for(int start = 1; start <10; start++) {
 LED_on(start);
 delay(200);
 LED_off(start);
 }

Insert this code at the end of the void setup() section of your code. Now, watch as your LEDs light up one after another. If one doesn’t light up, you know it’s a hardware problem, as opposed to the software just taking it’s time to randomly twinkle that LED.

Let me know if it works for you in the comments or if you have any suggestions. Thanks.

Tagged ,

Twinkle LEDs

Like many of my posts, this isn’t the most practical project. However, the end result is a pretty cool twinkling row of LED lights. I got the idea for the project from this tutorial on Sparkfun (http://www.sparkfun.com/tutorials/312). While the tutorial is for the LilyPad, a sewable micro controller for fabrics, it works fine with an Arduino Uno.

Here’s what you’ll need:

  • 9 LEDs
  • Lots of jumper wires
  • Arduino Uno or similar board
  • Solderless breadboard

And here’s what you’ll do:

You can lay out your breadboard however you choose, but an easy way to do so is to line up the LEDs in a row across the breadboard. For the sake of keeping things organized and tidy, have all the the positive pins of the LEDs (the longer pin) pointing towards the left and the negative towards the right (the shorter pin). Each positive pin on the LEDs will be connected by a jumper wire to a digital input on the Arduino. I’m using digital pins 2 through 10. Each negative pin is connected to the negative rail of the breadboard with one wire on that rail connected to ground on the Arduino. Here a couple of photos to get you started:

It’s a semi-organized mess of wires. There certainly could be a better way of wiring it that would use fewer jumpers, but this gets the job done. Also, I’m completely ignoring the fact that a resistor should be used in each LED circuit. I’m not too concerned about damaging my Arduino. I only plan on running these twinkling lights for a few minutes at a time. If you want LEDs that you could leave twinkling for hours, you’ll have to redesign the circuit to use something like a 330K ohm resistor on the negative end of each LED. So, just be aware that I’m encouraging bad behavior.

On to the software. This I took almost line by line from the SparkFun tutorial. Upload the following code to your Arduino:

// Twinkle code for 9 LEDS
#define LED1 2
#define LED2 3
#define LED3 4
#define LED4 5
#define LED5 6
#define LED6 7
#define LED7 8
#define LED8 9
#define LED9 10
#define interval 11000
#define dead_time 1000
unsigned long PWM_counter = 0;
int offset = 0;
int step_size = 200;
unsigned long on_time = 0;
unsigned long cycle_start = 0;
char dir = 1;
int offset2 = 0;
int step_size2 = 200;
unsigned long on_time2 = 0;
unsigned long cycle_start2 = 0;
char dir2 = 1;
byte LED_tracker1 = 1;
byte LED_tracker2 = 1;
void setup() {
 Serial.begin(57600);
 pinMode(LED1, OUTPUT); 
 pinMode(LED2, OUTPUT); 
 pinMode(LED3, OUTPUT); 
 pinMode(LED4, OUTPUT); 
 pinMode(LED5, OUTPUT); 
 pinMode(LED6, OUTPUT); 
 pinMode(LED7, OUTPUT); 
 pinMode(LED8, OUTPUT); 
 pinMode(LED9, OUTPUT); 
 offset = random(2500,7500);
 offset2 = random(2500,7500);
}
void loop()
{
 PWM_counter = micros();

 if( (PWM_counter + offset - cycle_start) >= interval) { // completed cycle, start over
 on_time += step_size*dir;
 cycle_start = PWM_counter;
 if (on_time >= (interval - dead_time) ) {
 dir *= -1;
 on_time = interval - dead_time;
 }
 else if (on_time <= 0) {
 on_time = 0;
 dir *= -1;
 LED_tracker1 = LED_tracker2;
 while (LED_tracker1 == LED_tracker2) LED_tracker1 = random(1,10);
 offset2 = random(0,4000);
 }
 }
 else if( (PWM_counter + offset - cycle_start) >= (dead_time + on_time) ) { // time to switch LED off
 //digitalWrite(LED1, LOW);
 LED_off(LED_tracker1);
 }
 else if( (PWM_counter + offset - cycle_start) >= dead_time) { // time to switch LED on
 //digitalWrite(LED1, HIGH);
 LED_on(LED_tracker1);
 } 

 if( (PWM_counter + offset2 - cycle_start2) >= interval) { // completed cycle, start over
 on_time2 += step_size2*dir2;
 cycle_start2 = PWM_counter;
 if (on_time2 >= (interval - dead_time) ) {
 dir2 *= -1;
 on_time2 = interval - dead_time;
 }
 else if (on_time2 <= 0) {
 on_time2 = 0;
 dir2 *= -1;
 LED_tracker2 = LED_tracker1;
 while (LED_tracker2 == LED_tracker1) LED_tracker2 = random(1,10);
 offset2 = random(0,4000);
 }
 }
 else if( (PWM_counter + offset2 - cycle_start2) >= (dead_time + on_time2) ) { // time to switch LED off
 //digitalWrite(LED1, LOW);
 LED_off(LED_tracker2);
 }
 else if( (PWM_counter + offset2 - cycle_start2) >= dead_time) { // time to switch LED on
 //digitalWrite(LED1, HIGH);
 LED_on(LED_tracker2);
 }
}
void LED_on(byte LED)
{
 if (LED == 1) digitalWrite(LED1, HIGH);
 if (LED == 2) digitalWrite(LED2, HIGH);
 if (LED == 3) digitalWrite(LED3, HIGH);
 if (LED == 4) digitalWrite(LED4, HIGH);
 if (LED == 5) digitalWrite(LED5, HIGH);
 if (LED == 6) digitalWrite(LED6, HIGH);
 if (LED == 7) digitalWrite(LED7, HIGH);
 if (LED == 8) digitalWrite(LED8, HIGH);
 if (LED == 9) digitalWrite(LED9, HIGH);
}
void LED_off(byte LED)
{
 if (LED == 1) digitalWrite(LED1, LOW);
 if (LED == 2) digitalWrite(LED2, LOW);
 if (LED == 3) digitalWrite(LED3, LOW);
 if (LED == 4) digitalWrite(LED4, LOW);
 if (LED == 5) digitalWrite(LED5, LOW);
 if (LED == 6) digitalWrite(LED6, LOW);
 if (LED == 7) digitalWrite(LED7, LOW);
 if (LED == 8) digitalWrite(LED8, LOW);
 if (LED == 9) digitalWrite(LED9, LOW);
}

That’s it. Afterward you should see all nine LEDs turning off and on randomly for a twinkle effect. Once again, thanks to Dia on SparkFun tutorials for the code and inspiration. Please let me know in the comments if you could think of a practical project that could incorporate these lights, or if you built it and simply enjoy staring into them. Actually, just tell me anything in the comments. I’d like to know that someone else is reading this and may actually find it useful.

Here’s a photo of the LEDs twinkling:

Tagged ,

Garage Door Status Server Prototype

Do you ever wonder if you remembered to close the garage door? With an Arduino micro controller board, an ethernet shield, network connection and a few basic components, you can make a simple web server that detects the status of your garage door.

There are many home automation projects across the internet where an Arduino is used to open and close a garage door. Today, I’m just interested in the most basic part of garage door control: determining whether or not the garage door is open or closed. We’ll leave controlling the garage door remotely for another time. Also, I’m not ready to start running wires in the garage quite yet, so we’ll just build a table top prototype.

Here’s what you’ll need:

  • Arduino Uno or similar board
  • Arduino Ethernet Shield
  • Solderless breadboard
  • One LED
  • One momentary push button
  • Two 320 OHM resistors
  • A few jumper wires
  • ethernet cable
  • USB cable

Step 1: Building the Circuit

The circuit for our garage door status server prototype is simple. Coming off the the Arduino and Ethernet Shield, there is a circuit for the  LED and a circuit for the momentary push button. These share the negative rail for ground connections. Here’s a photo:

Here are the connections:

  • Ground on the Arduino is connected with a jumper to negative rail of the breadboard
  • Pin 3 on the Arduino is connected with a jumper to the positive end of the LED
  • 320 Ohm resistor connects negative end of the LED to negative rail of the breadboard
  • 5 volt pin on Arduino is connected with a jumper to one end of the momentary push button switch
  • Pin 2 on the Arduino is connected with a jumper to other end of the momentary push button switch
  • 320 Ohm resistor connects the other end of the momentary push button to the negative rail

Step 2: Programming the software

First, copy and paste the following code into a new Arduino sketch:
/*
 Garage Door Protype

 Arduino Uno + Ethernet Shield
 Button or switch on pin 2
 LED on 3
 When button is pressed, LED lights, web server reports door closed

 Based on examples from Arduino.cc
 joesgizmos.wordpress.com
 */
// Libraries to be included
#include <SPI.h>
#include <Ethernet.h>
// pins for LED and Switch
const int LED = 3;
const int SWITCH = 2;
int state = 0; // state of switch, high = closed, low = open
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192,168,1, 177); // Static IP needed for webserver
// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
 // Setup input & output pins
 pinMode (LED, OUTPUT);
 pinMode (SWITCH,INPUT);

 // Open serial communications and wait for port to open:
 Serial.begin(9600);
 while (!Serial) {
 ; // wait for serial port to connect. Needed for Leonardo only
 }
// start the Ethernet connection and the server:
 Ethernet.begin(mac, ip);
 server.begin();
 Serial.print("server is at ");
 Serial.println(Ethernet.localIP());
}

void loop() {
 // listen for incoming clients
 EthernetClient client = server.available();
 if (client) {
 Serial.println("new client");
 // an http request ends with a blank line
 boolean currentLineIsBlank = true;
 while (client.connected()) {
 if (client.available()) {
 char c = client.read();
 Serial.write(c);
 // if you've gotten to the end of the line (received a newline
 // character) and the line is blank, the http request has ended,
 // so you can send a reply
 if (c == 'n' && currentLineIsBlank) {
 // send a standard http response header
 client.println("HTTP/1.1 200 OK");
 client.println("Content-Type: text/html");
 client.println("Connnection: close");
 client.println();
 client.println("<!DOCTYPE HTML>");
 client.println("<html>");
 // add a meta refresh tag, so the browser pulls again every 5 seconds:
 client.println("<meta http-equiv="refresh" content="5">");

 // Web info goes here
 client.print("The garage door is ");
 if (state == HIGH){
 client.print ("closed");
 }
 else {
 client.print("open");
 }
 client.println("</html>");
 break;
 }
 if (c == 'n') {
 // you're starting a new line
 currentLineIsBlank = true;
 } 
 else if (c != 'r') {
 // you've gotten a character on the current line
 currentLineIsBlank = false;}
 }
 }
 // give the web browser time to receive the data
 delay(1);
 // close the connection:
 client.stop();
 Serial.println("client disonnected");
 }
 // Check state of switch
 state = digitalRead(SWITCH);
 if (state == HIGH) { 
 // turn on LED
 digitalWrite (LED, HIGH);
 }
 else { // turn off
 digitalWrite(LED, LOW);
 }

}
Next, find the following section of code:
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192,168,1, 177); // Static IP needed for web server

For the line beginning with byte mac[] =, put the Ethernet MAC address of your ethernet shield between the curly brackets {}. The MAC address is often found on a sticker on the box or actual ethernet shield. It’s okay to use the default MAC address, if no other device on your network has that exact address. For more about MAC addresses, see: http://en.wikipedia.org/wiki/MAC_address.

For the line beginning with IPAddress, enter an IP address that is open on your network between the parenthesizes. Make sure it’s not an IP address that your DHCP server would give out to another client. 192.168.1.177 should be fine for most simple home networks. For more on IP addresses, see: http://en.wikipedia.org/wiki/IP_address. Remember this IP address for step 3.

Connect the ethernet cable or your network and the USB cable to your computer’s USB port. Upload the sketch.

The device is now properly programmed!

Step 3: Operation

To operate our Garage Door Status Server, navigate your web browser to the IP address you specified earlier. The default is 192.168.1.177.  Here’s what you should see:
This web page shows you the status of your garage door. It defaults to stating that the garage door is open. It also automatically reloads every five seconds and checks the status of the door. Watch what happens when you press and hold the button.
The web server now reports the garage door as being closed and the LED on the breadboard lights up. Notice you have to keep pressing down the button for it to report as closed.
Another item to note is that this web page is only available within your local network. It will take some work with port forwarding on your router to make it available publicly. We’ll leave that for a another post.
And there you have it!
To take this beyond the prototype form, I plan on using a magnetic reed switch to determine if the door is open or closed. It should be as simple as replacing the momentary push button switch with a magnetic reed switch.
Let me know in the comments if this worked for you or if you have any suggestions for improvements.
Tagged , ,

Force Sensitive Resistor, LED, LCD Display and Arduino

About three weeks ago, I picked up the Make: Ultimate Microcontroller Pack with Arduino Uno, as well the book Getting Started with Arduino by Massimo Banzi. While I don’t have a background in micro controllers and have only the most basic knowledge of programming, I’ve found learning the Arduino hardware and software quite fun!

Inspired by the projects in chapter five of Bonzi’s book and some of the parts in the Ultimate Microcontroller Pack, I decided to use a force sensitive resistor(FSR) to light up an LED proportional with amount of force applied to the FSR. Basically, the harder you squeeze the FSR, the brighter the LED gets. Kind of neat, but nothing too impressive. So, I decided to integrate the LCD display into the project and have it show a number representing the amount of force on the FSR.

This project does not do anything practical, besides impress people with your basic electronics skills. However, I found the experience to be quite educational.

Here it is:

Here’s how I did it:

Materials:

Arduino Uno Microcontroller Board

Lots of Jumper Wires

Full Sized Solder-less Breadboard

Arduino Compatible LCD display, 2×16 with 16 pin interface

10K Potentiometer, breadboard compatible

One 10K resistor

One 1K resistor

One Force Sensitive Resistor

Steps:

1. Connecting the LED

The simplest step is connecting the LED to the Arduino. I’m using a six inch breadboard and wired the LED on the opposite end from where I plan to place the LCD. I connected the LED to the breadboard and wired the positive side to pin 9 on the Arduino. It’s important to use one of the Arduino’s PWM (Pulse-Width Modulation) pins as they are capable of the <analogWrite> command. I choose pin 9 on my Arduino Uno because it does PWM and isn’t used by the LCD display later.

To the negative end of the LED, I connected a 1K ohm resistor and used a jumper wire to connect it to the ground rail of the breadboard. (I’m not sure if 1K ohm is the best to use, but it seems to work fine.) Connect a jumper wire from the ground rail of the breadboard to one of the ground pins on the Arduino.

2. Connecting the FSR

The force sensitive resistor is also easy to connect to the breadboard. First, connect a jumper wire from the 5 volt pin on the Arduino to the positive rail of the breadboard. This is important because because pin #2 on the LCD will also use 5 volts. From the 5V rail connect a jumper to one side of your force sensitive resistor. At the opposite foot of the FSR, connect a 10k ohm resistor and a jumper wire to the analog 0 pin or the Arduino. At the other end of the 10k resistor, connect a jumper wire to the ground rail of the breadboard.

3. Connecting the LCD Display

For this step, you’ll need an LCD display that is compatible with Arduino, a 10K potentiometer and quite a few jumper wires. I used a 2 x 16 LCD display with 16 interface pins from the Make: Ultimate Microcontroller Pack. The 10K pot from that set doesn’t work with a breadboard, so I purchased this one from RadioShack

This is the most difficult part of the build. Fortunately, the official Arduino LCD tutorial uses the exact same 2 x 16 LCD display. Still,  I had a hard time wiring it to the breadboard. The most helpful resource is this image, showing where each pin on the LCD connects to the Arduino and the pot. On my setup, it’s slightly different because I used the 5v and ground rails on the breadboard. Here’s a helpful chart:

LCD Pin         Connected to

#1                     LCD pin #5

#2                     First pin of pot

#3                     Second pin of pot

#4                     Digital pin #12 of Arduino

#5                     Pin #1 of LCD and third pin of pot

#6                     Digital pin #11 of Arduino

#11                   Digital pin #5 of Arduino

#12                   Digital pin #4 of Arduino

#13                   Digital pin #3 of Arduino

#14                   Digital pin #2 of Arduino

Potentiometer

#1                     LCD pin #2 and 5v rail

#2                     LCD pin #3

#3                     LCD pin #5 and ground

It is easy to get confused with all of the wires and you’ll need to double check your connections.

4. Programming the Arduino

Here’s the sketch to upload to your Arduino:

// Force Sensitive Resistor(FSR) to light LED analogly according to pressure
// and display amount of pressure on an 16*2 LCD screen

// Includes public domain LCD display code from official Arduino website

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // set pins for LCD
// set global variables
const int LED = 9; // LED connected to pin 9 for analog write
const int FSR = 0; // FSR connected to analog pin 0

int level = 0; // variable for the level of force applied to the FSR

void setup() // run once to setup
{
pinMode (LED, OUTPUT); // LED set as output
//analog pins are automatically set to input
lcd.begin(16,2); // set up LCD number of columns and rows
lcd.print(“Pressure Level:”); // display message on LCD
}
void loop() // repeated routine
{
// first, read the pressure level and set brightness of LED
level = analogRead(FSR)/4; // read level of pressure on the FSR
// divided by 4 as analog read is 4x sentive as digital write
analogWrite(LED, level); // set brightness of LED to pressure level
delay(100); // wait to see the effect

//display pressure level on the LCD screen
lcd.setCursor(0,1); // set cusor to second line, first spot
lcd.print(level); // display level of pressure
}

Now, all you need to do is squeeze the force sensitive resistor, watch the LED light up accordingly and note the pressure level on the LCD. The pressure level goes from 0 to 255. You can adjust the contrast level of the LCD with the potentiometer. It’s good for a few minutes of amusement.

I’ve noticed some problems with the LCD displaying nonsensical values when the level of force is going down and below 100.

Please let me know in the comments if there are any problems in the instructions, or if you have any suggestions for improvements.

Tagged , , ,

Welcome!

Greetings and welcome to Joe’s Gizmos! This is the first of what hopefully will be many more blog posts about my experiments with micro controllers, home made electronics, and other related fun projects. I hope you find Joe’s Gizmos to be enjoyable as well as informative. I encourage anyone reading this to join the conversation by making comments and sharing interesting posts with like minded friends. Thanks for reading.

Follow

Get every new post delivered to your Inbox.

Join 266 other followers