Skip to content

Instantly share code, notes, and snippets.

@niradler
Created May 26, 2018 13:02
Show Gist options
  • Save niradler/cecc26ca05cc53d0c89c4ecda07cbd41 to your computer and use it in GitHub Desktop.
Save niradler/cecc26ca05cc53d0c89c4ecda07cbd41 to your computer and use it in GitHub Desktop.
int winStates[] = {1, 1, 2, 3, 3, 3, 3};
int states[] = {0, 0, 0, 0, 0, 0, 0};
int s[] = {0, 0};
int loc = 0 ;
int s_loc = 0 ;
int s_isMatch = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(6, INPUT);
pinMode(8, INPUT);
pinMode(4, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int buttonState1 = digitalRead(6);
int buttonState2 = digitalRead(8);
int buttonState3 = digitalRead(4);
if (buttonState1 == 1) {
Serial.println("buttonState1");
updateState(1, buttonState1);
waitToLow(6);
}
else if (buttonState2 == 1) {
Serial.println("buttonState2");
updateState(2, buttonState2);
waitToLow(8);
}
else if (buttonState3 == 1) {
Serial.println("buttonState3");
updateState(3, buttonState3);
waitToLow(4);
} else {
updateState(1, 0);
updateState(2, 0);
updateState(3, 0);
}
delay(1); // delay in between reads for stability
}
void waitToLow(int pin) {
while (digitalRead(pin) == 1) {
delay(100);
}
delay(100);
}
void updateState(int btn, int state) {
if (state == 1) {
int isFirst = 0;
s[s_loc] = btn;
s_loc = s_loc + 1;
if (s_loc > 1) {
s_loc = 0;
}
if (s_isMatch == 0) {
s_isMatch = 1;
for (int i = 0; i < 2; i++) {
Serial.print(s[i]);
Serial.print(",");
if (s[i] != 1) {
s_isMatch = 0;
}
}
if (s_isMatch == 1) {
isFirst = 1;
}
Serial.println("");
}
if (s_isMatch == 1) {
Serial.println("s_ match");
if(isFirst == 1){
states[0] = 1;
loc=1;
}
states[loc] = btn;
loc = loc + 1;
if (loc > 6) {
loc = 0;
s_isMatch = 0;
}
int isMatch = 1;
for (int i = 0; i < 7; i++) {
Serial.print(states[i]);
Serial.print(",");
if (winStates[i] != states[i]) {
isMatch = 0;
}
}
Serial.println("");
if (isMatch == 1) {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000);
} else {
digitalWrite(13, LOW); // turn the LED on (HIGH is the voltage level)
}
}
} else {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment