Go Down

Topic: Program Delay Action (Read 16556 times) previous topic - next topic

geisty

Hi people,

I am new to electronics and my knowledge base is relatively limited so I am not sure this is proper place to ask my question, if so I apologize in advance.

I recently built a simple circuit using arrays and for loops and I came across this problem which has me baffled.

I am using a for loop to set the pins for the array as outputs and other for loop to simply turn on and then off a series of leds.

The array runs for a period of time fine, in my case roughly 13 times, before all the leds remain on for a pro-long period of time(roughly 8 seconds then turns off, wait roughly a second of so then turns all the less on and the hesitation starts over eventually leaving all the less lite.

I have no idea why this is happening since the code seems simple enough, can anyone tell me why this is happening?

Assistance is greatly appreciated.


my code:

int ledArray[] = {2,3,4,5,6,7}; // pins in the array
int Time = 75; // sets the on and off time

void setup() {
  // initialize digital pins 2 through 7 as an output

  for(int i; i <6; i++)
  {
    pinMode(ledArray, OUTPUT);
  }
}

  // turns on leds in sequence
  void loop()
 {
    for (int i=0; i<=6; i++) {
      digitalWrite(ledArray, HIGH);
      delay(Time);
  }

    for (int i=6; i>=0; i--)  // turns off the leds in sequence
  {
      digitalWrite(ledArray, LOW);
      delay(Time);
    }

    delay(1000);
  }

CrossRoads

#1
May 20, 2020, 06:43 pm Last Edit: May 20, 2020, 06:45 pm by CrossRoads
The code is missing the array indexing []

Code: [Select]

pinMode(ledArray[i], OUTPUT);

digitalWrite(ledArray[i], HIGH);

digitalWrite(ledArray[i], LOW);

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

geisty

Thanks for your response Cross Roads.

I changed the code as suggested(see revised code below) and although the run time tripled, the end result is the same, the code runs for a period of time ending with the leds remain on and the sequence stopped. It's got my head hurting :). I have tried changing boards from a nano, uno, and mega, the results are the same. Any other suggestions for why this might be happening?

Your assistance is greatly appreciated.


Revised code with indexing:

int ledArray[] = {2,3,4,5,6,7}; // pins in the array
int Time = 75; // sets the on and off time

void setup() {
  // initialize digital pins 2 through 7 as an output

  for(int i; i <6; i++)
  {
    pinMode(ledArray, OUTPUT);
  }
}

  // turns on leds in sequence
  void loop() {
    for (int i=0; i<=6; i++) {
      digitalWrite(ledArray, HIGH);
      delay(Time);
    }

    for (int i=6; i>=0; i--)  // turns off the leds in sequence
    {
      digitalWrite(ledArray, LOW);
      delay(Time);
    }
    delay(1000);
  }

Go Up