Arduino robot programming help?

Hello.

I need you help again. I am using an arduino UNO with L298N motor driver and robot chassis kit with two DC motors from ebay.

I wrote a simple code for robot. When I press the button wheels is spining forward for 1 second, than wheels is spining backward, but I can't understand why wheels didn't stop after one second? When I press the button second time wheels stop.

#define forwardLeft 7
#define backwardLeft 8
#define pwmLeft 9
#define forwardRight 4
#define backwardRight 12
#define pwmRight 10

#define startStopButton 2
int state = 0;
int buttonState;

void setup() {

  pinMode(forwardLeft, OUTPUT);
  pinMode(backwardLeft, OUTPUT);
  pinMode(pwmLeft, OUTPUT);
  pinMode(forwardRight, OUTPUT);
  pinMode(backwardRight, OUTPUT);
  pinMode(pwmRight, OUTPUT);

  pinMode(startStopButton, INPUT);
  Serial.begin(9600);

}

void loop() {

  buttonState = digitalRead(startStopButton);

  if (buttonState == HIGH) {

    if (state == 0) {

      state = 1; {

        normalMode();
      }
    }
    else {

      state = 0; {

        stopMode ();
      }
    }
  }
}


void normalMode (void) {

  digitalWrite(forwardLeft, HIGH);
  digitalWrite(backwardLeft, LOW);
  analogWrite(pwmLeft, 88);
  digitalWrite(forwardRight, HIGH);
  digitalWrite(backwardRight, LOW);
  analogWrite(pwmRight, 88);
  Serial.print(buttonState);
  delay(1000);
  digitalWrite(forwardLeft, LOW);
  digitalWrite(backwardLeft, HIGH);
  analogWrite(pwmLeft, 88);
  digitalWrite(forwardRight, LOW);
  digitalWrite(backwardRight, HIGH);
  analogWrite(pwmRight, 88);
  delay(1000);
}

void stopMode (void) {

  digitalWrite(forwardLeft, LOW);
  digitalWrite(backwardLeft, LOW);
  analogWrite(pwmLeft, 0);
  digitalWrite(forwardRight, LOW);
  digitalWrite(backwardRight, LOW);
  analogWrite(pwmRight, 0);
  Serial.print(buttonState);
  delay(1000);
}

Have you got a pull-down on pin 2?

AWOL: Have you got a pull-down on pin 2?

Yes, I got it.

I tryed it on MEGA 2560 and the same problem. Backward spining without time delay.

      state = 1; {

        normalMode();
      }

What are the curly braces for? Random curly braces do not make your code better.

Why don't you confirm that the switch is being read properly? Print it's state every time you read it.

Post a schematic showing how the switch is wired. Using the internal pullup resistor is so much easier than wiring the switch your way (whatever your way is).

PaulS: ```      state = 1; {

       normalMode();      }



What are the curly braces for? Random curly braces do not make your code better.

Why don't you confirm that the switch is being read properly? Print it's state every time you read it.

Post a schematic showing how the switch is wired. Using the internal pullup resistor is so much easier than wiring the switch your way (whatever your way is).

My button schematic is same as arduino button tutorial. https://www.arduino.cc/en/Tutorial/Button

Really no one can help me? I tryed a lot of google codes for arduino start/stop button, but without results. :frowning:

My latest code:

#define forwardLeft 7
#define backwardLeft 8
#define pwmLeft 9
#define forwardRight 4
#define backwardRight 12
#define pwmRight 10

#define startStopButton 2
int state = 0;
int buttonState = 0;

void setup() {

  pinMode(forwardLeft, OUTPUT);
  pinMode(backwardLeft, OUTPUT);
  pinMode(pwmLeft, OUTPUT);
  pinMode(forwardRight, OUTPUT);
  pinMode(backwardRight, OUTPUT);
  pinMode(pwmRight, OUTPUT);

  pinMode(startStopButton, INPUT);

}

void loop() {

  buttonState = digitalRead(startStopButton);

  if (buttonState == HIGH) {

    if (state == 0) {

      state = 1;
      normalMode();

    }
    else {

      state = 0;
      stopMode ();
    }
  }
}


void normalMode (void) {

  // Move forward
  digitalWrite(forwardLeft, HIGH);
  digitalWrite(backwardLeft, LOW);
  analogWrite(pwmLeft, 88);
  digitalWrite(forwardRight, HIGH);
  digitalWrite(backwardRight, LOW);
  analogWrite(pwmRight, 88);
  delay(2000);
  // Move backward
  digitalWrite(forwardLeft, LOW);
  digitalWrite(backwardLeft, HIGH);
  analogWrite(pwmLeft, 88);
  digitalWrite(forwardRight, LOW);
  digitalWrite(backwardRight, HIGH);
  analogWrite(pwmRight, 88);
  delay(2000);
}

void stopMode (void) {

  // STOP
  digitalWrite(forwardLeft, LOW);
  digitalWrite(backwardLeft, LOW);
  analogWrite(pwmLeft, 0);
  digitalWrite(forwardRight, LOW);
  digitalWrite(backwardRight, LOW);
  analogWrite(pwmRight, 0);
  delay(1000);
}

Really no one can help me?

No, because you won't help yourself. We asked for a schematic. You posted some text that looked like a URL, but wasn't a link. The link icon isn't that hard to use.

You have WAY to much code for someone who isn't even sure the switch works.

You haven't looked at the state change detection example, which shows the proper way to read switches.

https://files.fm/g/zy2aq8a9#aa/Button_schematic.jpg

Now it's better?