Array Challenge: No Delay – Kevin Chiasson

This question comes from Kevin Chiasson:

  • Arduino Course for Absolute Beginners 2nd Edition
    • Using Arrays
      • Array Basics Part 1
        • Challenge 6: Add an array that will store 4 different delays times.  Use this array to have a different delay period between each LED turning on and off.

Question

“Hello Mike,  I’m stuck on the “Array Challenge”.  I can’t seem to get the delay to change in the array.  The delay does not change no matter what values I put in the delay array.  The lights just keep blinking at a steady pace.  Any help would be appreciated.
Thanks”
/*A sketch to deomonstrate array declaration
   initialization, and referenceing element in an array

   in other words
*/
const byte delayp = 4;
const byte LEDpinArraySize = 5;
//declare and initialize an array to hold pin numbers

byte LEDpinArray[LEDpinArraySize] = {2, 4, 7, 9, 11};
byte delayPeriod[delayp] = {2000, 1500, 2000, 200};

//set some delay times for blinking the LEDs
//int longerDelayTime = 1000;
//int shorterDelayTime = 20;

void setup() {
  // set the modes of the pins in the array to outputs
  pinMode(LEDpinArray[0], OUTPUT);
  pinMode(LEDpinArray[1], OUTPUT);
  pinMode(LEDpinArray[2], OUTPUT);
  pinMode(LEDpinArray[3], OUTPUT);
  pinMode(LEDpinArray[4], OUTPUT);

}

void loop() {
  // Turn off all the LedS
  digitalWrite(LEDpinArray[0], LOW);
  digitalWrite(LEDpinArray[1], LOW);
  digitalWrite(LEDpinArray[2], LOW);
  digitalWrite(LEDpinArray[3], LOW);
  digitalWrite(LEDpinArray[4], LOW);

  //Delay for a moment to see the LEDs stay OFF
  delay(delayPeriod[0]);

  //turn on all the LEDs, one by one
  digitalWrite(LEDpinArray[4], HIGH);


  digitalWrite(LEDpinArray[3], HIGH);
  delay(delayPeriod[1]);

  digitalWrite(LEDpinArray[2], HIGH);
  delay(delayPeriod[2]);

  digitalWrite(LEDpinArray[1], HIGH);
  delay(delayPeriod[3]);

  digitalWrite(LEDpinArray[0], HIGH);
  delay(delayPeriod [0]);

}

 

Your Challenge

Find the error.

Hints

Look at the array declarations.

Solution

[fvplayer src=”https://vimeo.com/343470912″ transcript=”auto” splash=”https://i.vimeocdn.com/video/792629227_1280x720.jpg?r=pad” caption=”Array Challenge – No Delay – Kevin Chiasson”]

Your Thoughts

This can be a tough one to spot!  What’s really tricky about mismatching the data-type is that sometimes the behavior matches what you might expect, and other times it doesn’t.  
Has something like this ever happened to you?

8 Comments

  1. A. Smith on November 12, 2019 at 2:28 pm

    Hi Mike,

    What threw me off is the sketch shown just after Kevin’s question at the top. It already shows the delay array as an int. Judging by your answer video, it was originally declared a byte. May I suggest putting the original error in the above sketch.

    All the best,
    A. Smith

    • Daniel Hester on August 29, 2020 at 4:37 pm

      agree… put line back to byte…

      • Michael Cheich on August 29, 2020 at 5:29 pm

        Thanks gentleman! I “corrected” it back to byte.

  2. Harvey King on March 22, 2021 at 4:10 pm

    I thought maybe that byte was too small for most of the time periods Kevin had in his array and that he needed to use int instead.

  3. Harvey King on March 22, 2021 at 4:13 pm

    Nice code tho; used all of the options the lesson presented… unlike my (lazy?) tendency to bypass those opportunities.

  4. Richie Devereux on August 18, 2021 at 3:32 pm

    I’m gonna say the problem is the delayPeriod has a byte data type, which needs to be an int.

  5. David Faulkner on October 10, 2021 at 7:26 am

    Thanks I needed help with that one. all working fine now!

  6. Javier Sanchez on December 28, 2024 at 10:03 pm

    Byte =0-255 can not contain numbers greater than that.
    corrected will be:
    byte LEDpinArray[LEDpinArraySize] = {2, 4, 7, 9, 11}; // can be byte
    int delayPeriod[delayp] = {500, 1500, 3000, 200}; // those numbers of delay need an integer not a byte

Leave a Comment