Home‎ > ‎Unit 06‎ > ‎

Reaction Challenge

In this lesson you will set up a simple game. You need an LED (plus resistor) attached to your Arduino board on pins 12 (green LED) and 13 (red LED). When you run the program the red LED will turn on. A bit later it will turn off and the green LED will turn on. You need to press a key while the green one is on. If you do you catch it. After 3 seconds it will start over, but this time the green one will be on for less time.

Try it out and see how you do.

import processing.serial.*; 
import cc.arduino.*;    

Arduino arduino;      

int redLed = 13;    //Red LED in pin 13 on Arduino
int greenLed = 12;  //Green LED in pin 13 on Arduino
int counter=0;
int reac=300;       //Reaction time in milliseconds
int stop=200;      //Number of cycles

void setup() {
  size(100, 100);    
  arduino = new Arduino(this, Arduino.list()[0], 57600);  //Initilize Arduino
  arduino.pinMode(redLed, Arduino.OUTPUT);            //Set both led pins as OUTPUTS
  arduino.pinMode(greenLed, Arduino.OUTPUT);
}


void draw()
{
  counter+=1;
  arduino.digitalWrite(redLed, Arduino.HIGH);    //Turn on red LED
  if (counter<stop)                              //Keep going untill Stop
  {
    delay(1);
  }
  else if (counter==stop)                        //Are we there yet?
  {
    arduino.digitalWrite(redLed, Arduino.LOW);   //Turn off red LED and turn on the Grren one
    arduino.digitalWrite(greenLed, Arduino.HIGH);
    delay(reac);                                  //Wait for reaction
  }
  else                                            //You missed it Start over
  {
    arduino.digitalWrite(greenLed, Arduino.LOW);
    counter=0;
  }
}

void keyPressed()    //Did you press a key at the right time?
{
  if (counter<stop)  //Pressed too soon, reset counter to zero
  {
    counter=0;
    println("Nope");
  }
  else if (counter == (stop)) //Perfect timing
  {
    print("Winner ");    //Proclaim your status
    println(reac);       //Current best reaction time
    delay(3000);         //wait for 3 seconds to bask in your glory
    reset();            //Execute the reset process
  }
}

void reset()
{
  stop = int(random(100, 500));  //Set the stop to a random integer
  reac = int(reac*.9);          //Decrease the reaction time by 10% and set as integer
  arduino.digitalWrite(greenLed, Arduino.LOW); //turn off the green LED
  counter=0;                    //reset the counter
}

You'll recall keyPressed as a variable from an earlier unit. In this sketch we used keyPressed() as a function. We can have more than just the setup() and draw() functions. The keyPressed() function will execute whenever a key is pressed. If we're in draw() the program will leave off where it is, goto the keyPressed() function, execute the code therein, and then return to draw() exactly where it left off.

I also created my own function, reset(). This is a function that I created and named. reset has no special meaning in Processing and it could have been named anything. I call the reset() function from within the keyPressed() function. When the program reaches reset()  it leaves the function it is in and executes the code in reset(). Once done it goes back to where it left off.

It is often very handy to make your own functions. It can help you streamline your draw() and make it easier to keep track of what happens when. It is also particularly useful when you might be using the same code over and over again.

Read the descriptions of int() and random() in the Processing reference. Be sure to focus on how they are used and what sort of parameters they can have.


Assignment 6.7 - There is currently no way to totally lose the game. Add in code to make it so you can lose and have the game start over from the beginning. Also add in code to actually draw something in our grey box (you don't have to keep it a grey box, btw) to indicate wins and losses.
Comments