After starting the automatic mode, I cant switch it off.

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 :slight_smile:

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);
  }
}

You do realize that loop( ) runs continuously?

Adjust your code to fit that scenario.

These "if"s should not be nested:

 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;
          }

ieee488:
You do realize that loop( ) runs continuously?

Adjust your code to fit that scenario.

Makes sense i guess, so i should make an other void for the automatic program?

boolrules:
These "if"s should not be nested:

 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;
          }

What is the problem with that? It does work.

HandigeHarry:
What is the problem with that? It does work.

Right. I didn't read closely enough.

Once you enter automatic mode, the delay() functions make the program not responsive to further button presses.

You will need to recraft the program to use non blocking techniques using millis() timers as shown in "blink without delay" or the tutorials on using millis() timers.

http://forum.arduino.cc/index.php?topic=223286.0

http://forum.arduino.cc/index.php?topic=503368.0

Are you using pullups or pulldowns on the button input switches. INPUT_PULLUP using the internal pullups is a good choice.

Thank you cattledog! That is probably my problem indeed. I will try using the millis().

I am using pullups for the switches indeed :wink: