Introduction to Arduino Programming 6: Gameshow

#Introduction to Arduino Programming
A series of based on New York University’s Introduction to Computer Programming class from the Courant Institute of Mathematical Sciences.

http://cims.nyu.edu/~kapp/common/CSCI-UA.0002/fall2013/

This is intended as a “gentle” introduction to computer programming and Arduino. The only prerequisite is that you’ve completed our Arduino Basic Basics guide!

###The Game Show

By using the special decision making powers imbued in the Arduino bit we’re going to create a multi-player game show that uses a few bits and some knowledge about random trivia.

###The Circuit

  • P1 Power Bit
  • Arduino Bit
  • 2 Buttons (one for each player)
  • 2 Leds or Bargraphs (one for each player)
  • W19 wire (to split power amongst the buttons)
  • 2 Buzzers (optional)
  • 2 W1 wires (optional)

Build your circuit so the P1 Power Bitgoes into each button by using the W19 wire if you like a longer circuit by using 2 W1 wires. I used the A0 and A1 pins for input and 1 and 9 pins for after.

The code!

//  Game Show
//
// Made by Syed Salahuddin
// License: CC-BY-SA 3.0

//let's tell our Arduino which input and output snaps we're working with.


const int buttonPlayer2 = A0;    //input on a0
const int buttonPlayer1 = A1;	 //input on a1
const int buzzerPlayer1 = 9;     //output on d9
const int buzzerPlayer2 = 1;	 //output on d1


void setup() {
  // initialize the buzzers as output:    
  pinMode(buzzerPlayer1, OUTPUT);
  pinMode(buzzerPlayer2, OUTPUT);
 
  // initialize the pushbutton as input:
  pinMode(buttonPlayer1, INPUT);
  pinMode(buttonPlayer2, INPUT);
}


void loop(){
   //let's reset our buzzers
  digitalWrite(buzzerPlayer1, LOW);
  digitalWrite(buzzerPlayer2, LOW);

  //read the state of pushbutton value
  if(digitalRead(buttonPlayer1) == HIGH){
      digitalWrite(buzzerPlayer1, HIGH);
      delay(2000);
  } else if(digitalRead(buttonPlayer2) == HIGH){
      digitalWrite(buzzerPlayer2, HIGH);
      delay(2000);
  }

}

Let’s break it down!

Our variables!

const int buttonPlayer2 = A0;    //input on a0
const int buttonPlayer1 = A1;	 //input on a1
const int buzzerPlayer1 = 9;     //output on d9
const int buzzerPlayer2 = 1;	 //output on d1

So we have defined four variables that are integers. Each of them corresponds to a bitsnap or pin on our Arduino. We have tow inputs that correspond to each player’s button. Then we have two corresponding buzzers for each player. So when a player presses their buzzer goes off for a few seconds.

Our pin modes!

  // initialize the buzzers as output:    
  pinMode(buzzerPlayer1, OUTPUT);
  pinMode(buzzerPlayer2, OUTPUT);
 
  // initialize the pushbutton as input:
  pinMode(buttonPlayer1, INPUT);
  pinMode(buttonPlayer2, INPUT);

buzzers will be output and players will be input

Now let’s enter our loop()

   //let's reset our buzzers
  digitalWrite(buzzerPlayer1, LOW);
  digitalWrite(buzzerPlayer2, LOW);

Notice we’re using a brand new function called digitalWrite() that takes two arguments. The pin we’re writing to and a value telling digitalWrite function to go on or off. Think of digitalWrite as a light switch. You can either turn it off or on, same thing with digitalWrite. We’re using it because don’t really need a range of values. (unlike the bargraph that can take a multitude of values)

IF ONLY

  //read the state of pushbutton value
  if(digitalRead(buttonPlayer1) == HIGH){
      digitalWrite(buzzerPlayer1, HIGH);
      delay(2000);
  } else if(digitalRead(buttonPlayer2) == HIGH){
      digitalWrite(buzzerPlayer2, HIGH);
      delay(2000);
  }

Ok so this is it! We’re making decisions with the Arduino! So What? Well when you start making decisions based of off some stateof our program, in this case if our button is pressed or not, you can do cool stuff like determine a winner or loser and keep track of score if a player has gotten something right.

In this case:

 if(digitalRead(buttonPlayer1) == HIGH)

We read the value of player one’s button and check if it’s been pressed. We do that by checking against the HIGH value of the Arduino (basically meaning on)

The way we do that is by the ==, operator.

Oper-wat?

You’ve used operators all your life. Think of 2 + 2 the operator in this case is + and it’s operands are 2. In this case our operator is a special operator called a relational operator it checks the relationship between of the value or expression on the left-side of the operator and the one to the right. We use == because = is already taken to assign variables. Don’t forget it, other wise you’ll be in a world of pain :open_mouth:

The insides

      digitalWrite(buzzerPlayer1, HIGH);
      delay(2000);

This straight forward so after we’ve checked that the value that is returning from digitalRead() is HIGH. Then let the buzzer buzz away!! (for 2 seconds or 2000 milliseconds).

OR ELSE

else if(digitalRead(buttonPlayer2) == HIGH){
      digitalWrite(buzzerPlayer2, HIGH);
      delay(2000);
}

So we’ve seen the if statement, it has a sister statement else if which kind of reads like "if player one’s button isn’t pressed then do this instead.

if the first player’s button has not been pressed then let’s check the second player’s button. So the first player does have a tiny bit of an advantage (8 milliseconds or so) but it shouldn’t effect the outcome!

And there you go! You’ve got yourself a gameshow controller!

Troubleshooting

##Remix!

  • How would you add more players
  • What other rules could you add to the logicof the Arduino code?
  • How would you add a time limit that buzzed for10 seconds after a player presses a button?!