kpg
August 1, 2020, 10:30am
1
I am trying to make a simpler brake light program that when either the front or back brakes are used it just turns on the light but when both are used it flashes.
In setup BrakemillisPrev = 0; is set.
Basically It doesnt flash as expected.....I must be nesting somewhere in correctly?
void brakes() {
unsigned long BrakemillisNow;
Fbrakestat = digitalRead (FbrakeSw);
Rbrakestat = digitalRead (RbrakeSw);
int brakelightstat = LOW;
if (Fbrakestat == LOW || Rbrakestat == LOW) {
digitalWrite (brakeLightout, HIGH);
} else if ((Fbrakestat == LOW) && (Rbrakestat == LOW)) {
BrakemillisNow = millis();
(BrakemillisNow - BrakemillisPrev >= Brakeperiod);
BrakemillisPrev = BrakemillisNow;
if (brakelightstat == LOW) {
brakelightstat = HIGH;
} else {
brakelightstat = LOW;
}
digitalWrite (brakeLightout, brakelightstat);
}
else {
digitalWrite (brakeLightout, LOW);
}
}
Your if is the wrong way round. If both brakes are in use then front OR back is true too. Check the AND case first.
kpg
August 1, 2020, 11:19am
3
I'm checking the && now first and added a serial.print test to see the && condition is met which it is but it still doesnt seem to flash. Is my period check correct?
void brakes() {
unsigned long BrakemillisNow;
Fbrakestat = digitalRead (FbrakeSw);
Rbrakestat = digitalRead (RbrakeSw);
int brakelightstat = LOW;
if ((Fbrakestat == LOW) && (Rbrakestat == LOW)) {
BrakemillisNow = millis();
if (BrakemillisNow - BrakemillisPrev >= Brakeperiod){
Serial.println ("brakes period");
BrakemillisPrev = BrakemillisNow;
if (brakelightstat == LOW) {
brakelightstat = HIGH;
} else {
brakelightstat = LOW;
}
digitalWrite (brakeLightout, brakelightstat);
}
} else if (Fbrakestat == LOW || Rbrakestat == LOW) {
digitalWrite (brakeLightout, HIGH);
} else {
digitalWrite (brakeLightout, LOW);
}
}
pcbbc
August 1, 2020, 11:24am
4
Oooops semicolon?
if (BrakemillisNow - BrakemillisPrev >= Brakeperiod);{
Check you if (....) { .... } else { .... } syntax.
pcbbc
August 1, 2020, 11:27am
5
Also this...
int brakelightstat = LOW;
Declares a new variable, and always sets it to low each time through loop.
If you need a variable to retain its value, declare it as global. Then do not redeclare it using the “int” keyword again.
kpg
August 1, 2020, 11:35am
6
Of course ....thanks....Its part of a much bigger program and I think I went a bit snow blind!!
int brakelightstat = LOW;
Now a global and all good.
Thanks guys.