So I am designing a simple project where my Arduino reads analog values from a potentiometer, or rotation sensor, as it is called on EBay, and uses that value to determine how many relays to close. I am connecting 4 digital pins to the relay module, and powering it with a constant digital signal from pin 2.
Here is my code:
//set up the pins for activating each relay on the module
#define in1 13
#define in2 11
#define in3 10
#define in4 9
#define power 2 //set up the power pin for the module
void setup(){
Serial.begin(9600);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(power, OUTPUT);
digitalWrite(power, HIGH); //for power to the relay module
}
void loop(){
float rotationValue = analogRead(A0); //read raw value from potentiometer
float percentValue = rotationValue / 10.23; //convert the value to 0%-100%
Serial.println(percentValue); //print it out on screen
if(percentValue >= 0.00 && percentValue <= 1.00){ //all relays off
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
else if(percentValue > 1.00 && percentValue <= 25.00){ //relay #1 on
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
else if(percentValue > 25.00 && percentValue <= 50.00){ //relay #1, #2 on
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
else if(percentValue > 50.00 && percentValue <= 75.00){ //relay #1, #2, #3 on
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
else if(percentValue > 75.00){ //all relays on
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, HIGH);
}
}
Ok, here's the problematic part. After I upload the program onto the board, when I set the potentiometer to the 0 position, all relays are on. And when I twist it to the 100 position, which is maximum, all relays are off. Although it is usable, what's weird is that when I start from 100 and twist it back toward 0, the relays will activate, but in the opposite order. So relay #4 will activate, then #3, then #2, and then #1, and all four are on when I reach 0. Is it something wrong with my code? I've checked and rechecked, and even checked with a different project where I connect the pins to LED's. And they work normally. But when I connect up the relay module, it's reversed. Any ideas?
Here is the relay module: http://www.miniinthebox.com/4-channel-relay-module-with-optocoupler-5v_p903429.html?currency=USD&litb_from=paid_adwords_shopping&gclid=Cj0KEQiAts-kBRCbgrXc1rnXw7MBEiQAnFqTdph_qI5n7rrpW7gZaxafVVkeWitQlN8FUSUJzdsV2JwaAlBA8P8HAQ
And here is the potentiometer: http://www.ebay.com/itm/Potentiometer-Sensor-module-Rotation-Angle-Sensor-Arduino-competible-/251656942542
I mean, right now I'm even thinking that the relay module may be labeled incorrectly, that the numbers are printed the wrong way. But it could be my fault, most likely.
Thanks for any help.