I have the following Code but i have problems with the pedestrian Lights.
The first const boolean block is for the reedkontacts on the ground (K1) this work very well but now i want when i press the button 1 or 2 the second block of the const boolean will be process.
How can i fix this problem?
regards
/*
* Arduino Projekt Ampelsteuerung 2016
*/
// Digital pin no.
//Ampel means Car Light
//Fußgänger is pedestrian Lights
#define AR1 23 // Ampel 1 RED
#define AY1 25 // Ampel 1 YELLOW
#define AG1 27 // Amepl 1 GREEN
#define AR2 29 // Ampel 2 RED
#define AY2 31 // Ampel 2 YELLOW
#define AG2 33 // Ampel 2 GREEN
#define FR1 35 // Fußgänger 1 RED
#define FG1 37 // Fußgänger 1 GREEN
#define FR2 39 // Fußgänger 2 RED
#define FG2 41 // Fußgänger 2 GREEN
#define Button2 2 // Taster Fußgänger 1
#define Button3 3 // Taster Fußgänger 2
#define K1 18 // Reedkontakt 1
#define K2 19 // Reedkontakt 2
const int numLights = 10;
const int lights[] = { AR1, AY1, AG1, FR1, FG1, AR2, AY2, AG2, FR2, FG2 };
// Zeiten von den Ampelphasen
const int numStates = 6;
const double stateSeconds[] = { 10.0, 3.0, 3.0, 10.0, 3.0, 3.0 };
// Ampelphasen deklariert
const boolean states[][numLights] = {
// AR1 AY1 AG1 FR1 FG1 AR2 AY2 AG2 FR2 FG2
{ LOW, LOW, HIGH, HIGH, LOW, HIGH, LOW, LOW, HIGH, LOW },
{ LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, LOW, HIGH, LOW },
{ HIGH, LOW, LOW, HIGH, LOW, HIGH, HIGH, LOW, HIGH, LOW },
{ HIGH, LOW, LOW, LOW, LOW, LOW, LOW, HIGH, HIGH, LOW },
{ HIGH, LOW, LOW, HIGH, LOW, LOW, HIGH, LOW, HIGH, LOW },
{ HIGH, HIGH, LOW, HIGH, LOW, HIGH, LOW, LOW, HIGH, LOW },
// AR1 AY1 AG1 FR1 FG1 AR2 AY2 AG2 FR2 FG2
{ LOW, LOW, HIGH, HIGH, LOW, LOW, LOW, HIGH, HIGH, LOW },
{ LOW, HIGH, LOW, HIGH, LOW, LOW, HIGH, LOW, HIGH, LOW },
{ HIGH, LOW, LOW, HIGH, LOW, HIGH, LOW, LOW, HIGH, LOW },
{ HIGH, LOW, LOW, LOW, HIGH, HIGH, LOW, LOW, LOW, HIGH },
{ HIGH, LOW, LOW, HIGH, LOW, HIGH, LOW, LOW, HIGH, LOW },
{ HIGH, HIGH, LOW, HIGH, LOW, HIGH, HIGH, LOW, HIGH, LOW },
};
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean buttonPressed = false;
long savedMillis = millis();
int state = 0; // Initial state is state 0; it's also the default state
int state2 = 0; // Initial state is state 0; it's also the default state
void setup() {
// Taster und Reedkontakt als INPUT
pinMode(Taster1, INPUT);
pinMode(Taster2, INPUT);
pinMode(K1, INPUT);
pinMode(K2, INPUT);
// LED's als OUTPUT
for (int i = 0; i < numLights; i++) {
pinMode(lights[i], OUTPUT);
}
}
void loop() {
setLights(state);
if (0 == state) {
if (!buttonPressed) {
// Monitor button presses only in state 0
currentButton = digitalRead(K1);
if (currentButton != lastButton) {
buttonPressed = true;
} else {
lastButton = currentButton;
}
} else {
state = nextState(state);
}
} else {
buttonPressed = false;
state = nextState(state);
}
setLights(state);
if (0 == state) {
if (!buttonPressed) {
// Monitor button presses only in state 0
currentButton = digitalRead(Button2) || digitalRead(Button3);
if (currentButton != lastButton) {
buttonPressed = true;
} else {
lastButton = currentButton;
}
} else {
state2 = nextState(state);
}
} else {
buttonPressed = false;
state2 = nextState(state);
}
}
// Sets the lights according the state provided
void setLights(int s) {
for (int i = 0; i < numLights ; i++) {
digitalWrite(lights[i], states[s][i]);
}
}
// Returns the state for the next loop cycle
int nextState(int s) {
long currentMillis = millis();
if (currentMillis - savedMillis > 1000.0 * stateSeconds[s]) {
savedMillis = currentMillis;
s = (s + 1) % numStates;
}
return s;
}