 # help!!!

Hi I need some help understanding this code. Can someone tell me what the code does line by line

Thank you so much

https://create.arduino.cc/editor/trintyfearon/e404a3a2-d958-4fc6-9523-42091c52c91d/preview

Better read the how to use this forum posts first, post code in the proper manner as explained therein, and use a proper title. so sorry! I am freaking out over all my deadlines and didn't notice the post. I have looked through it and will re-post in the proper format.

This is going to be a problem:

``````byte ledPin[] = {4, 5, 6, 7, 8, 9};
...
for (int x=0; x<9; x++) {//
pinMode(ledPin[x], OUTPUT); }
``````

That array has a size of 6. Trying to access element 8 is bad news.

Jimmus:
This is going to be a problem:

``````byte ledPin[] = {4, 5, 6, 7, 8, 9};
``````

for (int x=0; x<9; x++) {//
pinMode(ledPin, OUTPUT); }

``````

That array has a size of 6. Trying to access element 8 is bad news.
``````

I think this would work and would be safer habit to get into…at least with array variables like this:

``````byte ledPin[] = {4, 5, 6, 7, 8, 9};
...
for (int x=0; x< sizeof ledPin; x++) {//
pinMode(ledPin[x], OUTPUT); }
``````

Using sizeof will always yield the correct array bound.

trintyfearon:
Hi I need some help understanding this code. Can someone tell me what the code does line by line

Thank you so much

Sure! No problem whatever!

``````// Create array for LED pins
``````

This is a comment, intended to explain something to the reader of the source code

``````byte ledPin[] = {4, 5, 6, 7, 8, 9};//creates a string of integers, each one is a light
``````

this declares a variable ‘ledPin’ which is an array of numbers. The array is initialized to some numbers that appear to be pin numbers.

``````int ledDelay;
``````

this declares an integer variable named ‘ledDelay’

``````int direction = 1;//there is one direction set as an integer
``````

this declares an integer variable named ‘direction’, an initializes it to 1

``````int currentLED = 0;
``````

This declares an integer variable named ‘currentLED’, and initializes it to zero.

``````unsigned long changeTime;
``````

This decalres an unsigened long variable named ‘changeTime’. Presumably, it will be assigned from millis() at some point. It’s a bit of a worry that this is an unsigned log, and that ledDelay - also a time probably in milliseconds - is only an unsigned int.

``````int potPin = 2;//descries the potentiometer as an integer with a value of 2
``````

This declares a variable named potPin, and initializes it to 2. Pretty much exacly like the comment says.

``````void setup()//sets up all the functions
``````

This declares a function named setup(). This function is special in the arduino programing environment. It is executed once when the board is powered up or reset.

``````{//the curly brackets make it a function
``````

This brace delimits the start of a block of code, being the code that is executed when setup() is invoked.

``````  for (int x=0; x<9; x++) {
``````

This declares a variable ‘x’, initializes it to 0, executes the following statement, increments x by 1, and keeps executing the statement and incrementing x for so long as x is less than 9. The following statement here is a block delimited by a staring and endng brace.

`````` pinMode(ledPin[x], OUTPUT); }
``````

Given the current value of x, this fetches the value with which ledPin was initialised above and sets the mode of that pin to OUTPUT. The closing brace delimits the end of the block controlled by the for statement above.

`````` changeTime = millis();
``````

this sets the changeTime variable to the current value of millis(), the number of milliseconds sonce the board was last reset.

``````}/*this section is about setting placeholders for the data, for example the second
``````

This brace delimits the end of the setup() function. There s also the start of a multiline comment that is intended to explain somethng to the person reading the code.

``````line says that each integer is a plugin, and that whatever is called "ledPin" will
``````

This is a continuation of that multi-line comment

``````be sent to those specific plugins*/
``````

This is the end of that multi-line comment.

``````void loop() {//makes it loop and repeat endlessly
``````

This declares a function named ‘loop’. loop() is special to the arduino - it is executed repeatedly for so long as the board is powered up (and not asleeep, obviously). The brace delimits the start of the function.

``````ledDelay = analogRead(potPin);//says that the delay between led lights is whatever the potentiometer directs,
through the analog
``````

This reads a value from the analog input specified in variable ‘potPin’, defined above. The value which will be a number between 0 and 1023 (inclusive) is put in the ledDelay variable.

`````` if ((millis() - changeTime) >//say that if
ledDelay) {
``````

This very badly formatted line is broken into two because it has a helpful one-line comment stuck in the middle of it. I apologize for not doing this ;line by line’, as you have asked, but hopefully you can manage.

This “line” asks the API how many miliseconds have elapsed since the board was reset, subtracts the value in changeTime, and checks that against the value in ledDelay. If changeTime was set previously to a value from millis(), tt will execute the block that it controlls only if the amount of milliseconds elapsed since changeTime was last set is greater than the value in ledDelay. The brace delimits the start of the block controlled by this if() statement.

`````` changeLED();
``````

This line invokes the changeLED functrion./ Presumably, that function changes the LED.

`````` changeTime = millis();
``````

this line sets changeTime to millis(), having the effect of keeping in thatr varibale the time at which the LEDs were last changed.

`````` }
``````

This brace delimts the end of the block controlled by the if statement

``````}
``````

This brace delimits the end of the loop() function

``````void changeLED() {
``````

This line declares a function named changeLED. The brace delimits the start of the function.

`````` for (int x=0; x<9; x++) {
``````

This declares a variable ‘x’, initializes it to 0, executes the following statement, increments x by 1, and keeps executing the statement and incrementing x for so long as x is less than 9. The following statement here is a block delimited by a staring and endng brace.

`````` digitalWrite(ledPin[x], LOW);
``````

This line writes a value of LOW to the pin whose number is contained in the x’th element of the ledPin array (zero indexed).

`````` }
``````

This line delimiots the end of the block controlled by the for() statement above.

`````` // turn on the current LED
``````

This line contains a helpful and useful comment

`````` digitalWrite(ledPin[currentLED],
HIGH);
``````

This badly formatted line writes a value of HIGH to the pin whose number is in the element of the ledPin array whose index is currentLED. Again, apologies for not dong this line-by-line as you have so reasonably and sensibly asked.

``````
``````

This is a blank line, intended to break up the code to make it easier for a person reading it.

`````` currentLED += direction;
``````

This adds the value of the ‘direction’ variable to the currentLED variable.

`````` if (currentLED == 9) {direction =
-1;}
``````

This badly formatted line (again with the weirdly split lines!) sets the value of the direction variable to -1 if the currentLED variable contains the value 9

`````` if (currentLED == 0) {direction = 1;}
``````

This line sets the value of the direction variable equal to 1 if the currentLED variable contains the value 0.

``````}
``````

This line delimits the end of the changeLED function.

Hope that helps!

Hi Paul Do you have more posts like this with explanations ?

ted:
Hi Paul
Do you have more posts like this with explanations ?

If you click on a members avatar, you can navigate to a link to all their posts.

Thanks, 9 pages of good stuff

thank you Paul for helping me understand. I'm new to all of this and feel a little less clueless now :). I now that asking for "line by line" was unnecessary.

trintyfearon: thank you Paul for helping me understand. I'm new to all of this and feel a little less clueless now :). I now that asking for "line by line" was unnecessary.

Sorry about the snark. I suppose that was unnecessary, too - you didn't know what you were asking.

What perhaps you actually needed was:

Line 2 contains a declaration of an array of 6 bytes, and initialises them to pin numbers. There will presumably be an led on each of these pins. curentLED is an index into this array. it is set initially to 0.

setup() runs through that array and sets each of those ins to being an output. It also sets 'change time' to the current value of millis.

loop is executed thousands of times every second. Each time, it reads a potentiometer, and treats the value in it (which will be a value from 0-1023) as a number of milliseconds. If the number of milliseconds between the current time and changeTime is >= this value, it calls changeLED() and updates changeTime. This has the effect of calling changeLED every X milliseconds, according to where the potentiometer is.

changeLED clears all of the LEDs, and then lights the LED whose pin is indexed by currentLED in the ledPin array. This has the effect of turning one LED on, as specified by currentLED.

It then updates current LED. To do this it bounces the number from end to end. It does this by holding a value 'direction', which indicates which way currentLED is moving, and by checking for a direction change when currentLED exceeds 9 (going up) or is less than 0 (going down). This is a bug - it will cause the value of currentLED to bounce between 0 and 9, but the ledPin array only has six elements. when currentLED is anywhere between 6 and 9, unexpected things may happen.

When currentLED is equal to (say) 7, changeLED will attempt to light the LED whose pin number is 7 bytes from the start of the ledPin array. But because the ledPin array is 6 elements long, this memory location could contain any old thing - we don't know. Consequently, changeLED will invoke digitalWrite with a pin number that could be pretty much anything. It might be some pin that the UNO doesn't have. It might be one of the other LEDs. It might be pin 0 or 1, which would interfere with serial communications.

The same comments apply to setup(), which also runs past the end of the array.