Choosing an Array and turning it all off in one time [SOLVED]

firt the code:

/*Alittle something for my dad's H0 modeltrain
*this is the light for a swing from the '50s
*based on a 8 LED kit from some web retailer.
*the code is simple, as were the Tivoli light in the '50s
************************************************************************/

 const int buttonPin = 12;    // the number of the pushbutton pin
//LED Pin Variables
int ledPins[] = {
2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; //An array to hold the pin each LED is connected to
                                   //i.e. LED #0 is connected to pin 2, LED #1, 3 and so on
                                   //to address an array use ledPins[0] this would equal 2
                                   //and ledPins[9] would equal 11
int pinCount = 10;                                   
 int delayTime = 350;
 int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

/***********************************************************************************
 * setup() - this function runs once when you turn your Arduino on
 * We the three control pins to outputs
 ******************************************************************************/
void setup() {
    //Set each pin connected to an LED to output mode (pulling high (on) or low (off)
 int i;
 for( int i=0; i < pinCount; i++){
      pinMode(ledPins[i],OUTPUT); //we use this to set each LED pin to output
  }
  pinMode(buttonPin, INPUT);
}
  /**************************************************************
  (commented code will not run)
   * these are the lines replaced by the for loop above they do exactly the
   * same thing the one above just uses less typing
  pinMode(ledPins[2],OUTPUT);
  pinMode(ledPins[3],OUTPUT);
  pinMode(ledPins[4],OUTPUT);
  pinMode(ledPins[5],OUTPUT);
  pinMode(ledPins[6],OUTPUT);
  pinMode(ledPins[7],OUTPUT);
  pinMode(ledPins[8],OUTPUT);
  pinMode(ledPins[9],OUTPUT);
  pinMode(ledPins[10],OUTPUT);
  pinMode(ledPins[11],OUTPUT);
  (end of commented code)
  **********************************************************************************/
  
 void oneAfterAnotherLoop(){
//  int delayTime = 350; //the time (in milliseconds) to pause between LEDs
                       //make smaller for quicker switching and larger for slower
 
//Turn Each LED on one after another
  for(int i = 0; i <= 9; i++){
    digitalWrite(ledPins[i], HIGH);  //Turns on LED #i each time this runs i
    delay(delayTime);                //gets one added to it so this will repeat 
  }                                  //8 times the first time i will = 0 the final
                                     //time i will equal 7;
//Turn Each LED off one after another
  for(int i = 9; i >= 0; i--){  //same as above but rather than starting at 0 and counting up
                                //we start at seven and count down
    digitalWrite(ledPins[i], LOW);  //Turns off LED #i each time this runs i
    delay(delayTime);                //gets one subtracted from it so this will repeat 
  }                                  //8 times the first time i will = 7 the final
                                     //time it will equal 0
 }
/*******************************************************************************************
*/
                                     
void slowonalloff(){
 // int delaytime = 350; //the time (in milliseconds) to pause between LEDs
                       //make smaller for quicker switching and larger for slower
//Turn each LED on one after another
 for(int i = 0; i <= 9; i++){
    digitalWrite(ledPins[i], HIGH);  
    delay(delayTime);                 
  }                                  
                                     
//Turn all LED off at one
for(int i = 0; i <pinCount; ){
digitalWrite(ledPins[i], LOW);        //Turns all LED off at once
delay(delayTime);
  }
}
/*************************************************************************************/

void LowToHigh(){ //This will run the light from the buttom to the top
  for(int i = 0; i < pinCount; i++) { 
    digitalWrite(ledPins[i], HIGH); //Will turn the LED on
    delay(delayTime);
  digitalWrite(ledPins[i], LOW); //Now we turn it off

  }
}

/**********************************************************************************/
void HighToLow(){ //This runs the light from the top to the buttom
  for(int i=pinCount - 1; i >=0; i--) { 
    digitalWrite(ledPins[i], HIGH); //Turn the LED on
    delay(delayTime); //Wait a little
  digitalWrite(ledPins[i], LOW);//And off again

  }
}
/****************************************************************************************
 * loop() - this function will start after setup finishes and then repeat
 * we call a function called oneAfterAnother().
*****************************************************************************************/
 
void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button 
  // (i.e. the input went from LOW to HIGH),  and you've waited 
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }
  
{                     // run over and over again
  slowonalloff();
}
}

The part that is the problem it this:

void slowonalloff(){
 // int delaytime = 350; //the time (in milliseconds) to pause between LEDs
                       //make smaller for quicker switching and larger for slower
//Turn each LED on one after another
 for(int i = 0; i <= 9; i++){
    digitalWrite(ledPins[i], HIGH);  
    delay(delayTime);                 
  }                                  
                                     
//Turn all LED off at one
for(int i = 0; i <pinCount; ){
digitalWrite(ledPins[i], LOW);        //Turns all LED off at once
delay(delayTime);
  }
}

When I power up, it goes on as it should, but only the first pin goes off and then the program freezes.
I have been looking all over after a solution to this little but BIG problem.
I'm sure it's just something I missed but please help me understand what?

 for(int i = 0; i <= 9; i++){
    digitalWrite(ledPins[i], HIGH);  
    delay(delayTime);                 
  }                                  
                                     
//Turn all LED off at one
for(int i = 0; i <pinCount; ){
digitalWrite(ledPins[i], LOW);        //Turns all LED off at once
delay(delayTime);
  }

Take a good long look at those two loops and see what the difference(s) between them are... It'd be better to write them identically...

What to you see if you print the value of i and ledPins[ i ] in each of the for loops ?

Hint

for(int i = 0; i <pinCount; ){

I have tried a lot af different coding, but can't get it to do as I like...
If you can just, point me in the right direction??

Turning LEDs on

for(int i = 0; i <= 9; i++){{

Turning LEDs off

for(int i = 0; i <pinCount; ){

Spot the difference ........

I tried both with

//Turn each LED on one after another
 for(int i = 0; i <= pinCount; i++){
    digitalWrite(ledPins[i], HIGH);  
    delay(delayTime);                 
  }                                  
                                     
//Turn all LED off at one
for(int i = 0; i <pinCount; i++){
digitalWrite(ledPins[i], LOW);        //Turns all LED off at once
delay(delayTime);
  }
}

and

//Turn each LED on one after another
 for(int i = 0; i <= pinCount; i++){
    digitalWrite(ledPins[i], HIGH);  
    delay(delayTime);                 
  }                                  
                                     
//Turn all LED off at one
for(int i = 0; i <pinCount; i){
digitalWrite(ledPins[i], LOW);        //Turns all LED off at once
delay(delayTime);
  }
}

The first one turns on one after another,just like it should, and turns off one anohter another. It's beautiful, but not what I'm looking for.
The second one turns on, one after another, just like it should, but only turns on the first one and freezes.
I would like it all to turn off at the same time, after delayTime.
Think that such a little thing is so difficult, but just what I want :slight_smile:

I would like it all to turn off at the same time, after delayTime.

Once you have got the loop structure sorted out and they turn off one after another with a delay between each one, if you really want them all to turn off at the same time (or so nearly the same time that you will not be able to tell) take the delay out of the for loop.

since you're kinda a n00b programmer, i suggest you draw a flow chart before you actually write the code.

the code won't run as what you THOUGHT, they run as what you've WRITTEN.

ok So I got

void slowonalloff(){
 // int delaytime = 350; //the time (in milliseconds) to pause between LEDs
                       //make smaller for quicker switching and larger for slower
//Turn each LED on one after another
 for(int i = 0; i <= pinCount; i++){
    digitalWrite(ledPins[i], HIGH);  
    delay(delayTime);                 
  }                                  
                                     
//Turn all LED off at one
for(int i = 0; i <pinCount;i++ ){
digitalWrite(ledPins[i], LOW);        //Turns all LED off at once
  }
  {
  delay(delayTime);
}
}

Thanks to UKHeliBob

Once you have got the loop structure sorted out and they turn off one after another with a delay between each one, if you really want them all to turn off at the same time (or so nearly the same time that you will not be able to tell) take the delay out of the for loop.

I love you almost as much as i love my girl :slight_smile:
Of cause I need to move the delayTime a little :slight_smile:

And Palatis

since you're kinda a n00b programmer, i suggest you draw a flow chart before you actually write the code.

What is that?

for(int i = 0; i <= pinCount; i++){
    digitalWrite(ledPins[i], HIGH);

Doesn't that look wrong?

Doesn't that look wrong?

Why does it look wrong??
It works like a charm :slight_smile:

for(int i = 0; i <= pinCount; i++){
...
for(int i = 0; i <pinCount;i++ ){

Once again, spot the difference?

It may be "working" to your eyes but it ain't right, one of them overruns the ledPins array.


Rob

Ahh ok, now I see :slight_smile:
I have one = too much in the upper one.

It's all in the details, little things like an extra = or no i++ matter. After a while you will (or at least should) start to pick up on these things.


Rob

Yes I should, and I think that I will, but for my second project I think that I have learned a lot. I'm starting to understand what happens in the code and how to make things possible.
I may be a noob still, but the way you guys can explain stuff, it helps a lot and makes it fun to keep on going and coming up with new ideas :slight_smile:
I started with this arduino stuff because of my aquarium and now I have a vision of building a robot some time in the future. When I have a lot more experience with this :slight_smile:

It works like a charm

Meaning "I got away with it this time, but I'll wonder why the next time I write a construct like that, it crashes my sketch"

It's useful to learn and reinforce good habits early on.

It sure is, and learning it the RIGHT way from the start is the ONLY way to learn it :slight_smile: But with such good mentors as you guys, I'm confident it can't go wrong :slight_smile:
I'm buying the "Arduino for Dummies" book, to have something to read in the bed, and I hope it will learn me a lot.
I'm a little obsessed with the "smart" code, code that can set up it self, store stuff and use it later on and check if it does it right, and if it does'ent, do it right then.

WARNING: Programming is very addictive, be prepared to "just want to get one more thing working" at 2AM :slight_smile:


Rob

WARNING: Programming is very addictive, be prepared to "just want to get one more thing working" at 2AM

I'm already past that, If I go to bed at 1AM, I can't sleep because I have a little thing I want to try.

I've written the button code now, just need to find a good and smart way to switch between the different types of LED functions...
And to change it from the PC would be nice as well...