Read 2 analog inputs and compare them

Hi! First of all thank you for your help!!
I’m stuck with my program.
I have 2 pot’s connected to analog A0 and A1 and and if the value (difference) of the 2 pots is higher or lower it will activate output A or B.
It’s working ok but I would like to add a routine that pauses the program if the value between analog A0 and A1 inputs is lower than 20 and if it’s higher it will activate output A or B.

Here is my code:

const int analogInPinA = A0; // Analog input Potentiometer A
const int analogInPinB = A1; // Analog input Potentiometer B

const int outPinB = 2; // Output relay A
const int outPinC = 3; // Output relay B

int sensorValueA = 0; // Potentiometer A value
int sensorValueB = 0; // Potentiometer B value

//Main program

void setup() {

// serial
Serial.begin(9600);

// pin 3 and 4 outputs
pinMode(outPinB, OUTPUT);
pinMode(outPinC, OUTPUT);

}

void loop() {

// Read analog inputs:

sensorValueA = analogRead(analogInPinA);

analogRead(5);
delay(100);

sensorValueB = analogRead(analogInPinB);

analogRead(5);
delay(100);

if (sensorValueA < sensorValueB) digitalWrite(outPinC, HIGH);
if (sensorValueA < sensorValueB) digitalWrite(outPinB, LOW);
if (sensorValueA > sensorValueB) digitalWrite(outPinB, HIGH);
if (sensorValueA > sensorValueB) digitalWrite(outPinC, LOW);

// Send data to serial monitor serial monitor:
Serial.print(“Potentiometer A = " );
Serial.print(sensorValueA);
Serial.print(”\t Potentiometer B = ");
Serial.println(sensorValueB);

}

Thank you for your help!!
Ricardo

You mean something like:

  int difference = sensorValueA - sensorValueB ;
  digitalWrite (outPinB, difference > 20 ? HIGH : LOW) ;
  digitalWrite (outPinC, difference < -20 ? HIGH : LOW) ;

THANK YOU!!! It's working the way I wanted!! The main purpose of this project is controling the back of my fathers farm tractor plow reading PotentiometerA (the position requested) that is installed in a lever and compares with the fisical position of the plow (potentiometer B). If the value it's the same it stops! I've tested it and it's working!! Thank you for your great help!!! Ricardo

Now for a bonus point, explain back to Mark how those lines of code he posted actually work 8)

Hi!
I will try to explain the code lines that MarkT posted:

int difference = sensorValueA - sensorValueB ; // The integer "difference" stores the value obtained by the subtraction of analog read A and B

  digitalWrite (outPinB, difference > 20 ? HIGH : LOW) ; // It will set the outPinB to logic High if the value (difference) is higher than 20 and logic low if it's lower than 20
  digitalWrite (outPinC, difference < -20 ? HIGH : LOW) ; // It will set the outPinC to logic High if the value (difference) is higher than -20 and logic low if it's lower than -20
                                                                             //This means that if the value is higher than 20 it will activate the PinB and if it's higher than -20 it will activate the PinC
                                                                             //If the value is between -20 and 20 it will set logic low to PinB and PinC and do nothing

I hope I got it!!
Thank you for beeing so amazing!!
I love arduino!!
Thank you
Ricardo

You have your bonus mark! Alas no monetary value is attached.

It will set the outPinB to logic High if the value (difference) is higher than 20 and logic low if it's lower than 20 Close, but not good enough for a gold star.