Hello, first time posting.
Have been working on this project for some days.
Got is all working pretty well with combining codes and adjusting to my needs.
But now when I press start the automatic loop, and I press the button again to switch it off. It stays in the automatic loop for ever. I also cant switch to manual when the automatic loop started.
So my question is, how do i break out of the automatic loop when it is running to turn it off.
All help is appreciated ![]()
const int vert_motor1Â = 2;Â Â //pin 2 of arduino to pin 7 of l293d
const int vert_motor2Â = 3;Â Â //pin 3 of arduino to pin 2 of l293d
const int horz_motor1Â = 7;Â Â Â //pin 7 of arduino to pin 10 of l293d
const int horz_motor2Â = 8;Â Â Â //pin 8 of arduino to pin 15 of l293d
const int mvert = 9;
const int mhorz = 10;
int joyvert = A0;
int joyhorz = A1;
int vert_motor_led = 6;
int horz_motor_led = 4;
int modeSwitchPin = 13;Â Â Â Â // switch is connected to pin 13
int ledAutoPin  = 11;
int ledManPin  = 12;
int val;Â Â Â Â Â Â Â Â Â Â Â Â // variable for reading the pin status
int val2;Â Â Â Â Â Â Â Â Â Â Â // variable for reading the delayed status
int modeButtonState;Â Â Â Â Â Â // variable to hold the button state
int Mode = 0;Â Â
int startauto = 5;
int val3;
int val4;
int startButtonState;
int state = 0;
int speed_vert = 0;
int speed_horz = 0;
int joyposvert = 512;
int joyposhorz = 512;
void setup() {
 // put your setup code here, to run once:
 Serial.begin(9600);
 pinMode(vert_motor1, OUTPUT);
 pinMode(vert_motor2, OUTPUT);
 pinMode(horz_motor1, OUTPUT);
 pinMode(horz_motor2, OUTPUT);
 pinMode(mvert, OUTPUT);
 pinMode(mhorz, OUTPUT);
 pinMode(vert_motor_led, OUTPUT);
 pinMode(horz_motor_led, OUTPUT);
 pinMode(modeSwitchPin, INPUT);  // Set the switch pin as input
 pinMode(startauto, INPUT);
 pinMode(ledAutoPin, OUTPUT);
 pinMode(ledManPin, OUTPUT);
 modeButtonState = digitalRead(modeSwitchPin); // read the initial state
 startButtonState = digitalRead(startauto); // read the initial state
}
void loop() {
 //***AUTO/MANUAL SWITCH LOOP***//
 val = digitalRead(modeSwitchPin);   // read input value and store it in val
 delay(10);              // 10 milliseconds is a good amount of time
 val2 = digitalRead(modeSwitchPin);  // read the input again to check for bounces
 if (val == val2) {          // make sure we got 2 consistant readings!
  if (val != modeButtonState) {    // the button state has changed!
   if (val == LOW) {         // check if the button is pressed
    if (Mode == 0) {
     Mode = 1;
    } else {
     if (Mode == 1) {
      Mode = 0;
     }
    }
   }
  }
 }
 modeButtonState = val;        // save the new state in our variable
   joyposvert = analogRead(joyvert);
   joyposhorz = analogRead(joyhorz);
 if (Mode == 0)
 {
   digitalWrite(ledAutoPin, HIGH);
   digitalWrite(ledManPin, LOW);
   Serial.println("Automatic Mode");
  Â
  val3 = digitalRead(startauto);   // read input value and store it in val
  delay(10);             // 10 milliseconds is a good amount of time
  val4 = digitalRead(startauto);   // read the input again to check for bounces
  if (val3 == val4) {        // make sure we got 2 consistant readings!
   if (val3 != startButtonState) { // the button state has changed!
    if (val3 == LOW) {       // check if the button is pressed
     if (state == 0) {
      state = 1;
     } else {
      if (state == 1) {
      state = 0;
     }
    }
   }
  }
 }
  startButtonState = val3;      // save the new state in our variable
  if (state == 0) { // all-off
   Serial.println("Stop automatisch programma");
   digitalWrite(vert_motor_led, LOW);
   digitalWrite(horz_motor_led, LOW);
   digitalWrite(vert_motor1, LOW);
   digitalWrite(vert_motor2, LOW);
   digitalWrite(horz_motor1, LOW);
   digitalWrite(horz_motor2, LOW);
   analogWrite(mvert, 0);
   analogWrite(mhorz, 0);
  }
  if (state == 1) {
   Serial.println("Start automatisch programma");
   digitalWrite(vert_motor1, LOW);
   digitalWrite(vert_motor2, HIGH);
   digitalWrite(vert_motor_led, HIGH);
   analogWrite(mvert, 100);
   delay(5000);
   digitalWrite(vert_motor1, LOW);
   digitalWrite(vert_motor2, LOW);
   digitalWrite(vert_motor_led, LOW);
   analogWrite(mvert, 0);
   delay(2000);
   digitalWrite(vert_motor1, HIGH);
   digitalWrite(vert_motor2, LOW);
   digitalWrite(vert_motor_led, HIGH);
   analogWrite(mvert, 100);
   delay(5000);
   digitalWrite(vert_motor1, LOW);
   digitalWrite(vert_motor2, LOW);
   digitalWrite(vert_motor_led, LOW);
   analogWrite(mvert, 0);
   delay(2000);
  }
 }
 if (Mode == 1)
 {
  digitalWrite(ledAutoPin, LOW);
  digitalWrite(ledManPin, HIGH);
  Serial.println("MANUAL MODE");
 Â
 if (joyposvert < 470)
 {
   digitalWrite(vert_motor1, LOW);
   digitalWrite(vert_motor2, HIGH);
   digitalWrite(vert_motor_led, HIGH);
   speed_vert = map(joyposvert, 470, 0, 0, 255);
 }
 else if (joyposvert > 550)
 {
   digitalWrite(vert_motor1, HIGH);
   digitalWrite(vert_motor2, LOW);
   digitalWrite(vert_motor_led, HIGH);
   speed_vert = map(joyposvert, 550, 1023, 0, 255);
 }
 else
 {
   speed_vert = 0;
   speed_horz = 0;
   digitalWrite(vert_motor_led, LOW);
   digitalWrite(horz_motor_led, LOW);
 }
 if (joyposhorz < 470)
 {
   digitalWrite(horz_motor1, LOW);
   digitalWrite(horz_motor2, HIGH);
   digitalWrite(horz_motor_led, HIGH);
   speed_horz = map(joyposhorz, 470, 0, 0, 255);
 }
 else if (joyposhorz > 550)
 {
   digitalWrite(horz_motor1, HIGH);
   digitalWrite(horz_motor2, LOW);
   digitalWrite(horz_motor_led, HIGH);
   speed_horz = map(joyposhorz, 550, 1023, 0, 255);
 }
 if (speed_vert < 8)speed_vert = 0;
 if (speed_horz < 8)speed_horz = 0;
   analogWrite(mvert, speed_vert);
   analogWrite(mhorz, speed_horz);
 }
}