# Simultaneous functions? *SOLVED*

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
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
}
``````

which shows exactly how to have a function called with a variable delay
in the main loop. You just need two of such functions.

This is what I ended up getting, but only the blueLed blinks. :-?

const int ledBlue = 13;
const int ledRed = 12;

int blueState = LOW;
int redState = LOW;

long interval = 1000;
long counter = 0;

void setup() {

pinMode(ledBlue, OUTPUT);
pinMode(ledRed, OUTPUT);
}

void blueFunction()
{
if(millis() - counter > (interval - 250))
{
counter = millis();
if(blueState == LOW)
{
blueState = HIGH;
digitalWrite(ledBlue, blueState);
}
else
{
blueState = LOW;
digitalWrite(ledBlue, blueState);
}
}
}

void redFunction()
{
if(millis() - counter > interval)
{
counter = millis();
if(redState == LOW)
{
redState = HIGH;
digitalWrite(ledRed, redState);
}
else
{
redState = LOW;
digitalWrite(ledRed, redState);
}
}
}

void loop()
{
redFunction();
blueFunction();
}

First to get your thinking straight, you should understand that it is not possible for a AVR microprocessor to do any Simultaneous functions, it can only execute one sequence of instructions at a time. There are features like interrupts that can stop one flow of instructions, execute a different sequence of code and later return to the original sequence.

However the processor runs at such a speed that there are several programming control structures available to the programmer so that it can appear that there are several simultaneous processes going one, when in fact there is not.

So study the control structures used by C and you can study a lot from reading the sketches submitted by users here and else where.

So one true multi-tasking / multi-processing / simultaneous stuff going on in any individual Arduino boards, it's all done with 'C' using smoke and mirrors.

Lefty

You need to have a counter-variable for each color. The criteria: millis() - counter > interval will never be fullfilled because you're resetting the counter every time millis() - counter > (interval - 250)...

retrolefty: phhh, okay. That puts things in perspective. I've tried to find some tutorials on google about using C++ (I'm familiar with it)
but I haven't had any luck, would you happen to know of any?

Aniss1001: That did it!

For people that stumble across this thread with the same question I had, here's my final code.

``````/*
Author: Andrew Douglas Hood
Date: October 10th, 2009
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
}
``````

Thanks for the help everyone!

This is an untested version using the TimedAction library:

``````#include <TimedAction.h>

//pin / state variables
const byte ledPin1 = 12;
const byte ledPin2 = 13;

boolean ledState1 = true;
boolean ledState2 = true;

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

void loop(){
timedAction1.check();
timedAction2.check();
}