I know that I am missing something basic but cannot find the problem. The button works correctly. The board is Nano connect. Monitor readout is stuck on first line although obviously the whole thing is repeating as it should.
int var = 0;
int sens = 2;
int grn = 0;
int red = 0;
int blu = 0;
void setup() {
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(5, INPUT); //switch pulled low
pinMode(12, OUTPUT); // N/A
pinMode(11, OUTPUT); // N/A
pinMode(10, OUTPUT); // N/A
digitalWrite(3, HIGH); //supply to switch
grn = HIGH;
}
void loop() {
int button = 0;
button = digitalRead(5);
// Serial.println(button);
if (button == HIGH && blu == HIGH) {
blu = HIGH;
}
if (blu == HIGH) {
blu = LOW;
red = HIGH;
}
if (button == HIGH && red == HIGH) {
red = HIGH;
}
if (red == HIGH) {
red = LOW;
grn = HIGH;
}
if (button == HIGH && grn == HIGH) {
grn = HIGH;
}
if (grn == HIGH) {
grn = LOW;
blu = HIGH;
}
if (blu == HIGH) {
Serial.println("blu");
}
if (red == HIGH) {
Serial.println("red");
}
if (grn == HIGH) {
Serial.println("grn");
}
}
1 Answer 1
The reason is: Your loop logic is such that the conditions if (<color> == HIGH)
is always true. Before the serial output the variables hold these values:
blu
is HIGH;red
is LOW;grn
is LOW.
This is additionally independent of the level at button
.
If all variables were LOW, there is no path to set one HIGH. Since you start with grn
set to HIGH, this combination cannot be reached.
The following table shows this.
Source line | Remark | blu |
red |
grn |
---|---|---|---|---|
repeated from former loop | HIGH | LOW | LOW | |
if (button == HIGH && blu == HIGH) { |
does not matter | : | : | : |
blu = HIGH; |
executed or not | already HIGH | : | : |
} |
: | : | : | : |
if (blu == HIGH) { |
is true | : | : | : |
blu = LOW; |
executed | set LOW | : | : |
red = HIGH; |
executed | : | set HIGH | : |
} |
: | : | : | : |
if (button == HIGH && red == HIGH) { |
does not matter | : | : | : |
red = HIGH; |
executed or not | : | already HIGH | : |
} |
: | : | : | : |
if (red == HIGH) { |
is true | : | : | : |
red = LOW; |
executed | : | set LOW | : |
grn = HIGH; |
executed | : | : | set HIGH |
} |
: | : | : | : |
if (button == HIGH && grn == HIGH) { |
does not matter | : | : | : |
grn = HIGH; |
executed or not | : | : | already HIGH |
} |
: | : | : | : |
if (grn == HIGH) { |
is true | : | : | : |
grn = LOW; |
executed | : | : | set LOW |
blu = HIGH; |
executed | set HIGH | : | : |
} |
: | : | : | : |
When investigating issues like this, take pen and paper and play the role of the Arduino. Alternatively you can insert a lot of Serial.print()
to follow the values of the variables.
else if
construct instead of justif
to prevent the simpler conditions overriding the previous conditions.if (blu == HIGH)
overridesif (button == HIGH && blu == HIGH)
. Another approach, since you are dealing with only 4 boolean variables button, blue, red and grn is to have 16 conditional expression listing each possible combination of the those variable values and the action to be taken.