Code problem from delay to TimeOne.h Arduino

Thread Starter

c32do

Joined Aug 13, 2012
3
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:

Rich (BB code):
buttonPin2State = digitalRead(buttonPin2);

if((buttonPin2State== HIGH) && (digitalRead(ledPin3) == HIGH)) {
for (count=0;count<5;count++ ) {
digitalWrite(pinArray[count], HIGH);
delay(timer*2);
digitalWrite(pinArray[count], LOW);
delay(timer);
}
}
else if ((buttonPin2State == HIGH) && (digitalRead(ledPin5) == HIGH)) {
for (count=0;count<5;count++) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(500);
}
}
else if((buttonPin2State== HIGH) && (digitalRead(ledPin6) == HIGH)) {
for (count=0;count<5;count++) {
digitalWrite(pinArray[count], HIGH);
delay(400);
digitalWrite(pinArray[count], LOW);
delay(200);
}}}

Revised script:

Rich (BB code):
#include <TimerOne.h>

int ledArray1[]={3,5,6}; 
int ledArray2[]={9,10,11,7,12};

int countA=6,countB=10; 

void setup()
{
  int count=0;
  while (count<3) 
  {
    pinMode(ledArray1[count],OUTPUT); 
    pinMode(ledArray2[count],OUTPUT);
    count++;
  }

  while (count<5) 
  {
    pinMode(ledArray2[count],OUTPUT);
    count++;
  }
  Timer1.initialize(1000000);
  Timer1.attachInterrupt( loopLeds ); 
}

void loop()


void loopLeds()
{
  if (countA<3) { 
    digitalWrite(ledArray1[countA],HIGH);
  }
  if (countA==3) { 
    digitalWrite(ledArray1[0],LOW);
    digitalWrite(ledArray1[1],LOW);
    digitalWrite(ledArray1[2],LOW);
  }
  countA++;
  if (countA>=6) countA=0;

  
  if (countB<5) { 
    digitalWrite(ledArray2[countB],LOW);
  }
  if (++countB>=10) countB=0; 
  if (countB<5) { 
    digitalWrite(ledArray2[countB],HIGH);
  }
}
 

Thread Starter

c32do

Joined Aug 13, 2012
3
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.
 
Top