-1

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");
 }
}
the busybee
2,4089 silver badges18 bronze badges
asked Nov 11, 2024 at 2:05
2
  • You could also consider using the else if construct instead of just if to prevent the simpler conditions overriding the previous conditions. if (blu == HIGH) overrides if (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. Commented Nov 11, 2024 at 10:43
  • your sketch is unnecessarily complicated ... you are keeping track of three flags, red, blue, grn ... all you need is one, a counter that counts 0 to 2 and repeats ... then light an LED in response to the value of the counter Commented Nov 11, 2024 at 23:00

1 Answer 1

2

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.

answered Nov 11, 2024 at 7:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.