Skip to content

Physical Computing Lab: Getting Creative with Digital I/O

September 14, 2011

Here’s the game I made to demonstrate my mastery of digital inputs and outputs on the Arduino. I’d like to make a nice enclosure for it with an arcade button instead of one of these little tactile switches, but that might take too much time. Plus, if I did it, I would want to use panel-mount LEDs, which I won’t be able to get in time for Thursday. Here’s the code:

/* Press Your Luck! */
#define buttonPin 2
int val;

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  randomSeed(analogRead(0)); // make it really random
}

void loop() {
  val = random(3,10);
  digitalWrite(val, HIGH);
  if (digitalRead(buttonPin) == HIGH)
  {
    delay(750);
    digitalWrite(val, LOW);
    delay(500);
    digitalWrite(val, HIGH);
    delay(750);
    digitalWrite(val, LOW);
    delay(500);
    digitalWrite(val, HIGH);
    delay(750);
    digitalWrite(val, LOW);
    delay(500);
    digitalWrite(val, HIGH);
    
  }
  delay(30);
  digitalWrite(val, LOW);
}

With some adjustments to the code, I’ve changed it into a game of skill. Here’s a demo:

And here’s the code:

/* Test Your Reflexes! */
#define buttonPin 2
void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
}

void loop() {

  for (int val = 3; val < 10; val++)
  {
    digitalWrite(val, HIGH);
    delay(50);
    if (digitalRead(buttonPin) == HIGH)
    {
      delay(750);
      digitalWrite(val, LOW);
      delay(500);
      digitalWrite(val, HIGH);
      delay(750);
      digitalWrite(val, LOW);
      delay(500);
      digitalWrite(val, HIGH);
      delay(750);
      digitalWrite(val, LOW);
      delay(500);
      digitalWrite(val, HIGH);
    }
     digitalWrite(val, LOW);
  }
}

As a final step, I boxed up the project in a SnapWare container with a rocker switch to cut power from the battery to the Arduino and I used an arcade switch instead of a tactile switch. Here’s a demo:

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: