Tag Archives: buzzer

Remember the colours – a game

The following article is based on this entry on Newton’s blog (in Polish). All the pictures, program code and the video clip came from there.

This project’s goal was to use as many parts from the Arduino Starter Kit, as possible. So, what is this project exactly? As the title itself suggests, it’s a game, whose object is to remember a colour sequence and recreate it. At the very beginning the sequence consists of 3 colours only, hovever, the difficulty level increases – an extra colour is added each time. Every colour has a different tone assigned. This tone is emitted from a buzzer when the corresponding colour is displayed, so the game involves two senses, both sight and hearing. User interface consists of: RGB LED diode displaying the colours, 8 red LED diodes arranged in a row to display which colour in turn we are setting and two microswitch type buttons. By pushing both in the same time we are starting the game, the left button chooses the colour, the right – accepts. There are two more LED diodes, indicating whether we have recreated the sequence properly (left LED), or not (right LED).

Whole device mounted on a breadboard looks like the following:

Device mounted on a breadboard

You can see (and hear) how it works on this video clip. Hereunder is the circuit schematic made in Fritzing:

Circuit schematic

Circuit schematic

All the resistors used are 220Ω and the IC in the centre of the breadboard is 74HC595 shift register, which allows to drive eight LEDs using only three Arduino digital pins. This was described in “Shifted LEDs” article.

When everything is connected, we should take care of the code. At the beginning we must declare where we have connected what (to which input/output). The following code is responsible of it:

int latchPin = 8, clockPin = 12, dataPin = 3; //shift register
byte data = 0; //register data

int led8 = 7, led9 = 6; //additional LEDs
int ledR = 11, ledG = 10, ledB = 9; //LED RGB
int buttonLeft = 5, buttonRight = 4; //buttons
int speakerPin = 13; //buzzer

Here comes the initialization part – setting pin as input/output, turning internal pull-up resistors on and so on:

void setup()
  for(int i=3;i<=13;i++) // setting all the pins
    pinMode(i, OUTPUT); // from 3 to 13 as outputs
  pinMode(buttonLeft, INPUT); // setting the button pins
  pinMode(buttonRight, INPUT); // as inputs

  digitalWrite(buttonLeft, HIGH); // turning internal pull-up
  digitalWrite(buttonRight, HIGH); // resistors on
  randomSeed(analogRead(0)); // initializing the pseudorandom number generator
  // with the value read from analog input no. 0 
  // - each time it's different

If you are wondering what stands for such a strange notation:

 a ? b : c

here is the explanation.

The main loop of our program looks like the following:

void loop()
  if(waitForButton() == 3)

  for(int j=3;j<=8;j++)
    if(!playLevel(7, j))

Consecutively: at the beginning, effect() function turns on visual and light effects, clearLeds turns all the LEDs off. After that, unless the user pushes both buttons at the same time, what will cause a reset, playLevel function starts successive levels of the game, increasing the second parameter (number of the lights to remember) by 1. The first parameter denotes the total number of colours (from 0 to 7). Of course, we can lower it, but then the game will become too easy. It’s better to add another colours using setColor function (changing parameters in setRGB) and upgrade it. Then, we can put the total number of colours (remember – the numeration starts from 0!) as the first parameter of playLevel function to make the game more difficult and hence – more addicting ;).

The whole program code with comments is available here. I think it’s clear enough and doesn’t require any additional explanations.

Arduino – play!

A long silence on the blog is just broken! During the last few months I was strongly engaged in projects not connected with Arduino, whole time was absorbed by keeping the Arduino shields shop, servicing customers, searching for new suppliers, etc.

Now I have a bit time which I can spend by documenting experiments with Arduino, thus I hope to add some examples with Starter Kit soon.

Silence, go away!

As we have to break the silence, so więc let’s begin from controlling so called buzzer a tiny speaker able to produce not very loud sounds, with the Arduino. Let’s start from final product:

Arduino buzzer from Starter Kit on Vimeo.

Buzzer is controlled by Arduino, playing simple melody. Playing speed depend on reading from photoresistor (the more light, the slower it plays).

How it’s made?

I wrote, how to read values from photoresistor in one of previous posts. Buzzer is stimulated to play by properly applied high and low voltage. For (Arduino output port) safety a 220 Ohm resistor is added to lower the current passing through (from PDF with documentation appears that it’s not obligatory – at least 14 Ohm resistance should not generate current higher than 36 mA – lower than the limit value for Arduino I/O port – 40 mA).
Thankfully Arduino is open source and there are many ready to use elements. So, playing melody (note) issue is described in Melody tutorial. We will take advantage of it and add only part reading value from photoresistor and converting it to adequate delay.

The only modification of Melody tutorial (except from setup) is a line in loop:


It divides the voltage reading by 3 and adds 100. This way we will obtain value in range from 100 to 1023/3+100=441. Original tempo was delay 300. Only the schematic remains:

The device schematic

The device schematic

For beginners – a detailed description, how to connect the wires:

Physical connections

Physical connections

Detailed part list (Fritzing has only piezo buzzer in basic library, so it is used on the schematics):

Fritzing Bill of Materials

Sketch: 	buzzer.fz
Date: 	Tue Sep 29 01:11:59 2009

Arduino1      Arduino Diecimila
Breadboard1   Tiny Breadboard
J1            Piezo Speaker
R1            220 Ω Resistor
R2            10k Ω Resistor
R3            Basic Photo-Resistor (Photocell)

Shopping List

Quantity	Part

1		10k Ω Resistor
1		220 Ω Resistor
1		Arduino Diecimila
1		Basic Photo-Resistor (Photocell)
1		Piezo Speaker
1		Tiny Bre

And finally – the code used in example: melody.zip

How do you like the diagrams and pictures designed in Frizting? For me, it’s perfect software for documenting experiments with Arduino. I reccommend – check it.

Let’s play, daddy – Family Feud

I was inspired by a simple DIY game seen on the web (I can’t find the link now) and despite the fact that original version was based on greatly simpler PIC I decided to build it with Arduino Starter Kit.

The rules are simple – the one, who will press his button faster after a diode lights up, gets a score. If the button will be pressed before the diode lights up, a point goes to opponent. The game continues, until someone gets five points. Let’s see it in action:

Simple game with Arduino from Starter Kit on Vimeo.

Beside the diode, which will be watched, we need also two diodes to indicate who gets the point and who has won the whole duel, buzzer to play simple melody at the end of the game and two buttons.

Continue reading