awinograd

Reaction Game

Apr 26th, 2012
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. /*
  2.   The reaction game!
  3.  */
  4.  
  5. // include the library code:
  6. #include <LiquidCrystal.h>
  7.  
  8.     int playerOnePin = A0;    // select the input pin for the potentiometer
  9.     int playerTwoPin = A1;
  10.     int ledPin = 9;      // select the pin for the LED
  11.     int ledPin2 = 10;
  12.     int resetBtn = 13;
  13.     int player1 = 0;
  14.     int player2 = 0;
  15.     const int THRESHOLD = 200;
  16.     boolean roundOver = false;
  17.     int rando;
  18.     long lastReset = 0;
  19.  
  20. // initialize the library with the numbers of the interface pins
  21. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  22. long numMillis = 10*1000-1;
  23.  
  24. void setup() {
  25.   lastReset = millis();
  26.   randomSeed(analogRead(0));
  27.   pinMode(ledPin, OUTPUT);  
  28.   pinMode(ledPin2, OUTPUT);
  29.   pinMode(resetBtn, INPUT);
  30.   // set up the LCD's number of columns and rows:
  31.   lcd.begin(16, 2);
  32.   // Print a message to the LCD.
  33.   resetRound();
  34.   lcd.setCursor(0,0);
  35.   lcd.print("Reaction Game!");
  36. }
  37.  
  38. void resetRound(){
  39.   lcd.setCursor(0, 1);
  40.   lcd.print("         ");
  41.   digitalWrite(ledPin, 0);
  42.   digitalWrite(ledPin2, 0);
  43.   rando = random(3,7);
  44.   lastReset = millis();
  45.   roundOver = false;
  46. }
  47.  
  48. void loop() {
  49.   if(digitalRead(resetBtn) && ((millis() - lastReset) > 1000)){
  50.    resetRound();
  51.   }
  52.  
  53.   if(roundOver){
  54.     return;
  55.   }
  56.  
  57.   player1 = analogRead(playerOnePin);
  58.   player2 = analogRead(playerTwoPin);
  59.  
  60.   // set the cursor to column 0, line 1
  61.   // (note: line 1 is the second row, since counting begins with 0):
  62.   lcd.setCursor(0, 1);
  63.   // print the number of seconds since reset:
  64.   long countDown = numMillis-millis()+lastReset;
  65.   if(countDown > rando*1000) {
  66.     lcd.print(countDown);
  67.   }
  68.   else {
  69.     lcd.print("????");
  70.   }
  71.  
  72.   if (player1 > THRESHOLD){
  73.     if (countDown > 0){
  74.       endRound(2, countDown);
  75.       digitalWrite(ledPin2, 1);
  76.     }
  77.     else{
  78.       endRound(1, countDown);      
  79.       digitalWrite(ledPin, 1);
  80.     }
  81.   }  
  82.   if (player2 > THRESHOLD){
  83.     if (countDown > 0){
  84.       endRound(1, countDown);      
  85.       digitalWrite(ledPin, 1);
  86.     }
  87.     else{
  88.       endRound(2, countDown);
  89.       digitalWrite(ledPin2, 1);
  90.     }
  91.   }    
  92. }
  93.  
  94. void endRound(int player, int countdown){
  95.   roundOver = true;
  96.   //lcd.clear();
  97.   lcd.setCursor(0, 1);
  98.   lcd.print("         ");
  99.   lcd.setCursor(0,1);
  100.   lcd.print(countdown);
  101.   lcd.setCursor(0,1);
  102.   //String str = "Player " + player;// + " wins!";
  103.   //lcd.print(str);
  104. }
Advertisement
Add Comment
Please, Sign In to add comment