Help with Slide Switch and Loop esp32

I am trying to make a windscreen wiper project with a 6 pin Slide Switch but the loop does not keep repeating. Here is my Code:

//Library Downloads
#include <ESP32Servo.h>

//Servo Setup
int servoPin = 17;
int servoVal = 0;
Servo myServo;

int led1 = 23;
int led2 = 21;

int button1 = 19;
int button2 = 18;

int state1;
int state2;

int j;

int dt = 100;

void setup() {

  Serial.begin(115200);
  
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);

  //Attach and Writing to the Servo
  myServo.attach(servoPin);
  myServo.write(servoVal);

}

void loop() {
  state1 = digitalRead(button1);
  state2 = digitalRead(button2);

  Serial.print("State1 = ");
  Serial.println(state1);
  
  Serial.print("State2 = ");
  Serial.println(state2);


  if (state1 == 0) {
    digitalWrite(led1, HIGH);
    j=0;
    do{
      Serial.println(j);
      servoVal = 180;
      myServo.write(servoVal);
      delay(dt);
      servoVal = 0;
      myServo.write(servoVal);
    }while(state2 == 1);
  }
  else {
    digitalWrite(led1, LOW);
    servoVal = 0;
    myServo.write(servoVal);
  }

  if (state2 == 0) {
    digitalWrite(led2 , HIGH);
  }
  else {
    digitalWrite(led2, LOW);
  }
}

Your topic was MOVED to its current forum category as it is more suitable than the original as it has nothing to do with Installation and Troubleshooting of the IDE

This loop keeps looping until variable state2 is unequal to 1
But you never change the value of variable state2 inside the loop
once your code has entered this do-while-loop it stays inside because there is nothing like

state2 = digitalRead(button2);

inside the do-while-loop

Thank you I will try it.

What exactly is the 6 pin switch ?

Please post a schematic of your project showing how its components are connected. A picture of a hand drawn circuit is good enough

How is the project, and particularly the servo, powered ?

In this do/while loop

    do{
      Serial.println(j);
      servoVal = 180;
      myServo.write(servoVal);
      delay(dt);
      servoVal = 0;
      myServo.write(servoVal);
    }while(state2 == 1);
what will change the value of state2 once the loop has been entered ?  You read the value of button2 just once in setup() and never again

It works, but on the Slide Switch there is a middle switch and when it is held down it nothing because there is no connection and the middle is just GND.

Post the schematic and a link to where you got the switch from and there is a chance that we may be able to help

My cousin gave it and I don't know where he got it from

This is the switch.

Your original picture

Does the switch have 3 positions or only 2 ?

It has 3 positions. Sorry I sent the wrong one at first.

Are you sure that you can move the knob to a defined middle-position?

Defined means you start moving it against same resistance and then it will snap-in almost from alone into a middle-position?

Yes you can.

Yes the picture shows a middle-position.

OK. Describe in normal words what your servo shall do if switch is

  • in left position
  • in middel position
  • in right position

?

Right button makes the Servo move at a slow speed and middle button to stop (but the connection is ground and no pin) and left button is to make the Servo move at a fast speed.

This can be solved through using the internal pull-up-resistors
pull-up-resistors invert the logic of the switch/button

If the switch is opened the IO-pin detects +5V = HIGH through the pull-up-resistor
The name is program: the pull-up-resistor pulls-up the voltage to +5V

If the switch is closed the voltages goes down to 0V = LOW

So closed switch means IO-pin detects LOW
opened switch means IO-pin detects HIGH
which is is opposite to what your everyday experiences with for example light-switches in houses do
switch closed => Lamp ON = "HIGH"
switch opened => Lamp OFF = "LOW"

right contact closed = LOW => slow moving
both contacts opened = both HIGH => stop
left contact closes = LOW => fast moving

there is a library called controlledServo that allows to automatically slow down the movement of servos

This library has a function .setRate(angleRate)

where you can set the speed how fast the servo shall turn

best regards Stefan

@StefanL38 thank you but now the servo moves until I move it to state2.

informatic project: most needed: information
post your most actual complete sketch

There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting
//Library Downloads
#include <ESP32Servo.h>

//Servo Setup
int servoPin = 17;
int servoVal = 0;
Servo myServo;

int led1 = 23;
int led2 = 21;

int button1 = 19;
int button2 = 18;

int state1;
int state2;

int j;

int dt = 850;

void setup() {

  Serial.begin(115200);

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);

  //Attach and Writing to the Servo
  myServo.attach(servoPin);
  myServo.write(servoVal);

}

void loop() {
  state1 = digitalRead(button1);
  state2 = digitalRead(button2);

  Serial.print("State1 = ");
  Serial.println(state1);

  delay(850);

  Serial.print("State2 = ");
  Serial.println(state2);


  if (state1 == 0) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, LOW);
    j = 0;
    do {
      Serial.print("State1 = ");
      Serial.println(state1);
      delay(850);
      Serial.print("State2 = ");
      Serial.println(state2);

      state2 = digitalRead(button2);
      Serial.println(servoVal);
      servoVal = 180;
      myServo.write(servoVal);
      delay(850);
      servoVal = 0;
      myServo.write(servoVal);
      Serial.println(servoVal);
    } while (state2 == 1);
  }
  else {
    digitalWrite(led1, LOW);
    servoVal = 0;
    myServo.write(servoVal);
  }

  if (state2 == 0) {
    digitalWrite(led2 , HIGH);
    digitalWrite(led1, LOW);
    do {
      state1 = digitalRead(button1);
      Serial.print("State1 = ");
      Serial.println(state1);
      delay(850);
      Serial.print("State2 = ");
      Serial.println(state2);

      state2 = digitalRead(button2);
      Serial.println(servoVal);
      servoVal = 180;
      myServo.write(servoVal);
      delay(700);
      servoVal = 0;
      myServo.write(servoVal);
      Serial.println(servoVal);
    } while (state1 == 1);
  }
  else {
    digitalWrite(led2, LOW);
  }
}

OK very good.

what does it mean if your three-position switch is in position that button1 == 0 is true

shall it mean slow moving
or
shall it mean fast moving ?

What does it indicate if led1 is switched ON led2 is switched off?
shall it mean slow-mode active
or
shall it mean fast-mode active?

additionally a normal worded description of the wanted functionality would be very helpful.

I try to describe it what your wanted functionality is:

If your three-position-switch is in position left move servo slowly
If your three-position-switch is in position right move servo fast
If your three-position-switch is in position middle stand still (maybe moving servo to position 180 or position 0)

is this right?
please confirm or correct the description
best regards Stefan