Well this is my first post, so Hi everybody!
I'm not sure if this is possible, but my goal is to two led's blinking at different intervals.
In my head it would look like this:
int ledBlue = 13;
int ledRed = 12;
void setup() {
pinMode(ledBlue, OUTPUT);
pinMode(ledRed, OUTPUT);
}
void blueFunction() {
digitalWrite(ledBlue, HIGH);
delay(750);
digitalWrite(ledBlue, LOW);
delay(750);
}
void redFunction() {
digitalWrite(ledRed, HIGH);
delay(1000);
digitalWrite(ledRed, LOW);
delay(1000);
}
/* This is where I get confused I know blueFunction will run, and then redFunction will run. My questions is how do I get them to run at the same time? */
void loop() {
blueFunction();
redFunction();
}
Looking forward to learning a lot around here,
Andrew
SOLUTION:
/*
Author: Andrew Douglas Hood
Date: October 10th, 2009
Title: Independent Blinks
Description: Have a blue led blink every 750 milliseconds,
and have a red led blink every 1000 milliseconds.
On the time line it looks like this:
milliseconds blue red
750 ON OFF
1000 ON ON
1500 OFF ON
2000 OFF OFF
2250 ON OFF
3000 OFF ON
*/
const int ledBlue = 13; // blue led on digital out pin 13
const int ledRed = 12; //red led on digital out pin 12
int blueState = LOW; // the variable to check whether the led is on or off
int redState = LOW;
long redInterval = 1000; // 1000 milliseconds off, 1000 on
long blueInterval = redInterval * 3 / 4; // 750 milliseconds on 750 off, and so an so forth
long blueCounter = 0; // I use these variables to store the amount of time it's been since the led was last on, or off.
long redCounter = 0;
void setup() {
pinMode(ledBlue, OUTPUT); // Set pins 12, and 13 to output
pinMode(ledRed, OUTPUT);
Serial.begin(9600); // Begin serial connection with PC
}
void blueFunction() // I use this function to blink the blue led on and off
{
if(millis() - blueCounter > blueInterval) // if it has been more than 750 ms since the blueState has changed, change it.
{
blueCounter = millis();
Serial.print("blueCounter = ");
Serial.println(blueCounter);
if(blueState == LOW)
{
blueState = HIGH;
digitalWrite(ledBlue, blueState);
Serial.println("Blue LED ON");
}
else
{
blueState = LOW;
digitalWrite(ledBlue, blueState);
Serial.println("Blue LED OFF");
}
}
}
void redFunction() // refer to blueFunction for explanation. Or to someone better at explaining on http://www.arduin.cc > Forums
{
if(millis() - redCounter > redInterval)
{
redCounter = millis();
Serial.print("redCounter = ");
Serial.println(redCounter);
if(redState == LOW)
{
redState = HIGH;
digitalWrite(ledRed, redState);
Serial.println("Red LED ON");
}
else
{
redState = LOW;
digitalWrite(ledRed, redState);
Serial.println("Red LED OFF");
}
}
}
void loop()
{
blueFunction(); //Run blue function
redFunction(); // Run Red function
}