Controlling 2 relays based on comparison between 2 sensors

i want to know why the relays are not switching according to my loops and if there any improvements to be made on my code

const int relayPin= 13;
const int relayPin1=12;
int relayState= 0;
int relayState1= 0;



void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(relayPin, OUTPUT); 
  pinMode(relayPin1, OUTPUT); 
}

void loop() {
  // put your main code here, to run repeatedly:
  int sensorValue;
  int sensorValue1;
  sensorValue = analogRead(A0);
  sensorValue1 = analogRead(A1);
  Serial.print("The voltage value:");
  Serial.print(sensorValue);
  Serial.println("mV");
  delay(200);
  Serial.print("\n");
  Serial.print("\t");
  Serial.print("\t");
  Serial.print("\t");
  Serial.print(" The voltage value:");
  Serial.print(sensorValue1);
  Serial.println("mV");
  delay(200);
  Serial.print("\n");
   if(sensorValue > sensorValue1){
   if (relayState ==0){
      digitalWrite (relayPin, HIGH);
      relayState=1;
    }
}
if(sensorValue1 > sensorValue){
   if (relayState1 ==0){
      digitalWrite (relayPin1, HIGH);
      relayState1=1;
    }
}
if (sensorValue = sensorValue1){
     if (relayState ==1){
      digitalWrite (relayPin, LOW);
      relayState=0;
  
  }
if (relayState1 ==1){
      digitalWrite (relayPin1, LOW);
      relayState1=0;
}
}
}

not "=" but "=="

As see in the code, this part is useless,

because once relay1 becames HIGH, it immediately returns to LOW in the last IF

i want to turn the 1st relay on when the sensorvalue is higher than sensorvalue1 and turn it off in addition into turning the other relay on when sensorvalue1 is higher than sensor value and vice versa. also i want both relays to be off when the values are equal

But the controller do what you exactly wrote in the code, not what you intended to write...

then how am i supposed to get what i want? how can i get it done? i dont want only a solution for sure if a solution is provided i will need explanation for next times

So every time you turn on one relay, turn off the other. And turn it off both when the values are equal

It's very unlikely that 2 analogRead()s will be equal. You might need to program a bit of leeway in there, switch them both off if they're within (say) 5 of each other.

Hi,
List out the number of input combinations you have and next to each combination list what you want your outputs to do.
THEN.. look up switch .. case.. function.

When you come to writing your code, first read ALL your inputs and store them in variables.
Then evaluate combination is which case and apply it.
In each case condition you have the commands to your outputs that you wish.

Switch.. case.. can makes lots of complicated if statement look like junk code.

Tom.... :smiley: :+1: :australia: :coffee:
PS, Sorry if it sounds a bit complicated, I am at work, on instant coffee as coffee truck didn't arrive.

hello
i have 2 inputs
and 2 output relays for forward and reverse rotation of a dc motor
analogreading from 2 sensors and comparing their results so that:
1- if the value of the first sensor is higher than the value of the 2nd sensor: relay 1 is on and relay 2 is off
2- if the value of the 2nd sensor is higher than the value of the first sensor: relay 2 is on and relay 1 is off
3- if the values of both sensors are equal: relay 1 is off and relay 2 is off

Hi,

Thanks for the point form explanation.
How stable is you analog inputs, you may need hysteresis/deadband to prevent unstable operation.

So you 3 cases... Look at switch.. case function.
Simpler than a mass of if and if else functions.

Tom.... :smiley: :+1: :coffee: :australia:

analogRead can be affected by external factors that make it likely that comparing for an exact match makes your system very twitchy. Hysteresis as @TomGeorge mentions is probably necessary. You will need to experiment to see how wide a dead-end you need.

okay thank you very much

yes i've took that into consideration when testing values of both sensors and all is good thank you for your help

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.