Skip to content

Instantly share code, notes, and snippets.

@zach-c-d
Created December 13, 2017 22:45
Show Gist options
  • Save zach-c-d/fdad5106850e4b74ebe6723bee6de171 to your computer and use it in GitHub Desktop.
Save zach-c-d/fdad5106850e4b74ebe6723bee6de171 to your computer and use it in GitHub Desktop.
//time the interval lengths
unsigned long prevMillis = 0;
unsigned long prevMillisButtonCheck = 0;
unsigned long prevMillisLEDCheck = 0;
unsigned long currentMillis;
//checks if button has been hit,
//if analogRead(button) exceeds the threshold
boolean buttonCheck = false;
int threshold = 500;
int LEDChoice;
bool greenLEDWasLit = false;
//keep track of the gameloop Interval
//count 0 = display LED
//count 1 = listen for input
//count 2 = display result
//count 3 = return to beginning
int count = 0;
//I/O
int button0 = A0;
////////////////
int LED0 = 7;
int LED1 = 8;
void setup(){
//initialize the pad
pinMode(button0, INPUT);
//initialize the LEDs
pinMode(LED0, OUTPUT);
pinMode(LED1, OUTPUT);
//initialize serial communication
Serial.begin(9600);
}
void loop(){
//set the currentMillis variable to the current time
currentMillis = millis();
if(count == 0){
displayLED();
} else if(count == 1){
//check if pad has been hit, light the LED
//check if the pad has been released, dim the LED
checkPadHit();
} else if(count == 2){
evaluatePadhit();
}
//check if the time Interval has ended
//reset prevMillis to reset the interval
checkIntervalEnd();
}
void displayLED(){
LEDChoice = random(0,2);
if (LEDChoice == 0){
digitalWrite(LED0, HIGH);
} else if(LEDChoice == 1){
digitalWrite(LED1, HIGH);
}
count += 1;
}
void checkPadHit(){
//record if the button is pushed
if(analogRead(button0) >= threshold){
buttonCheck = true;
//light the green LED
digitalWrite(LED1, HIGH);
}
//if 30 milliseconds have passed,
//and the button is released,
//and the butotn has been previously pushed
if(currentMillis - prevMillisButtonCheck >=30
&& threshold > analogRead(button0)
&& buttonCheck == true)
{
count++;
buttonCheck = false;
greenLEDWasLit = true;
prevMillisButtonCheck = currentMillis;
//dim the green LED
digitalWrite(LED1, LOW);
}
}
void evaluatePadhit(){
if(greenLEDWasLit
&& LEDChoice == 0){
currentMillis = millis();
prevMillisLEDCheck = currentMillis;
if(currentMillis - prevMillisLEDCheck >=30){
digitalWrite(LED0, HIGH);
digitalWrite(LED1, HIGH);
}
if(currentMillis - prevMillisLEDCheck >=60){
digitalWrite(LED0, LOW);
digitalWrite(LED1, LOW);
}
if(currentMillis - prevMillisLEDCheck >=90){
digitalWrite(LED0, HIGH);
digitalWrite(LED1, HIGH);
}
if(currentMillis - prevMillisLEDCheck >=120){
digitalWrite(LED0, LOW);
digitalWrite(LED1, LOW);
}
}
}
void checkIntervalEnd(){
//at the end of an interval
if(currentMillis - prevMillis >= 1000){
if(count == 0){
digitalWrite(LED0, LOW);
digitalWrite(LED1, LOW);
}
Serial.print('gameloop : ');
Serial.println(count);
Serial.print('buttonRead : ');
Serial.println(analogRead(button0));
count ++;
prevMillis = currentMillis;
}
}
void checkGameLoopEnd(){
if (count == 3){
count = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment