Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- The reaction game!
- */
- // include the library code:
- #include <LiquidCrystal.h>
- int playerOnePin = A0; // select the input pin for the potentiometer
- int playerTwoPin = A1;
- int ledPin = 9; // select the pin for the LED
- int ledPin2 = 10;
- int resetBtn = 13;
- int player1 = 0;
- int player2 = 0;
- const int THRESHOLD = 200;
- boolean roundOver = false;
- int rando;
- long lastReset = 0;
- // initialize the library with the numbers of the interface pins
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
- long numMillis = 10*1000-1;
- void setup() {
- lastReset = millis();
- randomSeed(analogRead(0));
- pinMode(ledPin, OUTPUT);
- pinMode(ledPin2, OUTPUT);
- pinMode(resetBtn, INPUT);
- // set up the LCD's number of columns and rows:
- lcd.begin(16, 2);
- // Print a message to the LCD.
- resetRound();
- lcd.setCursor(0,0);
- lcd.print("Reaction Game!");
- }
- void resetRound(){
- lcd.setCursor(0, 1);
- lcd.print(" ");
- digitalWrite(ledPin, 0);
- digitalWrite(ledPin2, 0);
- rando = random(3,7);
- lastReset = millis();
- roundOver = false;
- }
- void loop() {
- if(digitalRead(resetBtn) && ((millis() - lastReset) > 1000)){
- resetRound();
- }
- if(roundOver){
- return;
- }
- player1 = analogRead(playerOnePin);
- player2 = analogRead(playerTwoPin);
- // set the cursor to column 0, line 1
- // (note: line 1 is the second row, since counting begins with 0):
- lcd.setCursor(0, 1);
- // print the number of seconds since reset:
- long countDown = numMillis-millis()+lastReset;
- if(countDown > rando*1000) {
- lcd.print(countDown);
- }
- else {
- lcd.print("????");
- }
- if (player1 > THRESHOLD){
- if (countDown > 0){
- endRound(2, countDown);
- digitalWrite(ledPin2, 1);
- }
- else{
- endRound(1, countDown);
- digitalWrite(ledPin, 1);
- }
- }
- if (player2 > THRESHOLD){
- if (countDown > 0){
- endRound(1, countDown);
- digitalWrite(ledPin, 1);
- }
- else{
- endRound(2, countDown);
- digitalWrite(ledPin2, 1);
- }
- }
- }
- void endRound(int player, int countdown){
- roundOver = true;
- //lcd.clear();
- lcd.setCursor(0, 1);
- lcd.print(" ");
- lcd.setCursor(0,1);
- lcd.print(countdown);
- lcd.setCursor(0,1);
- //String str = "Player " + player;// + " wins!";
- //lcd.print(str);
- }
Advertisement
Add Comment
Please, Sign In to add comment