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.
- Array Basics Part 1
- Using Arrays
Question
/*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”]

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
agree… put line back to byte…
Thanks gentleman! I “corrected” it back to byte.
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.
Nice code tho; used all of the options the lesson presented… unlike my (lazy?) tendency to bypass those opportunities.
I’m gonna say the problem is the delayPeriod has a byte data type, which needs to be an int.
Thanks I needed help with that one. all working fine now!
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