Connect with us

LED flash issue

Discussion in 'Microcontrollers, Programming and IoT' started by bigone5500, Aug 25, 2014.

Scroll to continue with content
  1. bigone5500

    bigone5500

    712
    120
    Apr 9, 2014
    I am trying to learn this stuff but I can't figure out this one thing I want to do. I have 6 leds that currently are flashing in sequence. That is working. When I power the arduino, I want all 6 to come on for 250ms, delay for 250, go off, delay 1000ms, do this twice then run the 'chaser' effect.

    Here is my code:
    Code:
    /*
      Blink
      Turns on an LED on for one second, then off for one second, repeatedly.
      This example code is in the public domain.
    */
    
    int led1 = 2;
    int led2 = 3;
    int led3 = 4;
    int led4 = 5;
    int led5 = 6;
    int led6 = 7;
    
    void setup()
    {
      pinMode(led1, OUTPUT);   
      pinMode(led2, OUTPUT);
      pinMode(led3, OUTPUT);
      pinMode(led4, OUTPUT);
      pinMode(led5, OUTPUT);
      pinMode(led6, OUTPUT);
    }
    
    void loop()
    {
      digitalWrite(led1, led2, led3, led4, led5, led6, HIGH);
      delay(250);
      digitalWrite(led1, led2, led3, led4, led5, led6, LOW);
      delay(250);
      digitalWrite(led1, led2, led3, led4, led5, led6, HIGH);
      delay(250);
      digitalWrite(led1, led2, led3, led4, led5, led6, LOW);
      delay(1000);
    
      digitalWrite(led1, HIGH);
      delay(20);
      digitalWrite(led1, LOW);
      delay(25);
    
      digitalWrite(led2, HIGH);
      delay(30);
      digitalWrite(led2, LOW);
      delay(35);
    
      digitalWrite(led3, HIGH);
      delay(40);
      digitalWrite(led3, LOW);
      delay(45);
    
        digitalWrite(led4, HIGH);
      delay(50);
      digitalWrite(led4, LOW);
      delay(55);
    
        digitalWrite(led5, HIGH);
      delay(60);
      digitalWrite(led5, LOW);
      delay(65);
    
        digitalWrite(led6, HIGH);
      delay(70);
      digitalWrite(led6, LOW);
      delay(75);
    }
     
  2. Gryd3

    Gryd3

    4,098
    875
    Jun 25, 2014
    digitalWrite(led1, led2, led3, led4, led5, led6, HIGH);

    I don't believe you can pass multiple pins to a 'digitalWrite'


    split it up and try again
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    digitalWrite(led4, HIGH);
    digitalWrite(led5, HIGH);
    digitalWrite(led6, HIGH);


    Also, your LEDs will all blink on, then off each time the tracer repeats.
    You can resolve this by creating another function above the void loop() function that contains your code to turn on all leds.
     
  3. bigone5500

    bigone5500

    712
    120
    Apr 9, 2014
    Ok thanks. I didnt think you could put anything else above the loop() part.

    Dont tell me too much...just some hints. I want to figure out as much as i can on my own as i learn faster that way.
     
  4. Gryd3

    Gryd3

    4,098
    875
    Jun 25, 2014
    EDIT: OOPS, then you don't have to read this part XD

    Alternatively... this may confuzzle you if you have not seen this before... but you can write a value to a 'port' which is a collection of pins.

    http://www.arduino.cc/en/Reference/PortManipulation

    This could allow you to change the state on multiple pins at the same time by using
    PORTD = B11111100 'Set value of pins 2,3,4,5,6,7 HIGH
     
  5. bigone5500

    bigone5500

    712
    120
    Apr 9, 2014
    I did see that on a video where a guy did a progress bar on an lcd.
     
  6. Gryd3

    Gryd3

    4,098
    875
    Jun 25, 2014
    And I may have spoken a little too soon regarding that other function above loop.

    I am unsure if the Arduino IDE will ignore it or not. You could be safe and put it in setup.
     
  7. bigone5500

    bigone5500

    712
    120
    Apr 9, 2014
    YES!!! I figured it out!

    Here's my new code:

    Code:
    /*
      Blink
      Turns on an LED on for one second, then off for one second, repeatedly.
      This example code is in the public domain.
    */
    int led1 = 2;
    int led2 = 3;
    int led3 = 4;
    int led4 = 5;
    int led5 = 6;
    int led6 = 7;
    
    void setup()
    {               
      pinMode(led1, OUTPUT);    
      pinMode(led2, OUTPUT);
      pinMode(led3, OUTPUT);
      pinMode(led4, OUTPUT);
      pinMode(led5, OUTPUT);
      pinMode(led6, OUTPUT);
      digitalWrite(led1, HIGH);
      digitalWrite(led2, HIGH);
      digitalWrite(led3, HIGH);
      digitalWrite(led4, HIGH);
      digitalWrite(led5, HIGH);
      digitalWrite(led6, HIGH);
      delay(250);
      digitalWrite(led1, LOW);
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
      digitalWrite(led4, LOW);
      digitalWrite(led5, LOW);
      digitalWrite(led6, LOW);
      delay(250);
        digitalWrite(led1, HIGH);
      digitalWrite(led2, HIGH);
      digitalWrite(led3, HIGH);
      digitalWrite(led4, HIGH);
      digitalWrite(led5, HIGH);
      digitalWrite(led6, HIGH);
      delay(250);
      digitalWrite(led1, LOW);
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
      digitalWrite(led4, LOW);
      digitalWrite(led5, LOW);
      digitalWrite(led6, LOW);
      delay(250);
    }
    void loop()
    { 
      digitalWrite(led1, HIGH);
      delay(20);
      digitalWrite(led1, LOW);
      delay(25);
    
      digitalWrite(led2, HIGH);
      delay(30);
      digitalWrite(led2, LOW);
      delay(35);
    
      digitalWrite(led3, HIGH);
      delay(40);
      digitalWrite(led3, LOW);
      delay(45);
     
        digitalWrite(led4, HIGH);
      delay(50);
      digitalWrite(led4, LOW);
      delay(55);
     
        digitalWrite(led5, HIGH);
      delay(60);
      digitalWrite(led5, LOW);
      delay(65);
     
        digitalWrite(led6, HIGH);
      delay(70);
      digitalWrite(led6, LOW);
      delay(75);
    }
     
  8. bigone5500

    bigone5500

    712
    120
    Apr 9, 2014
    Now that I have crossed that hurdle, I want to see if I can modify the code to allow all leds to flash twice, let them chase 5 times, then flash all twice again...repeat. I think I will have to use the function to do this but I don't know.

    This is running on my Nano BTW.
     
    Last edited: Aug 26, 2014
  9. Gryd3

    Gryd3

    4,098
    875
    Jun 25, 2014
    There is more than one way to skin a cat ;)
    And I was mistaken earlier saying I had a Nano... I have a micro XD.

    Keep us updated on your progress, and if you want someone to work through it with ya, I can power my micro up.
     
  10. bigone5500

    bigone5500

    712
    120
    Apr 9, 2014
    :D
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-