Go Down

Topic: Help with cycling multiple LED states within modified Blink without delay (Read 825 times)previous topic - next topic

Lachstock1

Hi there,

I recently undertook an arduino project for uni. I'm a total novice when it comes to anything arduino, I made a couple of posts regarding project guidance and have made a little progress, however there are still some pretty big gaps in my knowledge. I've tried to follow the advice I've received as best I can but have had trouble a little with various bits, and understanding basic programming language...

So here it is:

I am making device which requires 2 lots of 12 LED's (I refer to them as banks) to be flashing at varying frequencies- for example:

Bank 1 might flash HIGH for 250, then LOW 250 with an interval of 400

while

Bank 2 might flash HIGH for 075, then LOW for 075, with an interval of 200.

This would go for 30 seconds, then both banks would change to a different frequency of flashes per second for another 30 seconds. And so on, for a total run time of 3 minutes.

I've worked out the wiring for the LED's, I'm using a MEGA and have the 2 banks running off PWM pins 11 and 12.

I found some modified 'Blink without delay code' off this forum and was able to tweak it ever so slightly so that I have a group of intervals which can be called. My problem is however, that I'm not sure how to get the loop to run through them, instead of just having one assigned interval for each pin.

I'm aware there are ways of using a 'for' or 'while' command ? do set a kind of timer or counter in order to keep track of how many times a loop may have run, but have no idea how to write this into the existing code..

If anyone could point me to the right solution, or even tack a little tweaking of their own onto this it would be an immense help!
Thank you in advance!

here is the code I currently have:

#define OFFTIME 1000
#define ONTIME 1000
unsigned long interval = 1000;

#define OFFTIME1 250
#define ONTIME1 300
unsigned long interval1 = 250;

#define OFFTIME2 250
#define ONTIME2 300
unsigned long interval2 = 250;

#define OFFTIME3 900
#define ONTIME3 10
unsigned long interval3 = 50;

const int ledPin1 =  12;
int ledState = LOW;
long previousMillis1 = 0;

const int ledPin2 =  11;
int ledState1 = LOW;
long previousMillis2 = 0;

void setup()
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);

}

void loop()
{

unsigned long currentMillis1 = millis();

if (currentMillis1 - previousMillis1 > interval3)
{
previousMillis1 = currentMillis1;

if (ledState == LOW)
{
ledState = HIGH;
interval = ONTIME;
}
else
{
ledState = LOW;
interval = OFFTIME;
}
digitalWrite(ledPin1, ledState);
}

unsigned long currentMillis2 = millis();

if (currentMillis2 - previousMillis2 > interval2)
{
previousMillis2 = currentMillis2;

if (ledState1 == LOW)
{
ledState1 = HIGH;
interval = ONTIME;
}
else
{
ledState1 = LOW;
interval = OFFTIME;
}
digitalWrite(ledPin2, ledState1);
}

}

marco_c

Look at the MultiBlink example in the Playground.

What you may want to do is defined 2 data tables and swap over which one you use to get the different patterns you are lookng for.
Arduino Libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com
Arduino++ blog https://arduinoplusplus.wordpress.com

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy