Is there anyone who can help me to revise the Arduino code that I made for setting up electric (AC) motors?
I use an Arduino Uno with 3 push buttons reverse, forward and stop then I use 2 relays for the reveres and forward contactors.
I want if I press the reverse push button then it cannot be interrupted by pressing the forward push button. This means that if the forward push button is pressed it will not function if the relay status is reverse on.
Likewise, if I press the forward push button, it cannot be interrupted by pressing the reverse push button. This means that if the reverse push button is pressed it will not function if the forward relay status is on.
Because I am a complete beginner in Arduino programming, I have tried various methods but have not succeeded.
Thank you if anyone is willing to help me.
Harkel
The code I have created is as follows:
const int reverseButtonPin = 2;
const int forwardButtonPin = 3;
const int stopAllButtonPin = 4;
const int relayPin1 = 5;
const int relayPin2 = 6;
void setup() {
pinMode(reverseButtonPin, INPUT_PULLUP);
pinMode(forwardButtonPin, INPUT_PULLUP);
pinMode(stopAllButtonPin, INPUT_PULLUP);
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
}
void loop() {
int reverseButtonState = digitalRead(reverseButtonPin);
int stopAllButtonState = digitalRead(stopAllButtonPin);
int forwardButtonState = digitalRead(forwardButtonPin);
// Mode Reverse
while (reverseButtonState == HIGH) {
digitalWrite(relayPin1, HIGH);
digitalWrite(relayPin2, LOW);
if (forwardButtonState == HIGH) {
digitalWrite(relayPin1, HIGH);
digitalWrite(relayPin2, LOW);
}
return;
}
// Mode Forward
while (forwardButtonState == HIGH) {
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, HIGH);
if (reverseButtonState == HIGH) {
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, HIGH);
}
return;
}
// Stop All
if (stopAllButtonState == HIGH) {
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, LOW);
}
}
