Returning to a loop WITHOUT restarting it

So I’m working on a code that will control two motors, one DC and one stepper doing different but similar things. So although I know you can’t do things truly simultaneously in arduino, you can have things take turns really really quickly.

Both motors need to go to a certain spot than change direction. I was attempting to have a while or for loop that switched between the codes. Basically I’d have a while loop that kept track of how much each motor has traveled, than called the function to drive the two motors a certain amount before returning to the loop and driving the other motor a similar increment. Back and forth until they reach the limits.

When I try this I have loop(); at the end of the function, but that restarts loop; I can’t seem to remember or how to, but I need a command that returns to the loop() function but continues where it left off.

I thought I found something like that a while ago, but I can’t remember now.

Eyes and brain are foggy. :frowning:

Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]

Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components.
Posting images:

but I need a command that returns to the loop() function but continues where it left off.

If you call a function from loop() then when the called function ends the program will return to the next statement after the one that called the function. No need for anything special.

Incidentally, whilst you have not posted your code (HINT) while loops do not sound like the right solution to simultaneously moving 2 motors

Do you call the loop() from your function? It's silly. It is the main task which is called repeatedly. If you want to return back just finish your function normally. Maybe you could post your code.

Thank you all for the replies. We have actually decided to focus on one motor at a time for now, the one we’re having the most trouble with is a DC windshield whipper motor, we want this sucker to continuously (for now, later we’ll have a stop function) go back and forth betwee 0 and 120 degrees. We have an absolute encoder on it and the commented code in the 'forward_flick() works, and changing pin 8 from HIGH to LOW changes direction, but if I try to add any other conditions it either doesn’t turn at all or turns continuously in one direction

Here I’m trying to get it to go until it hits one of its bounds, then change direction and go to the other bound. As you can see I’m having dir == 1 be one direction and -1 be the other. Maybe using a boolean would be better, but I’m pretty sure it would be basically the same.

#include <SPI.h>

#define CS 3 //Chip or Slave select 

uint16_t ABSposition = 0;
uint16_t ABSposition_last = 0;
uint8_t temp[2];    //This one.

float deg = 0.00;
float tempDeg;
char x;

float stop1 = 0.0;
float stop2 = 120.0;
int dirDC = 1;

void setup(){
 
   // setup for encoder
   
Serial.begin(250000);
 pinMode(CS,OUTPUT);//Slave Select
 digitalWrite(CS,HIGH);
 SPI.begin();
 SPI.setBitOrder(MSBFIRST);
 SPI.setDataMode(SPI_MODE0);
 SPI.setClockDivider(SPI_CLOCK_DIV32);
 Serial.begin(115200);
 Serial.println("starting");
 Serial.flush();
 delay(2000);
 SPI.end();

//////dc motor setup

pinMode(8, OUTPUT); //set direction pin as output, dir
pinMode(11, OUTPUT); //set enable pin as output, pwm

 forward_flick(60,60,deg,1);

}
// more encoder setup

 uint8_t SPI_T (uint8_t msg)    //Repetive SPI transmit sequence
{
  uint8_t msg_temp = 0;  //vairable to hold recieved data
  digitalWrite(CS,LOW);     //select spi device
  msg_temp = SPI.transfer(msg);    //send and recieve
  digitalWrite(CS,HIGH);    //deselect spi device
  return(msg_temp);      //return recieved byte
}


//encoder function



//loop dc driver and encoder
void loop(){


/////encoder code

  uint8_t recieved = 0xA5;    //just a temp vairable
  ABSposition = 0;    //reset position vairable
  
  SPI.begin();    //start transmition
  digitalWrite(CS,LOW);
  
  SPI_T(0x10);   //issue read command
  
  recieved = SPI_T(0x00);    //issue NOP to check if encoder is ready to send
  
  while (recieved != 0x10)    //loop while encoder is not ready to send
  {
    recieved = SPI_T(0x00);    //cleck again if encoder is still working 
    delay(2);    //wait a bit
  }
  
  temp[0] = SPI_T(0x00);    //Recieve MSB
  temp[1] = SPI_T(0x00);    // recieve LSB
  
  digitalWrite(CS,HIGH);  //just to make sure   
  SPI.end();    //end transmition
  
  temp[0] &=~ 0xF0;    //mask out the first 4 bits
   
  ABSposition = temp[0] << 8;    //shift MSB to correct ABSposition in ABSposition message
  ABSposition += temp[1];    // add LSB to ABSposition message to complete message
   
  if (ABSposition != ABSposition_last)    //if nothing has changed dont wast time sending position
  {
    ABSposition_last = ABSposition;    //set last position to current position
    deg = ABSposition;
    deg = deg * 0.08789;// aprox 360/4096
    

   forward_flick( stop1,stop2,deg,dirDC);
  
    Serial.println(deg);     //send position in degrees
//    Serial.println(encoder_read);     //send position in degrees
//   }   
return(deg);
  delay(10);    //wait a bit till next check
 
  }
 }




void forward_flick(float point1, float point2, float degree, int dir) {

if(dir == 1){
   if (degree <= point2){
   digitalWrite (8,HIGH); //dir cw
   analogWrite (11,30);
   Serial.println("are you doing this?");
   }
   else if(degree > point2){
   digitalWrite (8,LOW); //dir cw
   analogWrite (11,30); 
   }
   else {
   analogWrite (11,0);

   
   } 
  return; 
}



//  if (degree > 120.0){
//    digitalWrite (8,LOW); //dir cw
//    analogWrite (11,30);
//    Serial.println("are you doing this?");
//    }
//    else {
//    analogWrite (11,0);
//    } 

}







////reading encoder
float encoder_read() {
//float deg;
uint16_t ABSposition = 0;
uint16_t ABSposition_last = 0;
uint8_t temp[2];    //This one.

uint8_t recieved = 0xA5;    //just a temp vairable
  ABSposition = 0;    //reset position vairable
  
  SPI.begin();    //start transmition
  digitalWrite(CS,LOW);
  
  SPI_T(0x10);   //issue read command
  
  recieved = SPI_T(0x00);    //issue NOP to check if encoder is ready to send
  
  while (recieved != 0x10)    //loop while encoder is not ready to send
  {
    recieved = SPI_T(0x00);    //cleck again if encoder is still working 
    delay(2);    //wait a bit
  }
  
  temp[0] = SPI_T(0x00);    //Recieve MSB
  temp[1] = SPI_T(0x00);    // recieve LSB
  
  digitalWrite(CS,HIGH);  //just to make sure   
  SPI.end();    //end transmition
  
  temp[0] &=~ 0xF0;    //mask out the first 4 bits
   
  ABSposition = temp[0] << 8;    //shift MSB to correct ABSposition in ABSposition message
  ABSposition += temp[1];    // add LSB to ABSposition message to complete message
   
  if (ABSposition != ABSposition_last)    //if nothing has changed dont wast time sending position
  {
    ABSposition_last = ABSposition;    //set last position to current position
    deg = ABSposition;
    deg = deg * 0.08789;// aprox 360/4096

   forward_flick( stop1,stop2, deg, dirDC); 
    
    Serial.println(deg);     //send position in degrees
  }   
return(deg);
  delay(10);    //wait a bit till next check

}

@Sginther

Other post DELETED

Please READ THIS POST to help you get the best out of the forum.

Bob.