Just to start with, Im a beginner with Arduino and this is actually my first project.
Im trying to control a pair of solenoids (24v) which in return control a hydraulic ram. Pretty much what I've done is write an if/else loop. I have a predetermined value I'd like to hold a potentiometer at , which gets it's reading from the hydraulic ram.
So my plan was to keep the value at 600 with the accuracy of +/- 20. So that if the reading goes below 580, it switches on a relay that turns on a solenoid and everything else is off. When the value goes over 620, it switches on a different relay and everything else is off and when the value is between 580 and 620, it switches on a green led and the relays are off.
What is happening is when it's below 580 or above 620, it works fine but when the value is between 580 and 620, the green led is on and instead of relays being off, they are both on as well. I've tried changing over the relay board thinking maybe thats it but no. Then I tried over and over again looking at the code, hoping I could find the mistake but with no luck. Haven't even found anything I thought I could use in the allmighty Google.
Really hoping someone would be able to help me with this!
Thank you in advance
Siim
// left roller controlls
const int Left_Relay = 10;
const int Right_Relay = 9;
const int Neutral = 8;
// pots in
const int Left_pot = A0;
const int value_Left=600;
const int value_sensitivity=20;
void setup() {
pinMode(Left_Relay, OUTPUT);
pinMode(Right_Relay, OUTPUT);
pinMode(Neutral, OUTPUT);
//start serial
Serial.begin(9600);
}
void loop() {
//read left pot and controll rollers
int sensorLeft = analogRead(Left_pot);
if (sensorLeft <= value_Left - value_sensitivity) {
digitalWrite (Left_Relay, HIGH);
digitalWrite (Right_Relay, LOW);
digitalWrite (Neutral, LOW);
}
else if (sensorLeft >= value_Left + value_sensitivity) {
digitalWrite (Left_Relay, LOW);
digitalWrite (Right_Relay, HIGH);
digitalWrite (Neutral, LOW);
}
else {
digitalWrite (Left_Relay, LOW);
digitalWrite (Right_Relay, LOW);
digitalWrite (Neutral, HIGH);
}
delay(100);
//print sensorLeft value to monitor
Serial.println(sensorLeft);
}
If the value is less than 580, it turns on the right pin to high and the relay works correctly. When its over 620 it turn on the other correct pin to HIGH, the rest to LOW as it should. But when its between 580 and 620, when the relay pins should be LOW and only the led pin HIGH, it still has the relay pins HIGH as well.
I've tried getting rid of the else part and writing the code as below and the relays switch on and off and don't stay on both the same time. But I would need it to be kind of in a neutral state as well and as soon as I try to add something to keep the relay pins LOW it all goes pear shaped
// left roller controlls
const int Left_Relay = 10;
const int Right_Relay = 9;
const int Neutral = 8;
// pots in
const int Left_pot = A0;
const int value_Left=600;
const int value_sensitivity=20;
void setup() {
pinMode(Left_Relay, OUTPUT);
pinMode(Right_Relay, OUTPUT);
pinMode(Neutral, OUTPUT);
//start serial
Serial.begin(9600);
}
void loop() {
//read left pot and controll rollers
int sensorLeft = analogRead(Left_pot);
if (sensorLeft < value_Left) {
digitalWrite (Left_Relay, HIGH);
digitalWrite (Right_Relay, LOW);
digitalWrite (Neutral, LOW);
}
else if (sensorLeft >= value_Left) {
digitalWrite (Left_Relay, LOW);
digitalWrite (Right_Relay, HIGH);
digitalWrite (Neutral, LOW);
}
delay(100);
//print sensorLeft value to monitor
Serial.println(sensorLeft);
}
Why are you not printing the value you read from the sensor where you read it?
Add Serial.print() statements in the two blocks. Make sure that the block you think is being executed IS being executed.
const int Left_pot = A0;
const int value_Left=600;
const int value_sensitivity=20;
It doesn't include the relay board. Please direct us to some information about it. A simple pencil sketch or cell phone image of the setup would be fine. The point is to include all the details. A little post-it note about the connections to the relay board is not good enough.
Sorry, I don't have any sort of manuals or anything else about it, but I added an ebay link where I bought a few of them.
The relays have a little led on board that indicates if the relay is on or off. If it's off the centre pin on the relay and the left pin are connected. If the relay is on, the centre and right hand pin are connected (the on-board led is on ) so it pretty much has a normally closed and normally open function.