// //int latchPin = 11; white //int clockPin = 9; orange //int dataPin = 12; brown //int buttonPin = 4; int clockPin = 0; int latchPin = 1; int dataPin = 2; int buttonPin = 3; bool hasWon = false; bool hasLost = false; int num = 0; bool MSB = true; unsigned long timeNow = 0; int waitPeriod = 200; byte mode = 2; byte gamestate = 0; byte leds = 0; void setup() { // put your setup code here, to run once: // Serial.begin(9600); pinMode(latchPin, OUTPUT); pinMode(dataPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(buttonPin, INPUT_PULLUP); } void loop() { // put your main code here, to run repeatedly: leds = 0; int buttonValue = digitalRead(buttonPin); if (buttonValue == LOW){ if(gamestate == 0){ //At Start menu. pressed and will play game // Serial.println("Gamestate: 0"); gamestate++; delay(500); } if(gamestate == 1){ //runAnimation // Serial.println("Gamestate: 1"); bitSet(leds, 1); updateShiftRegister(); bitSet(leds, 7); updateShiftRegister(); delay(200); bitSet(leds, 2); updateShiftRegister(); bitSet(leds, 6); updateShiftRegister(); delay(200); bitSet(leds, 3); updateShiftRegister(); bitSet(leds, 5); updateShiftRegister(); delay(200); bitSet(leds, 4); updateShiftRegister(); delay(200); num = 0; leds = 0; updateShiftRegister(); delay(100); gamestate++; } if(gamestate == 2){ //play game with mode 2 // Serial.println("Gamestate: 2"); if(num == 4){ // Serial.println("WON"); hasWon = true; } if(num > 4 || (num != 4 && num != 0) ){ // for some reason debounce happens here. had to make sure the button was not pressed off the start. // Serial.println("LOST!"); hasLost = true; } delay(500); } } if(gamestate == 2){ if (millis() > timeNow + waitPeriod) { if(!hasWon && !hasLost){ if(mode == 1){ mode1(); }else{ mode2(); } bitSet(leds, num); updateShiftRegister(); timeNow = millis(); } } nextMode(); } } void nextMode(){ if(hasWon || hasLost){//blink green led // Serial.println("In Next Mode"); int tempLED = num; for(byte i = 0; i < 6; i++){ //loop 5 times // Serial.println("Looping"); delay(200); bitSet(leds, tempLED); updateShiftRegister(); delay(200); leds = 0; num = 0; updateShiftRegister(); } if(hasWon){ if(waitPeriod >= 100){ waitPeriod -= 50; }else{ waitPeriod -= 10; } num = 0; mode = random(2); //switch randomly between two modes. MSB = true; hasWon = false; }else if(hasLost){ waitPeriod = 200; num = 0; mode = random(2); //switch randomly between two modes. MSB = true; hasLost = false; gamestate = 0; } } } void mode1(){ //One path the whole time if(num < 7){ num++; }else{ num = 1; } } void mode2(){ //Bounce back and fourth if(num < 8 && MSB){ num++; if(num == 8){ MSB = false; } } if(num > 1 && !MSB){ num--; if(num == 1){ MSB = true; } } } void updateShiftRegister(){ digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, MSBFIRST, leds); digitalWrite(latchPin, HIGH); }