running two independant processes

Making your program work.
Post Reply
ndpm
Posts: 6
Joined: Tue Nov 18, 2008 12:49 pm

running two independant processes

Post by ndpm » Wed Dec 10, 2008 12:57 am

Hello all,

It seems like it should be easy to find examples of this, but I have yet to do so.

I did see somethng about state machines but the code that was being executed was very complicated and I could not sort out the basic method.

At any rate, I am using two LEDs to simulate the end process. I blink one LED continuously and am trying to control the blink of another LED independently.

I know how to do it if I want the duty cycle of the green LED to be 50% -- but that is not what I want. While the RED device is on for 10 seconds and off for 10 minutes, the GREEN device needs to be on for 1 hour and off for 6 hours (or any other vriationon tht basic scheme)

Maybe I've been working on it too long or maybe its too late at night, but I can't figure out why this doesn't work...


/* Two Process Experiment
*
* Blinks a red LED continuously while a green LED blinks at a different rate
*
*/

int RedLED = 7; // Red LED connected to digital pin 7
int GreenLED = 8; // Green LED connected to digital pin 8

long onTime = 5000; // green LED on time (ms)
long offTime = 1000; // green LED off time (ms)
long last = 0;

int Flag = 1; // 1 = green LED on, 0 = Green LED off

void setup()
{
pinMode(RedLED, OUTPUT); // sets the digital pin as output
pinMode(GreenLED, OUTPUT); // sets the digital pin as output
}

void loop()
{

BlinkRed(); // flash the red LED


if ( (Flag==1) && ((millis() - last) < onTime) )

digitalWrite(GreenLED, HIGH);

if( (millis() - last) > onTime)
{
Flag=0;
last=millis();

}


if ( (Flag==0) && ((millis() - last) < offTime) )

digitalWrite(GreenLED, LOW);

if( (millis() - last) > offTime)
{
Flag=1;
last=millis();
}

}// end loop

void BlinkRed()
{
digitalWrite(RedLED, HIGH);
delay(50);
digitalWrite(RedLED, LOW);
delay(50);
}

The red LED flashes.

The green LED turns on for 10 seconds and then goes off, but it never comes back on again.

Any suggestions or a better approach would be appreciated.

Thanks,

Phil...

ndpm
Posts: 6
Joined: Tue Nov 18, 2008 12:49 pm

Re: running two independant processes

Post by ndpm » Wed Dec 10, 2008 9:24 pm

I think I have a solution to the problem. It involves the metro library.

Here is sample code that seems to be doing the job. The only think I am not sure of is exactly what the range of a metro object is. So I am not yet clear on how long the intervals can be.

/* Two Process Experiment
*
* Blinks red and green LED's independantly
* In this example, green once a second, red once every 10 seconds
* This is a minor extention of the sample code from
* the Metro Library page http://www.arduino.cc/playground/Code/Metro
* Thanks to Thomas Ouellet Fredericks for this library
*/

#include <Metro.h>

// Instanciate a 2 metro objects initialized to 250 milliseconds (0.25 seconds).
Metro GreenLEDMetro = Metro(250);
Metro RedLEDMetro = Metro(250);

int RedLED = 7; // Red LED connected to digital pin 7
int GreenLED = 8; // Green LED connected to digital pin 8

//Create variables to hold the LED's states
int GreenState = HIGH;
int RedState = HIGH;

void setup()
{
pinMode(RedLED, OUTPUT); // sets the digital pin as output
digitalWrite(RedLED,RedState); // writes the red LED state data to the pin
pinMode(GreenLED, OUTPUT); // sets the digital pin as output
digitalWrite(GreenLED,GreenState); // writes the green LED state data to the pin
}

void loop()
{

if (GreenLEDMetro.check() == 1) { // check if the metro has passed it's interval .
if (GreenState==HIGH) {
GreenState=LOW;
GreenLEDMetro.interval(500); // if the pin is HIGH, set the interval to 0.25 seconds.
}
else {
GreenLEDMetro.interval(500); // if the pin is LOW, set the interval to 1 second.
GreenState=HIGH;
}
digitalWrite(GreenLED,GreenState);
}

if (RedLEDMetro.check() == 1) { // check if the metro has passed it's interval .
if (RedState==HIGH) {
RedState=LOW;
RedLEDMetro.interval(9500); // if the pin is HIGH, set the interval to 0.25 seconds.
}
else {
RedLEDMetro.interval(500); // if the pin is LOW, set the interval to 1 second.
RedState=HIGH;
}
digitalWrite(RedLED,RedState);
}

}// end loop

ndpm
Posts: 6
Joined: Tue Nov 18, 2008 12:49 pm

Re: running two independant processes

Post by ndpm » Thu Dec 11, 2008 10:55 am

And finally to complete the thread (unless any one has anything to add) metro takes an unsigned long so the maximum interval time should be long enough for most applications.

Phil...

paul
Site Admin
Posts: 735
Joined: Mon May 12, 2008 4:19 pm

Re: running two independant processes

Post by paul » Sat Jan 24, 2009 9:47 pm

The first example seems simpler, but both will certainly work.

Paul

repco
Posts: 10
Joined: Wed Dec 16, 2009 2:54 am

Re: running two independant processes

Post by repco » Tue Jan 26, 2010 10:29 pm

This is an old question from 2008, but it wasn't answered in terms of his code.

What This program needs is to test the flag state before it changes the flag or the last time. Otherwise, it will reset the last time during the second test, and the greenled will constantly be set high.

If it didn't stay on, then it was wired to come on when the pin was written low.

code is below that works

/* Two Process Experiment
*
* Blinks a red LED continuously while a green LED blinks at a different rate
*
*/

int RedLED = 11; // Red LED connected to digital pin 7
int GreenLED = 12; // Green LED connected to digital pin 8

long onTime = 5000; // green LED on time (ms)
long offTime = 1000; // green LED off time (ms)
long last = 0;

int Flag = 1; // 1 = green LED on, 0 = Green LED off

void setup()
{
pinMode(RedLED, OUTPUT); // sets the digital pin as output
pinMode(GreenLED, OUTPUT); // sets the digital pin as output
}

void loop()
{

BlinkRed(); // flash the red LED


if ( (Flag==1) && ((millis() - last) < onTime) ) // if LED went off it was wired to go on when port was low

digitalWrite(GreenLED, HIGH);

if( (Flag == 1) && ((millis() - last) > onTime)) // the two flag changing test must also test current flag
{ // status the flag only changes if it needs to and last
Flag=0; // only changes when flag does
last=millis();

}


if ( (Flag==0) && ((millis() - last) < offTime) )

digitalWrite(GreenLED, LOW);
// because offtime is less than ontime
if( (Flag==0) && ((millis() - last) > offTime)) // without testing the flag this test will restart last
{ // resulting in last never being > ontime
Flag=1; // so flag =1 and millis()-last <ontime test will always be true
last=millis(); // and the greenled will always be high.
} // this fixes it.

}// end loop

void BlinkRed()
{
digitalWrite(RedLED, HIGH);
delay(50);
digitalWrite(RedLED, LOW);
delay(50);
}

rep

Post Reply