Electronic – arduino – I have 2 Arduino programs that work separately. How to merge them

arduinoservo

Program 1

2 red LEDs & 1 green LED are high for 4 seconds then low for 4 seconds. Red is then high for 4 seconds then low for 4 seconds then it repeats.

int  LedR = 10;
int  LedG = 11;

void setup() {
// put your setup code here, to run once:
pinMode (LedR, OUTPUT);
pinMode (LedG, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite (LedG, HIGH); //Light Greed Led
delay (4000); //delay 4s

digitalWrite(LedG, LOW);//Low Greed Led

delay (4000); //delay 4s

digitalWrite(LedR, HIGH);//Light Red Led

delay (4000); //delay 4s

digitalWrite (LedR, LOW); //Low Greed Led
delay (4000); //delay 4s`



}

Program 2

Servo rotates 20 degrees waits for 4 seconds the returns to 0 degrees waits for 4 seconds then repeats.

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
myservo.attach(9);  // attaches the servo on pin 9 to the servo object

}


void loop() {

delay(4000);
             

for (pos = 0; pos <= 20; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos);              // tell servo to go to position in variable 'pos'
delay(15);                       // waits 15ms for the servo to reach the position
}delay(4000);

for (pos = 20; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos);              // tell servo to go to position in variable 'pos'
delay(15);                       // waits 15ms for the servo to reach the position

}
}

Both programs work independently but are impossible to integrate as the Arduino does not allow multiple loops.

I have tried to learn the millis function unsuccessfully.

Is there anyway to get the LEDs to light consecutively at the same time the servo rotates?

Please assist me. This is the code that does not work:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position
int  LedR = 10;
  int  LedG = 11;

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode (LedR,OUTPUT);
pinMode (LedG,OUTPUT);
  
}


void loop() {

  digitalWrite (LedG, HIGH); //Light Greed Led 
delay (4000); //delay 4s

digitalWrite(LedG, LOW);//Low Greed Led

delay (4000); //delay 4s

digitalWrite(LedR, HIGH);//Light Red Led

delay (4000); //delay 4s

digitalWrite (LedR, LOW); //Low Greed Led

delay (4000); //delay 4s
  
  delay(4000);

Best Answer

Instead of using a delay functions that holds everything up, configure or access a timer peripheral that keeps counting in the background regardless of what the Arduino is doing whose value can be checked whenever you need to.

For example: https://www.arduino.cc/reference/en/language/functions/time/millis/

That timer rolls over every 50 days so if you never plan on leaving your thing running that long, you can treat it as timer for absolute time. If you are running longer than 50 days then you have to add some extra if else checks for when the timer wraps around.

Then use two conditional statements for your two other functions in the loop to poll that timer to see whether they should be doing anything on that particular loop iteration. Those functions can record a value from the clock at events of significance and compare it against the current value to see if enough time has elapsed. You can use global variables for this, but static variables (if Arduino allows them) are safer since they are only visible within the function and retain their value between function calls. You also don't have timekeeping global variable floating around for every single function that are accessible to everything.

That way, if the Arduino isn't supposed to be doing anything at that particular time for that particular action, it can go on and do something else instead of twiddling its thumbs waiting around and holding everything after that up. Pretty much never use a delay function that just holds the processor preventing it from doing anything else if you can. Toss that delay() in the trash where it belongs 99% of the time.

Welcome to the world of multi-tasking!