Arduino code help.

Discussion in 'Programmer's Corner' started by c.marsh, May 22, 2013.

  1. c.marsh

    Thread Starter Active Member

    May 16, 2009
    100
    0
    okay so im struggling with something.

    I'm trying to make a larson scanner with the arduino, the problem is i CANNOT use the delay function. I can make the scanner with it, but it locks out the cpu and i need some other functions running which wont work because of the delay.

    so using this example

    Code ( (Unknown Language)):
    1. const int ledPin =  13;      // the number of the LED pin
    2.  
    3. // Variables will change:
    4. int ledState = LOW;             // ledState used to set the LED
    5. long previousMillis = 0;        // will store last time LED was updated
    6.  
    7. // the follow variables is a long because the time, measured in miliseconds,
    8. // will quickly become a bigger number than can be stored in an int.
    9. long interval = 1000;           // interval at which to blink (milliseconds)
    10.  
    11. void setup() {
    12.   // set the digital pin as output:
    13.  
    14.   pinMode(ledPin, OUTPUT);      
    15. }
    16.  
    17. void loop()
    18. {
    19.   // here is where you'd put code that needs to be running all the time.
    20.  
    21.   // check to see if it's time to blink the LED; that is, if the
    22.   // difference between the current time and last time you blinked
    23.   // the LED is bigger than the interval at which you want to
    24.   // blink the LED.
    25.   unsigned long currentMillis = millis();
    26.  
    27.   if(currentMillis - previousMillis > interval) {
    28.     // save the last time you blinked the LED
    29.     previousMillis = currentMillis;  
    30.  
    31.     // if the LED is off turn it on and vice-versa:
    32.     if (ledState == LOW)
    33.       ledState = HIGH;
    34.     else
    35.       ledState = LOW;
    36.  
    37.     // set the LED with the ledState of the variable:
    38.     digitalWrite(ledPin, ledState);
    39.   }
    40. }
    and the code i already have which is minimal:

    Code ( (Unknown Language)):
    1. int pinled1 = 13;
    2. int pinled2 = 12;
    3. int pinled3 = 11;
    4. int pinled4 = 10;
    5. int pinled5 = 9;
    6. int pinled6 = 8;
    7. int pinled7 = 7;
    8.  
    9. void setup() {
    10.    pinMode(pinled1, OUTPUT);
    11.    pinMode(pinled2, OUTPUT);
    12.    pinMode(pinled3, OUTPUT);
    13.    pinMode(pinled4, OUTPUT);
    14.    pinMode(pinled5, OUTPUT);
    15.    pinMode(pinled6, OUTPUT);
    16.    }
    17.  
    18. void loop()
    19. {
    20. }
    I simply cannot work out how to get the scanner to do what i want it to. which this:

    led 1 comes on > led 2 comes on> led 1 turns off and led 3 turns on > led 2 turns of and led 4 turns on

    with a time of 50ms between the functions (but led 1 turning off at same time as led 3 turning on)

    can someone help me out by getting the first 3 led's working? after that i can type up the rest of the code as it turns out i can understand the code if i can type it out copying something that already works. bazaare i know but hey we all learn in different ways.

    I'm trying to keep each led's function seperated out so that i can add in other code along the way and get other functions running in the right places.
     
Loading...