At the end the motors keep going, how do I get them to stop?
#include <Servo.h>
Servo servoarm9; // create servo object to control a servo
Servo servoswitch10; // create servo object to control a servo
int pos = 0; // variable to store the servo position
// this constant won't change:
const int buttonPin = 8; // the pin that the pushbutton is attached to
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 7;
const int brakeB = 4;
const int dirA = 12;
const int dirB = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
pinMode(dirA, OUTPUT); //Initiates Motor Channel A pin
pinMode(brakeA, OUTPUT); //Initiates Brake Channel A pin
//Setup Channel B
pinMode(dirB, OUTPUT); //Initiates Motor Channel B pin
pinMode(brakeB, OUTPUT); //Initiates Brake Channel B pin);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
servoarm9.attach(9); // attaches the servo on pin 9 to the servo object
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servoarm9.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
servoarm9.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounter % 4 == 0) {
//Motor A forward @ full speed
digitalWrite(dirA, HIGH); //Establishes forward direction of Channel A
digitalWrite(brakeA, LOW); //Disengage the Brake for Channel A
analogWrite(pwmA, 255); //Spins the motor on channel A at full speed
//Motor B forward @ full speed
digitalWrite(dirB, HIGH); //Establishes forward direction of Channel B
digitalWrite(brakeB, LOW); //Disengage the Brake for Channel B
analogWrite(pwmB, 255);
delay(3000);
digitalWrite(brakeA, HIGH); //Engage the Brake for Channel A
digitalWrite(brakeB, HIGH); //Engage the Brake for Channel B
delay(1000);
//Motor A backwards @ full speed
digitalWrite(dirA, LOW); //Establishes backward direction of Channel A
digitalWrite(brakeA, LOW); //Disengage the Brake for Channel A
analogWrite(pwmA, 255); //full speed
//Motor B backwards @ half speed
digitalWrite(dirB, LOW); //Establishes backward direction of Channel B
digitalWrite(brakeB, LOW); //Disengage the Brake for Channel B
analogWrite(pwmB, 127); //half speed
delay(3000);
//Motor A forward @ half speed
digitalWrite(dirA, HIGH); //Establishes forward direction of Channel A
digitalWrite(brakeA, LOW); //Disengage the Brake for Channel A
analogWrite(pwmA, 127); //half speed
//Motor B forward @ full speed
digitalWrite(dirB, HIGH); //Establishes forward direction of Channel B
digitalWrite(brakeB, LOW); //Disengage the Brake for Channel B
analogWrite(pwmB, 255); //full speed
delay(3000);
//Motor A backwards @ full speed
digitalWrite(dirA, LOW); //Establishes backward direction of Channel A
digitalWrite(brakeA, LOW); //Disengage the Brake for Channel A
analogWrite(pwmA, 255); //full speed
//Motor B backwards @ half speed
digitalWrite(dirB, LOW); //Establishes backward direction of Channel B
digitalWrite(brakeB, LOW); //Disengage the Brake for Channel B
analogWrite(pwmB, 127); //half speed
delay(3000);
//Motor A forward @ half speed
digitalWrite(dirA, HIGH); //Establishes forward direction of Channel A
digitalWrite(brakeA, LOW); //Disengage the Brake for Channel A
analogWrite(pwmA, 127); //half speed
//Motor B forward @ full speed
digitalWrite(dirB, HIGH); //Establishes forward direction of Channel B
digitalWrite(brakeB, LOW); //Disengage the Brake for Channel B
analogWrite(pwmB, 255); //full speed
delay(3000);
digitalWrite(brakeA, HIGH); //engage the Brake for Channel A
digitalWrite(brakeB, HIGH); //engage the Brake for Channel B
delay(3000);
servoswitch10.attach(10); // attaches the servo on pin 9 to the servo object
for(pos = 0; pos < 90; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servoswitch10.write(pos); // tell servo to go to position in variable 'pos'
// waits 15ms for the servo to reach the position
}
} else {
}
}}