I Have 2 Arduino Codes that work separately …but i have no idea how to merge them

Code # 1) 2 Leds Red & Green Led Green is high for 4 seconds then low for 4 seconds Led Red is then high for 4 seconds then low for 4 seconds then the code 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`

}` Code # 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 codes work independently but are impossible to integrate as 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);
by kennybossss
August 01, 2020
Add comment...

Please sign in or create an account to comment.


1 Answer

Answer by vanderghast

Multiple solutions. The easiest one is define two subroutines, one which flash the LEDs, and a second one which turns the servo. Then, in the main loop, call each subroutine one after the other:

...

void loop() {

 sub1( );

 sub2( );

}

Note that the loop will take a total of all the delay( xxx ) it encounters. You can chose to use an argument, so it become easier to set the final acceptable delay when you use the subroutine in ONE place, instead of in multiple place (or define a unique constant symbol in the setup, and use that symbolic name instead of the constant 4000 in multiple places. Doing so, to change the delay everywhere, you just have to change it at one place, again.

You can also merge the original code in each section, less clean though:

void setup() {

code for setup the LED here

code for setup the servo here

}

void loop() {

 code to be repeated for the LEDS here

 code to be repeated for the servo here

}

I personally prefer the subroutine approach. After all, that is what delay() and digitalWrite( ) are and which you already use.

+1 vote
by vanderghast
August 02, 2020
Add comment...

Please sign in or create an account to comment.


Your Answer

You must log in or create an account (free!) to answer a question.

Log in Create an account


Go Ad-Free. Activate your CircuitLab membership. No more ads. Save unlimited circuits. Run unlimited simulations.

Search Questions & Answers


Ask a Question

Anyone can ask a question.

Did you already search (see above) to see if a similar question has already been answered? If you can't find the answer, you may ask a question.


About This Site

CircuitLab's Q&A site is a FREE questions and answers forum for electronics and electrical engineering students, hobbyists, and professionals.

We encourage you to use our built-in schematic & simulation software to add more detail to your questions and answers.

Acceptable Questions:

  • Concept or theory questions
  • Practical engineering questions
  • “Homework” questions
  • Software/hardware intersection
  • Best practices
  • Design choices & component selection
  • Troubleshooting

Unacceptable Questions:

  • Non-English language content
  • Non-question discussion
  • Non-electronics questions
  • Vendor-specific topics
  • Pure software questions
  • CircuitLab software support

Please respect that there are both seasoned experts and total newbies here: please be nice, be constructive, and be specific!

About CircuitLab

CircuitLab is an in-browser schematic capture and circuit simulation software tool to help you rapidly design and analyze analog and digital electronics systems.