Newbie Having Programming Problems.

I'm trying to add a second concurrent loop to codes for some LEDs that's working correctly. When I try to add the second loop I can no longer compile.

Can anyone troubleshoot this for me?
Thanks!

#include <Scheduler.h>


int Trunk1APin = 9;   // Red LED,   connected to digital pin 9
int Trunk1BPin = 10;  // Green LED, connected to digital pin 10
int Trunk1CPin = 11;  // Blue LED,  connected to digital pin 11
int Trunk2APin = 3;   // Red LED,   connected to digital pin 9
int Trunk2BPin = 5;  // Green LED, connected to digital pin 10
int Trunk2CPin = 6;  // Blue LED,  connected to digital pin 11
int CanopyPin = 2;

// Program variables
int redVal   = 255; // Variables to store the values to send to the pins
int greenVal = 1;   // Initial values are Red full, Green and Blue off
int blueVal  = 1;

int i = 0;     // Loop counter    
int wait = 50; // 50ms (.05 second) delay; shorten for faster fades
int DEBUG = 0; // DEBUG counter; if set to 1, will write values back via serial





void setup()
{
  {Scheduler.startLoop(loop2);}
   
  pinMode(Trunk1APin, OUTPUT);   // sets the pins as output
  pinMode(Trunk1BPin, OUTPUT);   
  pinMode(Trunk1CPin, OUTPUT); 
  pinMode(Trunk2APin, OUTPUT);
  pinMode(Trunk2BPin, OUTPUT);
  pinMode(Trunk2CPin, OUTPUT);
  if (DEBUG) {           // If we want to see the pin values for debugging...
    Serial.begin(9600);  // ...set up the serial ouput on 0004 style
  }
  
  
}

// Main program
void loop()
{
  i += 1;      // Increment counter
  if (i < 255) // First phase of fades
  {
    redVal   -= 1; // Red down
    greenVal += 1; // Green up
    blueVal   = 1; // Blue low
  }
  else if (i < 509) // Second phase of fades
  {
    redVal    = 1; // Red low
    greenVal -= 1; // Green down
    blueVal  += 1; // Blue up
  } 
  else if (i < 763) // Third phase of fades
  {
    redVal  += 1; // Red up
    greenVal = 1; // Green low
    blueVal -= 1; // Blue down
  }
  else // Re-set the counter, and start the fades again
  {
    i = 1;
  }  

  analogWrite(Trunk1APin,   redVal);   // Write current values to LED pins
  analogWrite(Trunk1BPin, greenVal); 
  analogWrite(Trunk1CPin,  blueVal);
  analogWrite(Trunk2APin, redVal);
  analogWrite(Trunk2BPin, greenVal);
  analogWrite(Trunk2CPin, blueVal);  
  
 
  if (DEBUG) { // If we want to read the output
    DEBUG += 1;     // Increment the DEBUG counter
    if (DEBUG > 10) // Print every 10 loops
    {
      DEBUG = 1;     // Reset the counter

      Serial.print(i);       // Serial commands in 0004 style
      Serial.print("\t");    // Print a tab
      Serial.print("R:");    // Indicate that output is red value
      Serial.print(redVal);  // Print red value
      Serial.print("\t");    // Print a tab
      Serial.print("G:");    // Repeat for green and blue...
      Serial.print(greenVal);
      Serial.print("\t");    
      Serial.print("B:");    
      Serial.println(blueVal); // println, to end with a carriage return
    }
  }
yield();  
}
void loop2()
{
  delay(wait); // Pause for 'wait' milliseconds before resuming the loop

  int delayTime = 500; // time (milliseconds) to pause between LEDs
                       // make this smaller for faster switching

  // turn all the LEDs off, then on kind of random:

  digitalWrite(CanopyPin, HIGH);   
  delay(delayTime);
  digitalWrite(CanopyPin, LOW);   
  delay(delayTime); 
  yield();          
}

That's simply because you can only have one loop() :wink: The micro can't do two things at the same time. So you just have to fit them quickly after each other aka alternate what you do :slight_smile:

Thanks, septillion. What controllers can do multiple loops? Eventually this is going on a Mega. Will that handle it?

You can have multple for-loops, while() loops and do-while() loops.

You put those loops inside your main loop, or in your yield() function, and you can have nested loops.

loop2() might work as an inline function, but it's not a loop. That is, it will only run once when the yield() function is called.

Jackie9:
What controllers can do multiple loops? Eventually this is going on a Mega. Will that handle it?

Multi core processors like the one in your PC. In micro controller land, not that much, certainly not on entry level. So no, the Mega can't do multiple loops. But you don't need it. Just switch between the two tasks you want to preform. As long as you don't make a blocking task (like using delay() ) you're fine :slight_smile:

You may get some ideas from the demo Several Things at a Time

...R

To use the "Scheduler" library, you need an Arduino Due:-
Scheduler

Scheduler
The Scheduler library enables the Arduino Due to run multiple functions at the same time. This allows tasks to happen without interrupting each other.

Edit: It can be installed via the 'Library Manager' in the IDE, but I should add that I've never tested it - I don't own a Due.

Edit2: Out of interest, I just installed the Arduino SAM boards package V1.6.4 using 'Boards Manager', and the code in the opening post compiled fine. First I tried package V1.6.7, but struck errors and found recommendations to use V1.6.4.
Now I want a Due to test it. :roll_eyes:

Hey Steve, I've got Scheduler running on a Due all the way up to 1.68. Ironically I couldn't get it to work on 1.65 (but a lot of things didn't) I'm just checking to see if we tweaked it all to get this to happen.

And to be fair, as you know, it's not really multiprocessing on the Due, the cpu is just capable of switching between tasks.

And why the need to do both of these things "at the same time"? Are you getting flickers in the LEDs while its off doing other things? Or jumps in brightness as it catches up?

ardshark:
Hey Steve, I've got Scheduler running on a Due all the way up to 1.68. Ironically I couldn't get it to work on 1.65 (but a lot of things didn't) I'm just checking to see if we tweaked it all to get this to happen.

And to be fair, as you know, it's not really multiprocessing on the Due, the cpu is just capable of switching between tasks.

Do you mean all the way up to "Arduino SAM boards package V1.6.8", or IDE V1.6.8?

In the thread on the subject that I found, the problems were reported with the Arduino SAM boards package, versions 1.6.5 to 1.6.6. As mentioned, I tried "Arduino SAM boards package V1.6.7" and had the same problem compiling.
In that thread, V1.6.4 was recommended and fixed most people's troubles, then when I tried it, my problem was also solved.
The actual compilation error was:-

arm-none-eabi-gcc: error: C:\Users\[user name]\AppData\Local\Temp\build9100607786597507022.tmp/core/syscalls_sam3.c.o: No such file or directory

Here's a link to the relevant thread:-
Solved: Arduino Due syscalls_sam3.c.o error

Ok good news and bad news. IDE1.68 but with 1.66 SAM. Sorry for the false alarm. And I think I know why we've been able to get 1.66 working where others couldn't. (and I'm pretty sure we had 1.65 in there at one point too)
We had a project where we needed a lot of Due's and we just couldn't get them so we built our own. We made a few mods (including that annoying reset issue). In fact we didn't even know there was a compile problem until after we were done and people were complaining about 1.66 SAM. So I think we lucked out there. I'm so used to calling them Due's I forgot they are not official boards.
I've got a bunch of genuine Due's here I'll fire up and see how high they can go.