I made an Arduino connection consisting of 2 relays and a servo motor. Aim is rotate the motor in one direction if one relay is ON and in other direction if other is ON.
Now the third condition is to rotate periodically according to my code, stating the condition as "LOW". Now the problem is that it's not doing that.
There's another condition that if both my relays are ON, it should be rotating between these two positions and it's doing that.
What I feel is it's only taking the condition "HIGH" , like being able to read only "HIGH" but not "LOW". I tried not equal to(!=) "HIGH" and less than (<) "HIGH" , but still not working.
Any suggestions..?
Please post a schematic diagram showing all connections; a photo of a hand-drawn one will be OK.
Also please show your code; just in case, please read How to use this forum - please read. - Installation & Troubleshooting - Arduino Forum, specifically point #7 about posting code.
// https://forum.arduino.cc/index.php?topic=637630
// 25 september 2019
//servo rotating left for relay1 ON and servo rotating right for relay2 ON.
#include <Servo.h>
Servo myservo;
// check the values in this next 5 lines....
byte myservoPin = 8;
byte myservoGoOneWaySwitch = 2; //wire buttons from pin to ground
byte myservoGoOtherWaySwitch = 3;
byte minPos = 0; //I like to keep away from the end stops
byte midPos = 90;
byte maxPos = 150;
byte xPos = 45;
byte yPos = 125;
void setup()
{
Serial.begin(9600);
Serial.println(".... servo controlled by two relays as normal inputs .... forum 637630");
Serial.print("Created: ");
Serial.print(__TIME__);
Serial.print(", ");
Serial.println(__DATE__);
Serial.print("File: ");
Serial.println(__FILE__);
//by default the servo starts in the middle
// uncomment one of these next lines if you want to start at either side
//myservo.write(minPos);
//myservo.write(maxPos);
myservo.attach(myservoPin);
pinMode(myservoGoOneWaySwitch, INPUT_PULLUP); //wire switches from pin to ground
pinMode(myservoGoOtherWaySwitch, INPUT_PULLUP);
Serial.println("Waiting for a relay"); Serial.println(" ");
} //setup
void loop()
{
//input_pullup are low when relay closed
/*if (digitalRead(myservoGoOneWaySwitch) == HIGH )//both closed=illegal
if (digitalRead(myservoGoOtherWaySwitch) == HIGH)
{
Serial.println("Going both ways");
myservo.write(maxPos);
delay(2000);
myservo.write(midPos);
delay(2000);
myservo.write(minPos);
}
else if (digitalRead(myservoGoOtherWaySwitch) == HIGH )// //both closed=illegal
if (digitalRead(myservoGoOneWaySwitch) == HIGH)
{
Serial.println("Going both ways");
myservo.write(minPos);
delay(2000);
myservo.write(midPos);
delay(2000);
myservo.write(maxPos);
}*/
if ((digitalRead(myservoGoOtherWaySwitch) == HIGH) && (digitalRead(myservoGoOneWaySwitch) == LOW))
{
myservo.write(minPos);
// this approach just zooms the servo at full speed, see https://www.arduino.cc/en/Tutorial/Sweep if that's a problem
Serial.println("Going one way");
}
else if ((digitalRead(myservoGoOneWaySwitch) == HIGH) && (digitalRead(myservoGoOtherWaySwitch) == LOW))
{
myservo.write(maxPos);
// this approach just zooms the servo at full speed, see https://www.arduino.cc/en/Tutorial/Sweep if that's a problem
Serial.println("Going other way");
}
else if (digitalRead(myservoGoOneWaySwitch) == HIGH)
if (digitalRead(myservoGoOtherWaySwitch) == HIGH)
{
myservo.write(maxPos);
delay(2000);
myservo.write(minPos);
delay(2000);
myservo.write(midPos);
delay(2000);
// this approach just zooms the servo at full speed, see https://www.arduino.cc/en/Tutorial/Sweep if that's a problem
Serial.println("Going other way");
}
else if (digitalRead(myservoGoOtherWaySwitch) == HIGH)
if (digitalRead(myservoGoOneWaySwitch) == HIGH)
{
myservo.write(minPos);
delay(2000);
myservo.write(maxPos);
delay(2000);
myservo.write(midPos);
delay(2000);
}
else if ((digitalRead(myservoGoOneWaySwitch) < HIGH))
if ((digitalRead(myservoGoOtherWaySwitch) < HIGH))
{
myservo.write(minPos);
delay(2000);
myservo.write(xPos);
delay(2000);
myservo.write(midPos);
delay(2000);
myservo.write(yPos);
delay(2000);
myservo.write(maxPos);
delay(2000);
}
} //loop [code]
Below looks like the same conditions
else if (digitalRead(myservoGoOneWaySwitch) == HIGH)
if (digitalRead(myservoGoOtherWaySwitch) == HIGH)
{
...
...
else if (digitalRead(myservoGoOtherWaySwitch) == HIGH)
if (digitalRead(myservoGoOneWaySwitch) == HIGH)
{
...
...
If you modify your code so the if statements basically show a truth table, your code might be easier to read.
With two buttons/relays, you have four options
myservoGoOtherWaySwitch | myservoGoOneWaySwitch | option
------------------------+-----------------------+------
1 | 1 | 3
------------------------+-----------------------+------
1 | 0 | 2
------------------------+-----------------------+------
0 | 1 | 1
------------------------+-----------------------+------
0 | 0 | 0
------------------------+-----------------------+------
So in below you can basically follow the table above
if ((digitalRead(myservoGoOtherWaySwitch) == HIGH) && (digitalRead(myservoGoOneWaySwitch) == HIGH))
{
...
...
}
else if ((digitalRead(myservoGoOtherWaySwitch) == HIGH) && (digitalRead(myservoGoOneWaySwitch) == LOW))
{
...
...
}
else if ((digitalRead(myservoGoOtherWaySwitch) == LOW) && (digitalRead(myservoGoOneWaySwitch) == HIGH))
{
...
...
}
else
//else if ((digitalRead(myservoGoOtherWaySwitch) == LOW) && (digitalRead(myservoGoOneWaySwitch) == LOW))
{
...
...
}
The last else/if is replaced by an else as there is no other option left. Hope that this gets you on the way.