Two processes at the same time


#1

I want ask if the mbot or arduino can run two prcesses at the same time.
I have tried the following code:

#include <MeMCore.h>

MeRGBLed led;
MeDCMotor motor1(M1); //Motor1 is Left Motor
MeDCMotor motor2(M2); //Motor2 is Right Motor

bool ledOn = false;
int interval = 500; //inteval at which to blink
long previousTime = 0;   //will store last time LED was updated

void setup() {
  led.setpin(13);
}

void loop() {
  // get current time every loop
  long currentTime = millis();

 /* //check if difference between last blink and currenttime is bigger than interval
  if(currentTime- previousTime >= interval )
  {
    //check if LED is already on
    if(ledOn == false)
    {
      led.setColor(255,255,255);
      led.show();
      ledOn = true;
    }
    else{
      led.setColor(0,0,0);
      led.show();
      ledOn = false;
      }
      
    // save the last time you blinked the LED
    previousTime = currentTime;
   }*/

   
   motor1.run(-150); //Motor1 (Left)  forward is -negative
   motor2.run(150);  //Motor2 (Right) forward is +positive
}

This code should always check the time bettween the last blinking.
But I have also tried the following code

#include <MeMCore.h>

MeRGBLed led;
MeDCMotor motor1(M1); //Motor1 is Left Motor
MeDCMotor motor2(M2); //Motor2 is Right Motor

bool ledOn = false;
int interval = 500; //inteval at which to blink
long previousTime = 0;   //will store last time LED was updated

void setup() {
  led.setpin(13);
}

void loop() {
  // get current time every loop

      led.setColor(255,255,255);
      led.show();
      delay(500);
      led.setColor(0,0,0);
      led.show();
      delay(500);

   motor1.run(-150); //Motor1 (Left)  forward is -negative
   motor2.run(150);  //Motor2 (Right) forward is +positive
}

And I couldn’t notice a different.

Thanks for an answer and explanation of the difference of these two examples and if the first is necessary.
kindly regards


#2

The Atmel328P microcontroller is single threaded, so to do what you are wanting to do will require a different approach, i.e., time slicing. You are using the delay method which is a blocking method. You will need to use the millis() method and account for rollover (when the counter reaches it’s maximum value it will start over at 0). See the code for ActionTimer (link) for an example of how to do that.


#3

Thanks for the answer.
I don’t understand it completly.
I think you mean to run more processes at the same time I can solve it with your link “ActionTimer”.
where in the code can I insert the blinking and running of the engine.

What about my first example. Does this also solve the problem that I can run two processes at the same time?
kindly regards again for an further answer


#4

Hi.
Hope someone can still answer my last comment.
Kindly regards


#5

As Chuck stated, this is a single threaded processor. That means it can only run ONE thing at a time. You would need a multi-core, multi-threaded processor to run multiple things at the same time. I use a Raspberry PI, connected to the Arduino to accomplish this.

What you have to do is time slice. A short amount of time for each part of your code, in a loop. The processor runs fast enough, that you will think it is multi-processing.


#6

Thanks again.
I have understand that arduino is a single threaded processor.
Therefore I asked if my example in post #1 is a possible solution that blinking and driving will solve it best for the single threaded processor.
kindly reagards again


#7

I’m sorry you did not understand my first response. Let me try again.

The processor is single-tasking and does not support threading or multi-tasking per se. The ActionTimer uses a cooperative time-slicing technique that will allow you to do several tasks in what appears to be multi-tasking. However, if any of the tasks requires a lot of processing, then you will gain nothing by using it.

In the ActionTimer example, I do a simple toggle of the value of a variable named state and print a message to the serial console. The actual time that it takes to do this is relatively small and so it doesn’t affect anything. For your purposes, you would substitute the code to toggle the LED on and off. Every time the loop method executes, the ActionTimer instance is called to see if it’s time to do something else. Using a state variable would allow you to control when the LED is turned on or off, just like in the example code.

Hope that’s clearer. If not, let me know.


#8

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.