Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define startSwitch A0
- #define alarm 8
- unsigned long startTime, buttonEvent1, buttonEvent2, playerOne, playerTwo, pwr;
- int switchState = 0;
- int startInterval = 0;
- int press1 = 0;
- int press2 = 0;
- int dis1 = 0;
- int dis2 = 0;
- int disQ = 0;
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
- void setup() {
- lcd.begin(16, 2);
- pinMode (2,OUTPUT);
- pinMode(3,OUTPUT);
- pinMode (8, OUTPUT);
- pinMode (9,INPUT);
- pinMode (10, INPUT);
- Serial.begin(9600);
- Serial.println("criticalGame2");
- }
- void loop() {
- pwr = millis();
- int gameStarted = 0;
- digitalWrite(8,0);
- lcd.clear();
- lcd.noDisplay();
- switchState = analogRead(startSwitch);
- Serial.print("magnet: ");
- Serial.println(switchState);
- // run the program
- if (switchState > 17) {
- startTime = millis();
- Serial.println(startTime - pwr);
- startInterval = random(1000, 7000);
- Serial.print("timeToStart: ");
- Serial.println(startInterval);
- lcd.display();
- lcd.print("Get Ready!");
- lcd.print("");
- // Game is ready, loop until some player presses a button
- while (press1 < 1001 && press2 < 1001) {
- // When the start interval is up, say GO!
- if (startTime -pwr >= startInterval && gameStarted == 0) {
- lcd.clear();
- lcd.print("Go!");
- digitalWrite(8,1);
- gameStarted = 1;
- delay(2000);
- lcd.clear();
- }
- // Read to see if either player pressed a button
- press1 = analogRead(A1);
- press2 = analogRead(A2);
- Serial.print(press1);
- Serial.print(": ");
- Serial.println(press2);
- if (press1 > 1000) {
- // player 1 pressed the button before the interval elapsed - CHEATER
- if (gameStarted == 0) {
- lcd.print("Player 1");
- lcd.setCursor(0,1);
- lcd.print("Disqualifed!");
- delay(2000);
- lcd.clear();
- }
- else {
- // print player 1 wins and beep/light
- buttonEvent1 = millis();
- playerOne = (buttonEvent1 - startTime);
- Serial.println("player1 wins");
- lcd.clear();
- digitalWrite(8,0);
- lcd.setCursor(1,0);
- lcd.print("Player 1 wins!");
- delay(2000);
- lcd.clear();
- lcd.setCursor(6,0);
- lcd.print(playerOne);
- lcd.setCursor(2,1);
- lcd.print("Milliseconds");
- delay(5000);
- }
- }
- if (press2 > 1000) {
- // player 2 pressed the button before the interval elapsed - CHEATER
- if (gameStarted == 1) {
- lcd.print("Player 2");
- lcd.setCursor(0,1);
- lcd.print("Disqualifed!");
- delay(2000);
- lcd.clear();
- }
- else {
- // print player2 wins and beep/light
- buttonEvent2 = millis();
- playerTwo = (buttonEvent2 - startTime);
- Serial.println("player2 wins");
- lcd.clear();
- digitalWrite(8,0);
- lcd.setCursor(1,0);
- lcd.print("Player 2 wins!");
- delay(2000);
- lcd.clear();
- lcd.setCursor(6,0);
- lcd.print(playerTwo);
- lcd.setCursor(2,1);
- lcd.print("Milliseconds");
- delay(5000);
- }
- }
- }
- }
- // If game not started yet, loop again, at 40hz
- else {
- delay(25);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement