I am fairly new to Ardunio and although doing a thorough search I have a problem I cannot solve.
Code below does not loop and and the last sub loop does not seem to work. Appreciate any help or guidance.
Regards
Ken
/*
Light LEDs on pins 2 to 7 sequentially, each after short delay and leaving all on.
Then turn all Leds off.
Follow by a reverse sequence (eg. Led on pin 7 down to led on pin 2)
Turn all Leds off.
Then loop to start.
*/
int timerS = 30 ;
int timerL = 300 ;
void setup() {
// use as loop to initialize each pin as an output:
for (int thisPin = 2; thisPin < 8; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
// Light up pin 2 to pin 7 with short delay:
for (int thisPin = 2; thisPin <= 8; thisPin++) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
// delay lighting the next LED:
delay(timerS);
}
//When all LEDS are HIGH, switch to LOW after long delay:
delay(timerL);
for (int thisPin = 2; thisPin <= 8; thisPin++) {
digitalWrite (thisPin, LOW);
}
// After long delay, switch LEDS on one at a time in reverse order:
delay(timerL);
for (int thisPin = 7; thisPin <= 8; thisPin–) {
// turn the pin on:
digitalWrite (thisPin, HIGH);
// delay lighting the next LED:
delay(timerS);
}
//Turn all LEDs LOW). NOTE: This sub loop below does not do what is intended and all Leds remain HIGH:
for (int thisPin = 2; thisPin <= 8; thisPin++) {
// turn the pin off:
digitalWrite(thisPin, LOW);
// Return to restart Loop ():
}
}
// After long delay, switch LEDS on one at a time in reverse order:
delay(timerL);
for (int thisPin = 7; thisPin <= 8; thisPin--) {
// turn the pin on:
digitalWrite (thisPin, HIGH);
// delay lighting the next LED:
delay(timerS);
}
This is not doing what you think it is doing (and some of your other “for” loops also have incorrect “conditions”).
/*
Light LEDs on pins 2 to 7 sequentially, each after short delay and leaving all on.
Then turn all Leds off.
Follow by a reverse sequence (eg. Led on pin 7 down to led on pin 2)
Turn all Leds off.
Then loop to start.
*/
int timerS = 30 ;
int timerL = 300 ;
void setup() {
// use as loop to initialize each pin as an output:
for (int thisPin = 2; thisPin < 8; ++thisPin) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
int thisPin; //since u are using this variable repeatedly maybe better to declare it at beginnig of loop
// Light up pin 2 to pin 7 with short delay:
for (thisPin = 2; thisPin < 8; ++thisPin) { //no need to used <=
// turn the pin on:
digitalWrite(thisPin, HIGH);
// delay lighting the next LED:
delay(timerS);
}
//When all LEDS are HIGH, switch to LOW after long delay:
delay(timerL);
for (thisPin = 2; thisPin < 8; thisPin++) {
digitalWrite (thisPin, LOW);
}
// After long delay, switch LEDS on one at a time in reverse order:
delay(timerL);
for (thisPin = 7; thisPin >1; --thisPin) { // since decrementing should be >= not <=
// turn the pin on:
digitalWrite (thisPin, HIGH);
// delay lighting the next LED:
delay(timerS);
}
//Turn all LEDs LOW). NOTE: This sub loop below does not do what is intended and all Leds remain HIGH:
for (thisPin = 2; thisPin < 8; thisPin++) {
// turn the pin off:
digitalWrite(thisPin, LOW);
// Return to restart Loop ():
}
}