Beginner's question

Hello

Im new to arduino and im learning from arduino for newbies. I have a question on the code on a lesson which is :

//Chapter 4: The All-Seeing Eye
//Sequentially lights up a series of LEDs
//A variable to set a delay time between each LED
int delayTime = 400;

//A variable to store which LED we are currently working on //it will also start with LED 4 (position 5)
int currentLED = 4;

//A variable to store the direction of travel
int dir = 1;

//A variable to store the last time we changed something
long timeChanged = 0;

//An array to hold the value for each LED pin
byte ledPin[] = {4,5,6,7,8,9,10,11,12,13};

void setup(){
 // Set all pints for OUTPUT
 for(int i=0;i<10;i++){
   pinMode(ledPin[i], OUTPUT);
 }

//Record the time once the setup has completed
 timeChanged = millis();
}

void loop(){
 //Check whether it has been long enough
 if ((millis() - timeChanged) > delayTime){

   //turn off all of the LEDs
   for(int i =0; i< 10; i++){
     digitalWrite(ledPin[i], LOW);
   }

   //Light the current LED
   digitalWrite(ledPin[currentLED], HIGH);

   //Increase the direction value (up or down)
   currentLED = currentLED + dir;

   //If we are at the end of a row, change direction
   if (currentLED ==9){
     dir = -1;
   }
   if (currentLED ==0){
     dir = 1;
   }

   //store the current time as the time we last changed LEDs
   timeChanged = millis();
 
}

 }

On this line :

void loop(){
 //Check whether it has been long enough
 if ((millis() - timeChanged) > delayTime){

   //turn off all of the LEDs
   for(int i =0; i< 10; i++){
     digitalWrite(ledPin[i], LOW);
   }

   //Light the current LED
   digitalWrite(ledPin[currentLED], HIGH);
  1. Is it turning everything off first before putting one pin as HIGH? if Yes, I don't see all of the LEDs are turning off, instead, it run continuosly.

example :

First loop : Pin 3,5,6,7,8,9,10,11,12,13 are OFF
Pin 4 are ON

Second loop: Pin 3,4,6,7,8,9,10,11,12,13 are OFF
Pin 4 are ON

Third loop: Pin 3,4,5,7,8,9,10,11,12,13 are OFF
Pin 5 are ON

(and so on..)

From what I understand, it should Run like this:

First Loop: All Pins OFF

second Loop : Pin 3,5,6,7,8,9,10,11,12,13
Pin 4 are ON

third loop : All Pins OFF

Fourth loop : Pin 3,4,6,7,8,9,10,11,12,13
Pin 5 are ON

(and so on)

  1. If both code run at the same time,isn't it creates conflict? where it ask to turn ALL Low and HIGH on a certain PIN at the same time. Isn't the code at the 'certain' PIN conflicts?

Thanks :smiley:

) Is it turning everything off first before putting one pin as HIGH?

Yes.

if Yes, I don't see all of the LEDs are turning off, instead, it run continuosly.

Hmmm. It's really hard to tell what you are seeing, and compare that to the code, since, obviously, your code does not look like that.

There is a sticky at the top of the forum. Read that, like you were supposed to before you improperly posted code here, and do it right. Then, we can talk.

Please put your code in its own window as seen in other posts. This can be done by placing     [code]  and [/code]  around the code or use the </> icon. This makes it easier for others to read.

How to use this forum

Weedpharma

In setup(), this line:

pinMode(ledPin, OUTPUT);

should be:

pinMode(ledPin[i], OUTPUT);

In loop(), this line:

digitalWrite(ledPin, LOW);

should be:

digitalWrite(ledPin[i], LOW);

Henry_Best:
In setup(), this line:

pinMode(ledPin, OUTPUT);

should be:

pinMode(ledPin[i], OUTPUT);

In loop(), this line:

digitalWrite(ledPin, LOW);

should be:

digitalWrite(ledPin[i], LOW);

Useless advice, as clearly the problem is that the forum software mangled the incorrectly posted code.

PaulS:
Useless advice, as clearly the problem is that the forum software mangled the incorrectly posted code.

Sorry PaulS, I don't understand what you are saying. Henry_Best's advice looks correct to me.

To the original poster, please go back and edit your first post, and put the code inside "code" tags. See the thread "how to post to the forum".

edit: ahh, I see. The OP has fixed the original post, and as you say, the forum was mangling his untagged version. Henrys_Best's advice was based on the messed up post.

Really Sorry

I was in rush back then. I've edited the the post :smiley:

However, it seems that i still couldn't understand on the answer given. Sorry :slightly_frowning_face:

From the Original Post

if Yes, I don't see all of the LEDs are turning off, instead, it run continuosly.

That is because it happens very quickly - in a few microseconds.

...R

Akh_f:
Really Sorry

I was in rush back then. I've edited the the post :smiley:

However, it seems that i still couldn't understand on the answer given. Sorry :slightly_frowning_face:

Henry_Best's answer was based on your original code that you posted, which looked like it had the bugs he described, but as PaulS said, that was because you originally did not post with code tags, and the forum messed with the code as a result.

So, ignore Henry_Best's answer, and we are back on track.

In answer to your original question, there is no delay between turning off all the leds, and turning on an individual led.

So, the sequence becomes simply:

  1. turn off all leds
  2. turn on the currently chosen led
  3. wait delay time (400ms)
  4. make the next led the currently chosen led
  5. go to step 1

the only delay is at step 3.

PaulS:
Useless advice, as clearly the problem is that the forum software mangled the incorrectly posted code.

Ah! I didn't spot that the forum software turned his [ i ] into italics.

Take a look at the code in you book and figure out the role of millis() and associated code.
It sure looks as you are missing some timing code.
Or what is the loop doing when the first if( ( and only if) is false? Nada.
Looks as incomplete code irregardless if is in italics or not.

I just tested the code in the original post (now in code tags) and it seems to work fine for me.

Vaclav:
Take a look at the code in you book and figure out the role of millis() and associated code.
It sure looks as you are missing some timing code.
Or what is the loop doing when the first if( ( and only if) is false? Nada.
Looks as incomplete code irregardless if is in italics or not.

The code looks OK to me. It is just not doing what he expected. It is moving a LED every 400ms, like a chaser circuit would. He expected a delay between one led going off, rather than just moving to the next led every 400ms.