1

I have a tutorial to finish and I don't know the code I have to use to make all (4 LEDs) to turn on in order. So this is my code and my simulation. Thanks in advance!

Figure 1

My Code:

void setup() {
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(6, HIGH);
  delay(100); // Wait for 100 millisecond(s)

  digitalWrite(13, LOW);
  digitalWrite(12, LOW);
  digitalWrite(9, LOW);
  digitalWrite(6, LOW);
  delay(100); // Wait for 100 millisecond(s)
}

EDITED: I have to follow this one. I guess my simulator is correct.

Figure 2

  • 1
    you have the LEDs really connected like on your fritzing schema? 5V to 5V pin – Juraj Feb 26 '18 at 15:13
  • 4
    That wiring diagram (I refuse to call those things schematics) makes no sense to me at all. You have two LEDs in parallel, in series with two resistors that are also in parallel, and that chain is connected to two IO pins in parallel. And then you have it duplicated. The best you could hope for is turning on the LEDs in pairs. At worst, you will break the Arduino. – Majenko Feb 26 '18 at 15:21
  • @Majenko, they are not in parallel. only the black wire is connected to 5V instead of ground (and uses red rail on the breadboard). – Juraj Feb 26 '18 at 15:41
  • 1
    @Juraj You forget the metal strips in the breadboard... those LEDs are in parallel pairs, and the resistors are in parallel pairs. – Majenko Feb 26 '18 at 15:42
  • @Majenko, true, Sorry, I am confused by this ugly fritzing – Juraj Feb 26 '18 at 15:49
2

I see in your second diagram that you are controlling the ground side of the LEDs. digitalWrite(PinNumber, HIGH) will turn an LED OFF, digitalWrite(PinNumber, LOW) will turn an LED ON. Using delay() in a sketch should be avoided.

void setup(){
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(6, OUTPUT); 
  digitalWrite(13, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(6, HIGH);
}

void loop(){
  digitalWrite(13, LOW);
  delay(100);
  digitalWrite(12, LOW);
  delay(100);
  digitalWrite(9, LOW);
  delay(100);
  digitalWrite(6, LOW);
  delay(100);
  digitalWrite(13, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(6, HIGH);
  delay(100);
}
1

Your code switches them all ON and OFF simultaneously.

What I read from your question is you want to turn them on in order. In this case, use a simple state diagram. This means you have to create variable (preferably an enumeration), e.g.:

enum EState { FIRST_ON, SECOND_ON, THIRD_ON, FOURTH_ON };

EState _state;

You can better use better names than FIRST_ON, ..., but I do not know what your lights mean.

In the setup, you initialize _state, e.g.

_state = FIRST_ON;

Than in the loop you check and change the state:

switch (_state)
{
case FIRST_ON:
    digitalWrite(13, HIGH); // Assuming this is the first light
    digitalWrite(12, LOW); // Put the others off
    ...
    ...
    _state = SECOND_ON; // Set the next state
    break;

case SECOND_ON:
    digitalWrite(13, LOW); // Assuming this is the first light
    digitalWrite(12, HIGH); // Put the second LED on
    .. // Put the third/fourth off
    _state = THIRD_ON; // Set the next state
    break;

    // Do the same for THIRD_ON and FOURTH_ON

default: // in all other cases
     break; // Should not happen
}

delay(100: // Wait for 100 milliseconds

Note, to not complicate the example, I've written out (mostly) the lines like you:

digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(9, HIGH);
digitalWrite(6, HIGH);

However, since these are almost copies (4 in the state diagram), it is best to write a separate function:

void SwitchLedsOn(int led1State, int led2State, int led3State, int led4State)
{
  digitalWrite(13, led1State);
  digitalWrite(12, led2State);
  digitalWrite(9, led3State);
  digitalWrite(6, led4State);
}

Now you can use in the first state case:

case FIRST_ON:
    SwitchLedsOn(HIGH, LOW, LOW, LOW); // Switch only first on
    break;

And do similar with the other states.

  • really? a state machine for this beginer? smart-jokes.org/programmer-evolution.html – Juraj Feb 26 '18 at 15:47
  • 1
    are you sure that is for beginner cause my subject is something like beginner for arduino – Ticherhaz Feb 26 '18 at 16:39
  • It's not for beginner's true. However, the concept is so useful, that even for beginner's it is good to know it exists. Maybe they can read this answer at a later stage. – Michel Keijzers Feb 26 '18 at 17:22
1

add this to loop() end

 digitalWrite(13, HIGH);
 delay(100);
 digitalWrite(12, HIGH);
 delay(100);
 digitalWrite(9, HIGH);
 delay(100);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.