Tag Archives: button

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()
{
  Serial.begin(57600);
  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
  clearLeds();
}

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()
{
  effect();
  clearLeds();
  if(waitForButton() == 3)
    return;

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

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.

How to connect switches

Switches (buttons, pushbuttons, tact switches) are commonly used with Arduino. But, for beginners, it’s sometimes irritating when it comes to wire them up correctly. Some of their terminals are shorted with each other. Such a switch doesn’t work if putted in reversely. Because of that, here’s some words of explanation.

For instance, we have a button like this:

Just an ordinary button.

As you can see, it has four terminals organised in two pairs. These pairs are shorted when we are pushing the button. Which of them are which? Maybe this picture will be helpful:

Internal connections of the button

And here is a photo with a switch symbol shown:

Button with schematic

Now it’s clear that proper orientation is the crucial thing. Turning the switch by 90 degrees causes permanent short – the button will act as a jumper, not as switch.

For even cleaner explanation, example of a working circuit:

Circuit with a button

And this is how it looks with schematic:

Circuit with its schematic

This switch shorts the resistor to ground. How to detect whether the button is pressed or not? Read more in the article about Family Feud.

Indicator – how to use a button

Weekend month ago became rainy, so I decided to stay at home with my three children. It was a kind of challenge to survive it without strained nerves :)

My son (3 years and a half) is on the stage of racking the carpet in every way with every toy car and train available, so I came up with idea of dicator, as he called it…

Indicator in action

Indicator in action

Thirty minutes and I succeded to make such a thing. I hoped it would arouse interest for the next half an hour, but it turned out to be essential component of obstacle courses builded on the carpet for a couple of days.

How it works?

The literature first – the idea was to make a simple indicator changing its state after pressing a button.

The red LED diode we connect to digital input number 3, the green to no. 3 and the button to no. 4 input.

Circuit diagram:

Code:

int buttonPin = 4;

int LED = HIGH;

int greenPin = 02;                // LED connected to digital pin 13
int redPin = 03;
int infraPin = 0;
int val = LOW;
int prev = LOW;

#define ST_OFF 0
#define ST_ON 1
#define ST_PULSE 2
int status = ST_OFF;

//initial configuration
void setup()
{
  pinMode(greenPin, OUTPUT);      // sets the digital pin as output
  pinMode(redPin, OUTPUT);      // sets the digital pin as output
  pinMode(buttonPin, INPUT); // sets the digital pin as input
  Serial.begin(57600);
  digitalWrite(redPin,HIGH);
  digitalWrite(greenPin,HIGH);
}

void display_status() {
  int static pulse = LOW;
  int static count = 5;
  switch (status) {
    case ST_OFF:
    off(redPin);
    on(greenPin);
    break;
    case ST_ON:
    on(redPin);
    off(greenPin);
    break;
    case ST_PULSE:
    off(redPin);
    digitalWrite(greenPin, pulse);
    pulse = !pulse;
    delay(400);
    count--;
    if (count==0) {
      status=ST_ON;
      count=5;
    }
  }
}

void next_status() {
  switch (status) {
    case ST_OFF:
    status = ST_PULSE;
    break;
    case ST_ON:
    status = ST_OFF;

    break;
    case ST_PULSE:

    break;
  }
}

void on( int pin) {
  digitalWrite(pin,HIGH);
}
void off( int pin) {
  digitalWrite(pin,LOW);
}
void loop()
{
  val = digitalRead(buttonPin);
  if (val == HIGH && prev == LOW) {
    next_status();
  }
  prev = val;
  display_status();
  delay(50);

}

The indicator can be in three states: off (ST_OFF, green light), on (ST_ON, red light) and pulse (ST_PULSE, green flashes).

In the main loop is performed a test, if the button was pressed. If yes, it goes to next state (next_status) excluding ST_PULSE, in which it stubbornly stays. Thanks to it, pressing the button during flashing don’t cause jumping to next state.

After that, the state according to current status (display_status) is displayed. Here the flashing is implemented and, after a proper number of loops, the state is changed to ST_ON, which stands for red.

display_status could be source of problems if it was more complicated circuit. It adds its own delay in ST_PULSE (delay(400)), changes state – asking for trouble. But in such a simple circuit – it can be.

What new?

The new part, which we weren’t using is a switch. Simple switch, whose pressing Arduino should detect.

Indicator - simple but works

Indicator - simple but works

Potential trap – why is there a resistor? Why don’t like this:

Don't try it at home :)

Don't try it at home :)

Well, when the switch is turned off, everything is all right. Digital input is connected to the supply voltage and remains in high state, but after turning the switch on, supply voltage is shorted to the ground, what may damage the supply device.

In case of not using the supply voltage, we should realize that when the button isn’t pressed, then the digital input remains not connected and it may be improperly detected by Arduino. The state of this input is random, high or low. Pulling it up to +5V with resistor makes us sure that the state will be stable and device not damaged.

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