How to run two void loop in one Arduino code

Hi,

I want to run "loop 1" if switch button is high and "loop 2" if switch button is low.
how to do this?
I am trying with below code but its not working
in loop 1, servo will be operated if photoresistor value change above 50 and
in loop 2, servo will be operated through POT.
this is for example, actually I am trying to write bigger code and want to avoid if-else statement, hence checking how to write code for multiple loops.

#include <Servo.h>

int LDR = 0;
int POT = 0;
int SERVOVAL = 0;
Servo servo_11;
int buttonState = 0;

void setup()
{
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
servo_11.attach(11, 500, 2500);
}

void loop()
{
buttonState = digitalRead(A2);
if (buttonState == HIGH) {
loop1();
} else {
loop2();
}
}

void loop1()
{
LDR = analogRead(A0);
if (LDR > 50) {
servo_11.write(90);
delay(10);
}
}

void loop2()
{
POT = analogRead(A1);
SERVOVAL = map(POT, 0, 1023, 0, 180);
servo_11.write(SERVOVAL);
delay(10);
}

I think your code should work just like this

void loop() {
    checkButton();
    loop1();
    loop2();
}

void checkButton()
{
  buttonState = digitalRead(A2);
  if (buttonState == HIGH) {
    loop1();
    } else {
    loop2();
  }
}


void loop1()
{   
  LDR = analogRead(A0);
  if (LDR > 50) {
    servo_11.write(90);
    delay(10);
  }
}
  
void loop2()
{
  POT = analogRead(A1);
  SERVOVAL = map(POT, 0, 1023, 0, 180);
  servo_11.write(SERVOVAL);
  delay(10);
}

You will make your code much easier to develop and debug if you give your functions meaningful names. For example checkLDR() rather than loop1()

...R

1 Like

Thank you Very much!!

following program worked now.
Now both loops are running using If-else condition.
Thanks

#include <Servo.h>

int LDR = 0;
int POT = 0;
int SERVOVAL = 0;
Servo servo_11;

void setup()
{
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
servo_11.attach(11, 500, 2500);
Serial.begin(9600);
}

void loop()
{
servo_11.write(30);
int buttonState = digitalRead(A2);
if (buttonState == HIGH) {
loop1();
} else {
loop2();
}
}

void loop1()
{
LDR = analogRead(A0);
Serial.println(LDR);
if (LDR > 50) {
servo_11.write(90);
delay(10);
int Loop1_Status = servo_11.read();
Serial.print("Loop1_Status = ");
Serial.println(Loop1_Status);
}
}

void loop2()
{
POT = analogRead(A1);
SERVOVAL = map(POT, 0, 1023, 0, 180);
servo_11.write(SERVOVAL);
delay(10);
int Loop2_Status = servo_11.read();
Serial.print("Loop2_Status = ");
Serial.println(Loop2_Status);
}

Now both loops are running using If-else condition.

I feel bound to point out that neither of them are actually loops as they execute their code once and then return. There is nothing wrong with that but if you gave each function a meaningful name it would help understanding the program. Auto Formatting the code in the IDE and posting here in code tags would also be helpful

1 Like

Also there is no good reason to have POT, LDR and SERVOVAL as global variables.
Or, indeed, be in all caps, which is normally reserved for constants.

Plus, Loop2_Status will always return the last value you wrote to the servo - it doesn't indicate that the servo actually reached that position.

UKHeliBob:
I feel bound to point out that neither of them are actually loops as they execute their code once and then return. There is nothing wrong with that but if you gave each function a meaningful name it would help understanding the program. Auto Formatting the code in the IDE and posting here in code tags would also be helpful

Hi,
Thank you for you reply.
Actually I was trying to figure out how I can run two codes in separate loop using if-else condition.
Above example was just for trial, actually I am trying to code for one application where I have ended up with two codes, which I need to run with if-else.
Thank you for telling about Auto-formatting, it is learning for me, I will surely use that.
Thanks

Be careful how you program each of the 2 code sections that loop because the way that the program is currently structured the button pin will not be read whilst the looping function is running

It would be better to have each function do only one step of the "loop" then return. Something like this

const byte buttonPin = A3;
byte counter1;
byte counter2;

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop()
{
  byte buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH)
  {
    func1();
  }
  else
  {
    func2();
  }
}

void func1()
{
  Serial.print("counter 1 : ");
  Serial.println(counter1);
  counter1++;
}

void func2()
{
  Serial.print("counter 2 : ");
  Serial.println(counter2);
  counter2++;
}

Do not put any code in the "looping" functions to stop them running freely. Try adding a delay(1000) in each of them and see how unresponsive the code becomes

TheMemberFormerlyKnownAsAWOL:
Also there is no good reason to have POT, LDR and SERVOVAL as global variables.
Or, indeed, be in all caps, which is normally reserved for constants.

Plus, Loop2_Status will always return the last value you wrote to the servo - it doesn't indicate that the servo actually reached that position.

Hi,
Thanks for your reply.
CAPITAL letters are reserved for constants is learning for me, I will remember that.
regarding Loop2_Status, actually I want to fetch actual status of Servo, how can I do that?
Thanks

UKHeliBob:
Be careful how you program each of the 2 code sections that loop because the way that the program is currently structured the button pin will not be read whilst the looping function is running

It would be better to have each function do only one step of the "loop" then return. Something like this

const byte buttonPin = A3;

byte counter1;
byte counter2;

void setup()
{
 Serial.begin(115200);
 while (!Serial);
 pinMode(buttonPin, INPUT_PULLUP);
}

void loop()
{
 byte buttonState = digitalRead(buttonPin);
 if (buttonState == HIGH)
 {
   func1();
 }
 else
 {
   func2();
 }
}

void func1()
{
 Serial.print("counter 1 : ");
 Serial.println(counter1);
 counter1++;
}

void func2()
{
 Serial.print("counter 2 : ");
 Serial.println(counter2);
 counter2++;
}



Do not put any code in the "looping" functions to stop them running freely. Try adding a delay(1000) in each of them and see how unresponsive the code becomes

Sure, thanks for suggestion.
I was trying with my code again,
when the button is high or low, respective loop function works correctly.
but when button is not connected or during transition from high to low, or low to high, - servo gets unstable and rapid movements happen.
Alongwith above commands is there any command necessary which stores previous state of button?

garyp36:
regarding Loop2_Status, actually I want to fetch actual status of Servo, how can I do that?

Without additional hardware, you can't

If the button is not connected and the servo starts moving rapid this is where electronic-knowledge chimes in.

If you have some wiring connectec to an IO-pin with the IO-pin configured as input

this input is very sensitiy even a 10cm wire will act as antenna and catch up electrical noise which makes the IO-pin switch fast from low to high and back.

A transition from low to high or high to low can't be done "slowly" by hand. A mechanical switch is opened or closed.
nothing inbetween. What does happen with mechanical switches is bouncing. bouncing means the conatcts open/close for a short moment multiple times. An Arduino is fast eniugh to detect each opening/closing a s a single button-press even if this button"-press" is only 1 millisecond long.

This is the reason why most programs use DE-bouncing when working with mechanical switches.
If you do not have a digital multimeter this is the right time to buy one.
Any digital multimeter in the 20 to 40 dollar range will do.
Look for one that can measure duty-percentage and frequency
And if you can afford another 10 dollars buy a 24 MHz 8 channel logic analyser.
Which cworks together with the freeware pusleview.

If you go on tinkering with microcontrollers and sensors this will be very helpful

best regards Stefan

when button is not connected or during transition from high to low, or low to high, - servo gets unstable and rapid movements happen.

Note the use of INPUT_PULLUP in pinMode() in the code in reply #6 to activate the internal pullup resistor

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.