const int FETSF = 2; // the pin for the forward direction mosfets
const int FETSR = 4; // the pin for the reverse direction mosfets
const int BUTTON = 7; // the input pin where the pushbutton is connected
int val = 0; // val will be used to store the state
// of the input pin
int but_state = 0; // button state
int dir_state = 1; // last direction state: 0 = last went forward, 1 = last went reverse
void setup() {
pinMode(FETSF, OUTPUT); // tell Arduino mosfet is an output
pinMode(FETSR, OUTPUT); // " " "
pinMode(BUTTON, INPUT); // and BUTTON is an input
}
void loop() {
val = digitalRead(BUTTON); // read input value and store it
// check if the input is HIGH (button pressed)
// and change the state
if (val == HIGH) {
but_state = 1 - but_state;
}
if (but_state == 1) {
if (dir_state == 1){
//const int FETSF = 2; // the pin for the forward direction mosfets
//const int FETSR = 4; // the pin for the reverse direction mosfets
digitalWrite(FETSF, HIGH); // turn motor forward
digitalWrite(FETSR, LOW)
delay (4000); // for 4 seconds
digitalWrite(FETSF, LOW); // turn motor off
dir_state = 0; //set last direction state to forward
}
else {
digitalWrite(FETSF, LOW);
digitalWrite(FETSR, HIGH)
delay (4000); // for 4 seconds
digitalWrite(FETSR, LOW); // turn motor off
dir_state = 1; //set last direction state to reverse
}
}
else {
digitalWrite(FETSF, LOW);
digitalWrite(FETSR, LOW);
}
}
How's that work for you?