Hello
I've got another one that's really hurting and would dearly love some help as I've been struggling for about ten hours with this and am chasing my tail with what should quite simple things.
I've cobbled the following code together out of the larger sketch I'm working on to demonstrate my lack of understanding and dimness a little clearer.
Please can somebody help me ...
The "locking out" and "recalling last state" are the problems.
There are three toggling inputs with no bounce:
(pin A1)- Lock mode/ free mode
(pin A2)- RED led on/off
(pin A3)- GREEN led on/off
I have three LEDs representing three situations.
LED 1 is "lock" mode.(pin 3)
LED 2 RED (pin 5)
LED 3 GREEN (pin 6)
Rules:
When LED 1 is on , RED and GREEN must turn off (if they are on).
When LED 1 is on, RED and GREEN must not be able to turn on.
When LED 1 is turned off, the RED and GREEN must revert to the on/off state they were in before the LED 1 was turned on- and toggle on off from that.
Powers up in "unlock" mode with Red and Green switching on/off with first button presses.
I'm pretty upset with myself for not being able to solve this. I love writing sketches but honestly have some kind of dyslexia with it.
As I said, this sketch summarises the problem I'm having in a larger sketch so don't want to re-write in too much of a different way..and I know there are much shorter and more efficient ways. Simple to understand is key for me.
Any help truly appreciated.
int state = LOW;
int reading;
int previous = LOW;
int remlock = HIGH;
int stater = HIGH;
int readingr;
int previousr = LOW;
int stateg = HIGH;
int readingg;
int previousg = LOW;
void setup()
{
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
digitalWrite(A2, HIGH);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
digitalWrite(3, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
}
void loop() {
if (digitalRead(A0) == LOW) {
remote();
}
else if (digitalRead(A1) == LOW) {
red();
}
else if (digitalRead(A2) == LOW) {
green();
}
}
void remote () {
delay(1);
reading = digitalRead (A0);
if (reading == LOW && previous == HIGH) {
state = !state;
}
if (state == LOW)
{ digitalWrite(3, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
}
if (state == HIGH)
{ digitalWrite(3, 1);
digitalWrite(5, stater);
digitalWrite(6, stateg);
//digitalWrite(5, previousr);
//digitalWrite(6, previousg);
//digitalWrite(5, readingr);
//digitalWrite(6, readingg);
// none work
}
previous = reading;
}
void red() {
delay(1);
readingr = digitalRead (A1);
if (readingr == LOW && previousr == HIGH) {
stater = !stater;
}
if (stater == LOW) digitalWrite(5, HIGH);
if (stater == HIGH) digitalWrite(5, LOW);
previousr = readingr;
}
void green () {
delay(1);
readingg = digitalRead (A2);
if (readingg == LOW && previousg == HIGH) {
stateg = !stateg;
}
if (stateg == LOW) digitalWrite(6, HIGH);
if (stateg == HIGH) digitalWrite(6, LOW);
previousg = readingg;
}