I am trying to add a delay between two motors operating i.e if one motor is operating wait one second then start the next and vice versa, I am using an H Bridge to change the direction of the motors..
const int switchPin = 2; // switch input
const int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A)
const int enablePin = 9; // H-bridge enable pin
const int motor3Pin = 5; //H-bridge leg 1
const int motor4Pin = 6; // H-Bridge leg 2
const int enablePin1 = 8;
int enaablePin1 = LOW;
long previousmillis = 0;
long interval = 1000;
void setup() {
// set the switch as an input:
pinMode(switchPin, INPUT);
// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode (motor3Pin, OUTPUT);
pinMode(motor4Pin, OUTPUT);
// set enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
}
void loop() {
unsigned long currentmillis = millis();
if(currentmillis - previousmillis > interval) {
previousmillis = currentmillis;
if (enablePin1 == LOW);
else
interval;
digitalWrite(enablePin1, HIGH);
}
// if the switch is high, motor will turn on one direction:
if (digitalRead(switchPin) == HIGH) {
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
digitalWrite(motor3Pin, LOW);
digitalWrite(motor4Pin, HIGH);
}
// if the switch is low, motor will turn in the other direction:
else {
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
digitalWrite(motor3Pin, HIGH);
digitalWrite(motor4Pin, LOW);
}
}