Monday, January 17, 2011

Shell Game Arduino

Since I have no new sensors left to connect to the Arduino for now, I thought about various small experiments I could do with the components I have. One of those experiments is a Shell Game.

You know those shady guys on the street that bet, you can't spot under which cup they put a ball while shuffling them? Well, this is called a shell game, because in the past it was played with walnut shells. Unfortunately those games are fraud and after someone else wins (another con man), you are up to a big loss.

In this experiment you can build your own shell game. Its intended to be used as an eye training and not to rip off your friends. So don't do anything evil with it.

At each round three LEDs are turned on at random for a very short amount of time. At the end you can guess which light flashed last. If you press the correct button, a green LED blinks. If you were wrong, a red LED blinks.

The setup is simple. Here is a schematic on how to connect everything to the Arduino Uno. Note that I left out the resistors in front of each LED so that the image doesn't get too crowded. I used 470Ω resistors. You need the resistors so that the LEDs don't get damaged by too much current.

The source code is pretty simple. You can change the On/Off delays for the LEDs to adjust the difficulty.
int leftLED = 2;
int middleLED = 3;
int rightLED = 4;
int upperLED = 5;
int lowerLED = 6;
int leftButton = 7;
int middleButton = 8;
int rightButton = 9;
int randomLED;
int lastLED;
int answerLED;
int analogReadSum;
boolean answerGiven = false;

void setup() {
  digitalWrite(leftButton, HIGH);
  digitalWrite(middleButton, HIGH);
  digitalWrite(rightButton, HIGH);
}

void loop() {
  answerGiven = false;
  //All analog ports are read, summed up and devided by their count.
  //There are fluctuations on the ports, so we should get a new number in each run.
  for(int x = 0; x < 6; x++) {
    analogReadSum += analogRead(x);
  }
  analogReadSum /= 6;
  //Initialize random sequence by unique number
  randomSeed(analogReadSum);
  analogReadSum = 0;
  for(int x = 0; x < 10; x++) {
    randomLED = random(2,5);
    digitalWrite(randomLED, HIGH);
    delay(15);
    digitalWrite(randomLED, LOW);
    delay(10);
    if(x == 9) {
      lastLED = randomLED;
    }
  }
  while(answerGiven == false) {
    if(digitalRead(leftButton) == LOW) {
      answerLED = 2;
      answerGiven = true;
    } else if(digitalRead(middleButton) == LOW) {
      answerLED = 3;
      answerGiven = true;
    } else if(digitalRead(rightButton) == LOW) {
      answerLED = 4;
      answerGiven = true;
    }
  }
  if(answerLED == lastLED) {
    for(int x = 0; x < 3; x++) {
      digitalWrite(upperLED, HIGH);
      delay(1000);
      digitalWrite(upperLED, LOW);
      delay(500);
    }
  } else {
    for(int x = 0; x < 3; x++) {
      digitalWrite(lowerLED, HIGH);
      delay(1000);
      digitalWrite(lowerLED, LOW);
      delay(500);
    }
  }
}
Here is a small clip of the game:

video