Hello everyone, I am new to arduino and I need your help in a code. I need to control a DC motor direction by 2 pushbutton connected to arduino and H-bridge. where I want to press and hold 3 second on SW 1 and release, the motor should turn in one direction and if the same for SW 2, the motor should turn in the opposite direction. My code is the following:
const int buttonPin1 = 2;
const int buttonPin2= 4;
const int ledPin2 = 7;
const int ledPin1 = 5;
// variables will change:
int buttonState1 = 0;
int buttonState2 = 0;
void setup() {
pinMode(ledPin1, OUTPUT); // direction 1 for motor
pinMode(ledPin2, OUTPUT); // direction 2 for motor
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop() {
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
if (buttonState1 == HIGH){
delay(3000);
digitalWrite(ledPin1, HIGH);
delay (10000);
}
if (buttonState2 == HIGH) {
delay(3000);
digitalWrite(ledPin2, HIGH);
delay(10000);}
}
The problem in this code is that when ledPin1 is on , I cannot access ledPin2 only after 10 s which is in delay(10000), I take it as arbitrary value to keep the LED on for a while. so any help please to fix the issue where I want to switch the direction of the motor and remain in this direction until another change as specified above, without waiting .
const int buttonPin1 = 2;
const int buttonPin2= 4;
const int ledPin2 = 7;
const int ledPin1 = 5;
// variables will change:
int buttonState1 = 0;
int buttonState2 = 0;
void setup() {
pinMode(ledPin1, OUTPUT); // direction 1 for motor
pinMode(ledPin2, OUTPUT); // direction 2 for motor
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop() {
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
if (buttonState1 == HIGH){
delay(3000);
digitalWrite(ledPin1, HIGH);
delay (10000);
}
if (buttonState2 == HIGH) {
delay(3000);
digitalWrite(ledPin2, HIGH);
delay(10000);}
}
The problem in this code is that when ledPin1 is on , I cannot access ledPin2 only after 10 s which is in delay(10000), I take it as arbitrary value to keep the LED on for a while. so any help please to fix the issue where I want to switch the direction of the motor and remain in this direction until another change as specified above, without waiting .