Arduino programming with multiple loops I was trying to program my Arduino (along with a connected motor controller) to switch from cycle to cycle with this program.  It doesn't seem to work however.  There's still plenty of storage space and dynamic memory, so those aren't the issue.  This is the program:

int EnablePin1 = 8;
int duty;
int period;
int cycle;

int PWMPinA1 = 11;  
int PWMPinB1 = 3;
int PWMPinA3 = 6;  
int PWMPinB3 = 5;

const byte CPin1 = A0;  

int CRaw1;      
void setup() {

    Serial.begin(9600);

    pinMode(EnablePin1, OUTPUT);

    pinMode(PWMPinA1, OUTPUT);
    pinMode(PWMPinB1, OUTPUT);
    pinMode(PWMPinA3, OUTPUT);
    pinMode(PWMPinB3, OUTPUT);

}

void loop() {
    digitalWrite(EnablePin1, HIGH); //enable the board
    for (duty = 0; duty &lt;= 255; duty += 15) 
    {

        analogWrite(PWMPinB1, 0); //Set pinB to 0, when speed is written to pinA             

        analogWrite(PWMPinA1, 255);
        delay(850);
        if (PWMPinA1 == 11)
        {
            PWMPinA1 = 3;
            PWMPinB1 = 11;
        }
        else
        {
            PWMPinA1 = 11;
            PWMPinB1 = 3;
        }
   }
   analogWrite(PWMPinA1,0);//end at max speed
   analogWrite(PWMPinB1,0);
   CRaw1 = analogRead(CPin1);

   Serial.println("Feedback");

   Serial.print(CRaw1);


   delay(2000);
   //analogWrite(PWMPinB3, 0);
   for (duty = 255; duty &gt;= 0; duty -= 51) // ramp down speed
   {
       //analogWrite(PWMPinA1, duty);
       analogWrite(PWMPinB3, 0);
       analogWrite(PWMPinA3, 255);
       delay(period);
       //delay(cycle);
       if(PWMPinA3 == 6)
       {
           PWMPinA3 = 5;
           PWMPinB3 = 6;
           //period = 850;
       }
       else
       {
           PWMPinA3 = 6;
           PWMPinB3 = 5;
          //period = 2500;
       }
   }
   analogWrite(PWMPinA3, 0); //set to 0 speed
   analogWrite(PWMPinB3, 0);
   delay(2000);

   digitalWrite(EnablePin1, LOW);// Toggle enable to reset the power chips           if we have had an overcurrent or overtemp fault

   for (cycle = 1000; cycle &lt; 9000; cycle += 4000) 
   {
       //cycle=period;
       period=cycle;
   }

 /*for (cycle = 2000; cycle &lt;= 3000; cycle += 1000) 
   {
       //cycle=period;
       period=10000;
   }*/
   for (cycle = 9000; cycle &gt;= 1000; cycle -= 8000) 
   {
       period=0;
   }

   //delay(2000);  // Swap pins to make the motor change direction

}//end main loop


I've also tried having the cycle loops be this:

 for (cycle = 0; cycle &lt; 9000; cycle += 3000) 
 {
     //cycle=period;
     period=cycle;
 }
 for (cycle = 9000; cycle &gt;= 0; cycle -= 9000) 
 {
     //cycle=period/3;
     period=cycle;
 }


But that didn't do anything (I can see what PWM pins are powered through LEDs, and the first attempt, the LEDs only lit up for 1 second and the 2nd, they didn't light up at all.
First things, try to use multiple function and call it from the loop()

You can use some code magic for dealing with multitasking on Arduino. 
It's a bit tricky to understand but it's working. 

Here is an example :


const int BROCHE_LED_1 = 13;
const int BROCHE_LED_2 = 12;
const int BROCHE_LED_3 = 11;

const unsigned long BLINK_INTERVAL_1 = 1000;
const unsigned long BLINK_INTERVAL_2 = 500;
const unsigned long BLINK_INTERVAL_3 = 2000;

void setup() {

  pinMode(BROCHE_LED_1, OUTPUT);
  pinMode(BROCHE_LED_2, OUTPUT);
  pinMode(BROCHE_LED_3, OUTPUT);

  digitalWrite(BROCHE_LED_1, LOW);
  digitalWrite(BROCHE_LED_2, LOW);
  digitalWrite(BROCHE_LED_3, LOW);
}

void loop() {

  task_led1();
  task_led2();
  task_led3();
}

void task_led1() {
  static unsigned long previousMillisLed1 = 0;
  static byte etatBrocheLed1 = LOW;

  unsigned long currentMillis = millis();


  if(currentMillis - previousMillisLed1 &gt;= BLINK_INTERVAL_1) {

    previousMillisLed1 = currentMillis;

    etatBrocheLed1 = !etatBrocheLed1;
    digitalWrite(BROCHE_LED_1, etatBrocheLed1);
  }
}

void task_led2() {
  static unsigned long previousMillisLed2 = 0;
  static byte etatBrocheLed2 = LOW;

  unsigned long currentMillis = millis();


  if(currentMillis - previousMillisLed2 &gt;= BLINK_INTERVAL_2) {

    previousMillisLed2 = currentMillis;

    etatBrocheLed2 = !etatBrocheLed2;
    digitalWrite(BROCHE_LED_2, etatBrocheLed2);
  }
}

void task_led3() {
  static unsigned long previousMillisLed3 = 0;
  static byte etatBrocheLed3 = LOW;

  unsigned long currentMillis = millis();

  if(currentMillis - previousMillisLed3 &gt;= BLINK_INTERVAL_3) {

    previousMillisLed3 = currentMillis;

    etatBrocheLed3 = !etatBrocheLed3;
    digitalWrite(BROCHE_LED_3, etatBrocheLed3);
  }
}


Second things, Try to use Real time operating system

You can try to use FreeRTOS, it's work with most arduino and you got lot of infos on the web. 

It make multi tasking way more easy than the previous example. 

Here is the Docs i've found