Running 2 operations at once

Hi all,

Ok so this is my first time at playing with the Arduino and im loving it. Ive been going through all the tutorials and vast amounts of information posted everywhere and its great.

Im just having trouble at trying to run several things at the same time.

Ok so lets keep it simple:

How do i write the sketch to run 2 blink programs at the same time but operate independantley of one another. All i can do is to get one to run one after the other.

And then how do i add more things to run independantley ( IE if i then wanted to add the 'Knight Rider' code to the 2 independantley blinking leds?????

Im sorry if this is a simple fix but i suppose we all have to start somewhere!

Any help with this would be a great start!!!! :slight_smile:

VR

Don't use delay() as that freezes the processor and nothing else gets done. Instead use the millis() function. And define when you want something to happen.
end_time = millis()+my_delay;

then in your main loop just say:-
if(millis() > end_time) { do my stuff like turn LED on }

Do you get the idea?

Hi Mike,

Thanks for your reply, Unfortunatley im still completely learning how to write in this format. Im so used to writing PLC ladder logic its taking some getting my head round!!

Here is the basic LED Blink code. COuld you show me how you would alter the delay parts to the milli parts:

int value = 0; // variable to keep the actual value
int ledpin = 9; // light connected to digital pin 9

void setup()
{
// nothing for setup
}

void loop()
{
for(value = 0 ; value <= 255; value+=5) // fade in (from min to max)
{
analogWrite(ledpin, value); // sets the value (range from 0 to 255)
delay(30); // time delay between each pwm fade in pulse
}
for(value = 255; value >=0; value-=5) // fade out (from max to min)
{
analogWrite(ledpin, value);
delay(30); // time delay between each pwm fade out pulse
}
}

Sorry for being such a newb!!!!

::slight_smile:

Back to the good old days of loop program like BBC basic , instead of event driven programming of Visual Basic etc , But I guess it hard to make a cpu multitask on 1k of ram.

I don't know it this will help you in any way, but you could take a look at Protothreads library. I just finished a Protothreads package to the Arduino IDE.

http://arduino.cc/pipermail/developers_arduino.cc/2009-January/000456.html

Protothreads will make you run two things at once (well kind of..)
Please also take a look at the authors website.

Regards
Benjamin

Thanks for the link. To be honest i have not got a clue what its all about. This is literally like trying to learn Chinese by reading it off the net!!!

I think i need to find a teacher or someone to explain all about 'C' here in Vancouver, because as much as im reading, its still not sinking in.

I find i can see how things work when im reading through peoples code but when i try and adjust codes to suit, all i get is error messages.

If someone could write a sketch to show me how to run the LED blink program and the Night rider program at the same time but independantly, then i would probably be able to see how these things work.
Im at the start of a big controller build program and i think its really going to push my limits of programming!!!

Please help!!!! VR :smiley:

Hang in there it will come in time. Even an old timer hardware guy like me is slowly picking C up semicolon by semicolon.

One thing that might help you is to end the paradigm of doing more then one thing at the same time. Most normal computers only do one instruction at a time, however the fact that they can perform steps so fast in human terms in can be made to look like it's doing more then one task at a time, but it's not really.

What you are looking for is how to structure a sequence of steps that repeat forever (loop) but some steps are only manipulating things for task A while the other steps are manipulating things for task B and a few steps are just to keep the whole thing repeating over and over.

Lefty

OK just didn't know how much you knew. Try this it uses a second LED connected to pin 10 that fades in the oposite way and does what I told you in the first post.

int value = 0;                            // variable to keep the actual value
int ledpin = 9;                           // light connected to digital pin 9
int ledpin2=10;    // second LED to fade
int value1=0, value2=0;
long time1, time2;

void setup()
{
 time1=millis();   // set time as now
 time2=millis();
}

void loop()
{
 if(millis()>time1) // fade in (from min to max)
 {
   analogWrite(ledpin, value1);           // sets the value (range from 0 to 255)
   time1 = millis()+30;                            // time delay between each pwm fade in pulse
   value1++;
   if(value1 > 255)value1=0;
 }
 if(millis()>time2)   // fade out (from max to min)
 {
   analogWrite(ledpin2, value2);
      time2 = millis() +35;
      value2= value2 -5;
   if(value2 < 0)value2=300;
                          
 }  
}

In essence what the above program is doing is this only with two tasks.

Thankyou all so much. This is a great help and i can now sit and look at each line one by one and work out how they all work together. I hope it starts to make sense with time!!!!!!

Im sure ill be picking your brains again but for now i need to go and play and manipulate and see if i can start getting my brain into the C mindset!!

Thanks once again!!!! :slight_smile:

VR

Guys,

Im sorry to be such a pain in the ass but i realised i posted the code for the LED fade instead of the LED blink. Im still trying to get my head round the code you did for me earlier, but i meant to post the blink so it was as easy as possible for me to understand.

Here is the code for the LED blink:

int ledPin = 13; // LED connected to digital pin 13

void setup() // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}

void loop() // run over and over again
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
}

Ive tried to adjust this to run 2 leds using the milli function but still can get anything to work.

Would you be kind enough again Mike to adjust this blink code using the milli functions to flash 2 LEDS independantly then i might be able to follow whats going on and translate that into whats happening in the LED fade code you did for me earlier.

Thanks in advance.

VR

OK last time:-

int ledPin = 13;                        // LED connected to digital pin 13
int ledPin2 = 12;                        // LED connected to digital pin 12
int led1val=0,led2val=0;
long blink1,blink2;
void setup()                             // run once, when the sketch starts
{
 pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  pinMode(ledPin2, OUTPUT);      // sets the digital pin as output
  blink1=millis();
  blink2=millis();
}

void loop()                              // run over and over again
{
  if(blink1>millis()) {
     digitalWrite(ledPin, led1val);     // sets the LED on
     if(led1val ==0) led1val=1; else led1val=0;  // toggle led value
    blink1= millis() +1000;  // set next time you want to do anything
  }
 
 if(blink2>millis()) {
     digitalWrite(ledPin2, led2val);     // sets the LED on
     if(led2val ==0) led2val=1; else led2val=0;  // toggle led value
    blink2= millis() +700;  // set next time you want to do anything this is faster
  }
 
}

Hi Mike. Thanks so much.

If i was back home in England id buy you a pint of Boddys!!!!

VR

disclaimer first post and I only got my arduino this week and have yet to write my first schetch but...
with the fade example you sort of started out with

byte ledPin_A = 11; // LED connected to pwm pin 11
byte ledPin_B = 10; // LED connected to pwm pin 10
byte swap = 0; // swap
byte value = 255 ;
void setup() { // run once, when the sketch starts
pinMode(ledPin_A, OUTPUT); // sets the digital pin as output
pinMode(ledPin_B, OUTPUT); // sets the digital pin as output
}

void loop() { // run over and over again
swap = ledPin_A; ledPin_A = ledPin_B; ledPin_B = swap; // exchange pins
for (value = 255 ; value ; value-=1){ // ends after value is zero
analogWrite(ledPin_A, 255-value); // fade in (from min to max)
analogWrite(ledPin_B, value); // fade out (from max to min)
delay(15); // time delay between each pwm step
}
}