[solved]Question about switchState

Hi, I'm new to this forum and to Arduino (Got my starter kit a week ago). As a way to practice Lesson 1 to 3, I wanted to make a row of leds to turn one after the other with the pulse of a button, and then go off

My code is this


int switchState = 0;
void setup() {
pinMode(2,INPUT);
for(int pinNumber = 2;pinNumber<=11;pinNumber++)
pinMode(pinNumber,OUTPUT);
}

void loop() {
//read state of pin 2
switchState = digitalRead(2);
//evaluate state of switch
if (switchState == LOW)

//loop sending LOW signal to leds (pin 3 to 11) if switch is not pulsed
{for (int offPin = 2;offPin<=11;offPin++){digitalWrite(offPin,LOW);}

//if switchState is High (button pulsed) then send HIGH signal to pins 3 to 11
}else{for (int onPin = 2;onPin<=11;onPin++){digitalWrite(onPin,HIGH);delay(250);}
}}

I've made two loops, to simplify the task of repeating the same instruction 9 times. But in reality, once the code is loaded to the arduino this is the behavior:

  1. all the leds are turned off,
  2. button pressed > the leds turn on one after the other with the delay
  3. all the leds remain on

if the button is pressed once, the leds shouldn't go back to off once all of them are on?

if I reverse the orders en the if function the leds go

  1. all on
  2. off one after another
  3. repeat 1 and 2 until I press the button

Please expand those "all in one lines". They are universally reviled. Use conventional C style. Only then will I read your code.


int switchState = 0;
void setup() {
pinMode(2,INPUT);
for(int pinNumber = 2;pinNumber<=11;pinNumber++)
pinMode(pinNumber,OUTPUT);
}

void loop() {
//read state of pin 2
switchState = digitalRead(2);

//evaluate state of switch
if (switchState == LOW)

//loop sending LOW signal to leds (pin 3 to 11) if switch is not pulsed
{for (int offPin = 2;offPin<=11;offPin++)
  {digitalWrite(offPin,HIGH);
  }

//if switchState is High (button pulsed) then send HIGH signal to pins 3 to 11
}
else{
    for (int onPin = 2;onPin<=11;onPin++)
    {
     digitalWrite(onPin,LOW);
     delay(250);
    }
}
}

It's ok like that? Sorry, new to all the formatting :sweat:

How are your switches wired? The way your code is written you would have to hold the button down to keep them on (assuming you have a pulldown resistor), because as soon as you let the button go they would turn off. If they are wired with a pull-up resistor then the opposite would be true.

Do you realize in setup() you are configuring pin 2 as INPUT and immediately configuring it as OUTPUT?

Do you realize in your loops you are doing a digital write to pin 2?

1 Like

If you configure the button pin with INPUT_PULLUP you can use the internal pullup resistor, then you won't need an external pullup/pulldown resistor.

1 Like

oh, what a stupid mistake! thats all. The wiring is ok, a pull-down resistor. Now works fine

Thanks!

Also don't do a digital write to input 2 either. It changes the mode to INPUT_PULLUP.

Thanks! I'll modify that

Thanks for the edit. Once you have done the basic formatting, you can also auto-format at any time, it auto-indents for you and that can help you find typos. It's ctrl-T in the IDE or Tools->Auto Format in the menu. Here is the result of using it:

int switchState = 0;
void setup() {
  pinMode(2, INPUT);
  for (int pinNumber = 2; pinNumber <= 11; pinNumber++)
    pinMode(pinNumber, OUTPUT);
}

void loop() {
  //read state of pin 2
  switchState = digitalRead(2);

  //evaluate state of switch
  if (switchState == LOW)

    //loop sending LOW signal to leds (pin 3 to 11) if switch is not pulsed
  { for (int offPin = 2; offPin <= 11; offPin++)
    { digitalWrite(offPin, HIGH);
    }

    //if switchState is High (button pulsed) then send HIGH signal to pins 3 to 11
  }
  else {
    for (int onPin = 2; onPin <= 11; onPin++)
    {
      digitalWrite(onPin, LOW);
      delay(250);
    }
  }
}

but I would unfold and regroup a few more brackets more conventionally like:

int switchState = 0;
void setup() {
  pinMode(2, INPUT);
  for (int pinNumber = 2; pinNumber <= 11; pinNumber++)
    pinMode(pinNumber, OUTPUT);
}

void loop() {
  //read state of pin 2
  switchState = digitalRead(2);

  //evaluate state of switch
  if (switchState == LOW) {
    //loop sending LOW signal to leds (pin 3 to 11) if switch is not pulsed
    for (int offPin = 2; offPin <= 11; offPin++) {
      digitalWrite(offPin, HIGH);
    }
  }
  //if switchState is High (button pulsed) then send HIGH signal to pins 3 to 11
  else {
    for (int onPin = 2; onPin <= 11; onPin++) {
      digitalWrite(onPin, LOW);
      delay(250);
    }
  }
}

See the difference?

1 Like

Thanks for the tips! clearly is more readable in this way :slightly_smiling_face:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.