Code problem from delay to TimeOne.h Arduino

Discussion in 'Programmer's Corner' started by c32do, Aug 15, 2012.

  1. c32do

    Thread Starter New Member

    Aug 13, 2012
    3
    0
    The script has been compiled using Arduino Uno.

    my old array was just using basic functions using delays etc. I am stuck with how to rewrite this snippet of code into my revised script. This bit of script wasn't even finished, but I got the basic idea of what I wanted to do. It played around with the delays to speed up a 5 led display blink array on the press of a button. So how do I change it to work in the revised script:

    Code ( (Unknown Language)):
    1. buttonPin2State = digitalRead(buttonPin2);
    2.  
    3. if((buttonPin2State== HIGH) && (digitalRead(ledPin3) == HIGH)) {
    4. for (count=0;count<5;count++ ) {
    5. digitalWrite(pinArray[count], HIGH);
    6. delay(timer*2);
    7. digitalWrite(pinArray[count], LOW);
    8. delay(timer);
    9. }
    10. }
    11. else if ((buttonPin2State == HIGH) && (digitalRead(ledPin5) == HIGH)) {
    12. for (count=0;count<5;count++) {
    13. digitalWrite(pinArray[count], HIGH);
    14. delay(timer);
    15. digitalWrite(pinArray[count], LOW);
    16. delay(500);
    17. }
    18. }
    19. else if((buttonPin2State== HIGH) && (digitalRead(ledPin6) == HIGH)) {
    20. for (count=0;count<5;count++) {
    21. digitalWrite(pinArray[count], HIGH);
    22. delay(400);
    23. digitalWrite(pinArray[count], LOW);
    24. delay(200);
    25. }}}

    Revised script:

    Code ( (Unknown Language)):
    1. #include <TimerOne.h>
    2.  
    3. int ledArray1[]={3,5,6};
    4. int ledArray2[]={9,10,11,7,12};
    5.  
    6. int countA=6,countB=10;
    7.  
    8. void setup()
    9. {
    10.   int count=0;
    11.   while (count<3)
    12.   {
    13.     pinMode(ledArray1[count],OUTPUT);
    14.     pinMode(ledArray2[count],OUTPUT);
    15.     count++;
    16.   }
    17.  
    18.   while (count<5)
    19.   {
    20.     pinMode(ledArray2[count],OUTPUT);
    21.     count++;
    22.   }
    23.   Timer1.initialize(1000000);
    24.   Timer1.attachInterrupt( loopLeds );
    25. }
    26.  
    27. void loop()
    28.  
    29.  
    30. void loopLeds()
    31. {
    32.   if (countA<3) {
    33.     digitalWrite(ledArray1[countA],HIGH);
    34.   }
    35.   if (countA==3) {
    36.     digitalWrite(ledArray1[0],LOW);
    37.     digitalWrite(ledArray1[1],LOW);
    38.     digitalWrite(ledArray1[2],LOW);
    39.   }
    40.   countA++;
    41.   if (countA>=6) countA=0;
    42.  
    43.  
    44.   if (countB<5) {
    45.     digitalWrite(ledArray2[countB],LOW);
    46.   }
    47.   if (++countB>=10) countB=0;
    48.   if (countB<5) {
    49.     digitalWrite(ledArray2[countB],HIGH);
    50.   }
    51. }
     
  2. c32do

    Thread Starter New Member

    Aug 13, 2012
    3
    0
    So if Button2 is pressed during ((digitalRead(ledPin3) == HIGH) then it speeds up ledArray2 for example 1 second faster

    or if Button2 is pressed during ((digitalRead(ledPin5) == HIGH) then it speeds up ledArray2 2 second faster

    or if Button2 is pressed during ((digitalRead(ledPin6) == HIGH) then it speeds up ledArray2 3 seconds faster

    and to better understand:

    ledArray2 is a car on a race track that goes around and around at 60mph
    and ledArray1 is a visual of say a speed increaser, so if ((digitalRead(ledPin3) comes on and we press the button it makes the car go a bit faster at 70MPH for 1 sec or
    we decide to wait until the next led to come on((digitalRead(ledPin5) so we press and it makes the car go even faster 70MPH for 2 seconds or we decide to wait until the next led to come on ((digitalRead(ledPin6) so we press and it makes the car go at light speed 70 MPH for 3 seconds

    I hope this explains it better.
     
Loading...