// === Alex Kaiser ====
// Module #3 project
const int NUM_BTNS = 2;
const int BTN_PINS[] = {34, 35}; // buttons on input only pins
const int RED_LED_PINS[] = {14, 25}; // N/S, E/W
const int YEL_LED_PINS[] = {12, 26};
const int GRN_LED_PINS[] = {13, 27};
const int STATE_DELAYS[] = {5000, 2000, 5000, 2000};
int state = 0;
unsigned long prevTime = 0;
int btnState[NUM_BTNS];
int oldBtnState[NUM_BTNS]; // or set HIGH
// function returns which button was pressed, or 0 if none
int checkButtons() {
int btnPressed = 0;
for (int i = 0; i < NUM_BTNS; i++) {
// check each button
btnState[i] = digitalRead(BTN_PINS[i]);
if (btnState[i] != oldBtnState[i]) { // if it changed
oldBtnState[i] = btnState[i]; // remember state for next time
if (btnState[i] == LOW) { // was just pressed
btnPressed = i + 1;
//Serial.print("Button ");
//Serial.print(i + 1);
//Serial.println(" pressed");
}
delay(20); // debounce
}
}
return btnPressed;
}
void doState(int state) {
for (int i = 0; i < 2; i++) {
digitalWrite(RED_LED_PINS[i], LOW);
digitalWrite(YEL_LED_PINS[i], LOW);
digitalWrite(GRN_LED_PINS[i], LOW);
}
if (state == 0) {
digitalWrite(RED_LED_PINS[0], HIGH);
digitalWrite(GRN_LED_PINS[1], HIGH);
} else if (state == 1) {
digitalWrite(YEL_LED_PINS[1], HIGH);
digitalWrite(RED_LED_PINS[0], HIGH);
} else if (state == 2) {
digitalWrite(RED_LED_PINS[1], HIGH);
digitalWrite(GRN_LED_PINS[0], HIGH);
} else if (state == 3) {
digitalWrite(YEL_LED_PINS[0], HIGH);
digitalWrite(RED_LED_PINS[1], HIGH);
}
}
void setup() {
Serial.begin(115200);
for (int i = 0; i < 2; i++) {
pinMode(BTN_PINS[i], INPUT_PULLUP);
pinMode(RED_LED_PINS[i], OUTPUT);
pinMode(YEL_LED_PINS[i], OUTPUT);
pinMode(GRN_LED_PINS[i], OUTPUT);
}
Serial.println("Running");
prevTime = millis();
doState(0);
}
void loop() {
int btnNumber = checkButtons();
if (btnNumber == 1) {
state = 3;
prevTime = millis();
doState(state);
}
if (btnNumber == 2) {
state = 1;
prevTime = millis();
doState(state);
}
if (millis() - prevTime >= STATE_DELAYS[state]) {
prevTime = millis();
state++;
if (state == 4) state = 0;
Serial.println(state);
doState(state);
}
}
/*
void loop() {
// The next three lines of code turn on the red LED1
digitalWrite(RED_LED_PINS[0], HIGH); // This should turn on the RED LED1
digitalWrite(YEL_LED_PINS[0], LOW); // This should turn off the YELLOW LED1
digitalWrite(GRN_LED_PINS[0], LOW); // This should turn off the GREEN LED1
delay(2000); // wait for 2 seconds
// The next three lines of code turn on the green LED1
digitalWrite(RED_LED_PINS[0], LOW); // This should turn off the RED LED1
digitalWrite(YEL_LED_PINS[0], LOW); // This should turn off the YELLOW LED1
digitalWrite(GRN_LED_PINS[0], HIGH); // This should turn on the GREEN LED1
delay(2000); // wait for 2 seconds
// The next three lines of code turn on the yellow LED1
digitalWrite(RED_LED_PINS[0], LOW); // This should turn off the RED LED1
digitalWrite(YEL_LED_PINS[0], HIGH); // This should turn on the YELLOW LED1
digitalWrite(GRN_LED_PINS[0], LOW); // This should turn off the GREEN LED1
delay(2000); // wait for 2 seconds
}
*/
North / South
East / West
N/S
E/W