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:
all the leds are turned off,
button pressed > the leds turn on one after the other with the delay
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
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
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?
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);
}
}
}