Arduino Multitasking

Hello,
I got the problem, that the following code ain’t multitasked.
How can i realize that?
My code works at the moment as follow:
I start my Android app, there I confirm the USB request.
After the press of the “start button”, my app sends a byte array to my arduino board.
The problem is that “stepper2(ingredient1value);” can only start when “stepper1…” finished.

I know that arduino ain’t the right plattform for multithreading, but I saw some solutions, but I can’t integrated them into my code

#include <Max3421e.h>
#include <Usb.h>
#include <AndroidAccessory.h>



#define VALUE_OFF 0x0
#define VALUE_ON 0x1
#define COMMAND_LED 0x2
#define TARGET_PIN_12 0x12

int stepperPin1 = 9;
int stepperPin2 = 10;
int stepperPin3 = 11;
int stepperPin4 = 12;
int stepperPin5 = 13;



//change this to the number of steps on your motor
#define STEPS 48

AndroidAccessory acc("Manufacturer", "Model", "Description", "1.0", "URI","Serial");
byte ingredient1value, ingredient2value, ingredient3value, ingredient4value, ingredient5value;
byte rcvmsg[8];



void setup() {
  Serial.begin(115200);
  

 pinMode(stepperPin1, OUTPUT);
 pinMode(stepperPin2, OUTPUT);
 pinMode(stepperPin3, OUTPUT);
 pinMode(stepperPin4, OUTPUT);
 pinMode(stepperPin5, OUTPUT);
  acc.powerOn();
}


void stepper1(int turns1){
 for(int i=0;i<turns1*STEPS;i++){
   digitalWrite(stepperPin1, HIGH);
   delayMicroseconds(800);
   digitalWrite(stepperPin1, LOW);
   delayMicroseconds(800);
  }
 }


 
void stepper2(int turns2){
 for(int i=0;i<turns2*STEPS;i++){
   digitalWrite(stepperPin2, HIGH);
   delayMicroseconds(800);
   digitalWrite(stepperPin2, LOW);
   delayMicroseconds(800);
  }
 }


 
void stepper3(int turns3){
 for(int i=0;i<turns3*STEPS;i++){
   digitalWrite(stepperPin3, HIGH);
   delayMicroseconds(800);
   digitalWrite(stepperPin3, LOW);
   delayMicroseconds(800);
  }
 }



 
void stepper4(int turns4){
 for(int i=0;i<turns4*STEPS;i++){
   digitalWrite(stepperPin4, HIGH);
   delayMicroseconds(800);
   digitalWrite(stepperPin4, LOW);
   delayMicroseconds(800);
  }
 }



 
void stepper5(int turns5){
 for(int i=0;i<turns5;i++){
   digitalWrite(stepperPin5, HIGH);
   delay(1000);
   digitalWrite(stepperPin5, LOW);
   delay(1000);
  }
 }
 


void loop() {
  delay(50);
  if (acc.isConnected()) {   
    acc.read(rcvmsg, sizeof(rcvmsg), 1);

    if (rcvmsg[0] == COMMAND_LED && rcvmsg[1] == TARGET_PIN_12) {
      byte value = rcvmsg[2];
      
      if (value == VALUE_ON){
        ingredient1value=rcvmsg[3] ;
        ingredient2value=rcvmsg[4] ;
        ingredient3value=rcvmsg[5] ;
        ingredient4value=rcvmsg[6] ;
        ingredient5value=rcvmsg[7] ;
        
         stepper1(ingredient1value);
         stepper2(ingredient2value);
         stepper3(ingredient3value);
         stepper4(ingredient4value);
         stepper5(5);

      }
    }
  }


}

The delayMicroseconds() calls are the obvious reason. You need to recast the logic using micros().

void loop() {
  delay(50);

Here’s another place where you do nothing for a while. There is no excuse for this one.

There are a couple of stepper libraries, AccelStepper and CustomrStepper, that operate stepper motors in non-blocking fashion (unlike your code) that bear investigation, too.

Why do the stepper functions take an int, when the sending device sends a byte?

I know that arduino ain’t the right plattform for multithreading,

Total CRAP even a Uno is capable of doing more than a 100 threads (its got 2k of SRAM after all).

Mark

Have a look at the demo several things at a time. It illustrates how to use millis() for non-blocking timing.

@holmes4, I'm not sure what 2k of SRAM has to do with the number of Threads. Time is usually the limiting factor.

...R