Problems with potentiometer inputs and digital outputs

Hi,

I am developing a code for use on a pneumatic control system. The goal is to have different sized air valves open when a proportional difference of resistance between 2 potentiometers is shown.

Example: Pot1 is located along the cylinder, Pot2 is located independently. When Pot2 is moved there becomes a difference in position of the two Pots. Depending on how great the difference is between the two Pots one gate will open at a time, allowing air pressure to move the cylinder rod forward until the difference in position of the two pots is 0. The same needs to happen when Pot2 is moved in reverse.

I have made adjustments to the code, but I can’t get any of the test LED’s(switches) to light up.
Any help would be greatly appreciated. Thank you for your time.

const int analogInPin1 = A0;
const int analogInPin2 = A1;
int switch1 = 1;
int switch2 = 2;
int switch3 = 3;
int switch4 = 4;
int switch5 = 5;
int switch6 = 6;
int switch7 = 7;
int switch8 = 8;
int switch9 = 9;
int switch10 = 10;

void setup() 
{
  //declaring the switches as outputs
  pinMode(switch1, OUTPUT);
  pinMode(switch2, OUTPUT);
  pinMode(switch3, OUTPUT);
  pinMode(switch4, OUTPUT);
  pinMode(switch5, OUTPUT);
  pinMode(switch6, OUTPUT);
  pinMode(switch7, OUTPUT);
  pinMode(switch8, OUTPUT);
  pinMode(switch9, OUTPUT);
  pinMode(switch10, OUTPUT);
  
  pinMode(analogInPin1, INPUT);
  pinMode(analogInPin2, INPUT);
}

void loop() {
  
  {
  if (-200 < analogRead((analogInPin1)-(analogInPin2)) <= 1) 
    {
    digitalWrite(switch1,HIGH);
  } 
  else {
    digitalWrite(switch1,LOW); 
  }
  }
  
  {
  if (-400 < analogRead((analogInPin1)-(analogInPin2)) <= -200) 
    {
    digitalWrite(switch2,HIGH);
  } 
  else {
    digitalWrite(switch2,LOW); 
  }
  }
  
  {
  if (-600 < analogRead((analogInPin1)-(analogInPin2)) <= -400) 
    {
    digitalWrite(switch3,HIGH);
  } 
  else {
    digitalWrite(switch3,LOW); 
  }
  }
  
  {
  if (-800 < analogRead((analogInPin1)-(analogInPin2)) <= -600) 
    {
    digitalWrite(switch4,HIGH);
  } 
  else {
    digitalWrite(switch4,LOW); 
  }
  }
  
  {
  if (-1023 <= analogRead((analogInPin1)-(analogInPin2)) <= -800) 
    {
    digitalWrite(switch5,HIGH);
  } 
  else {
    digitalWrite(switch5,LOW); 
  }
  }
  
  
  
  
  
  {
  if (1 < analogRead((analogInPin1)-(analogInPin2)) <= 200) 
    {
    digitalWrite(switch6,HIGH);
  } 
  else {
    digitalWrite(switch6,LOW); 
  }
  }
  
  {
  if (200 < analogRead((analogInPin1)-(analogInPin2)) <= 400) 
    {
    digitalWrite(switch7,HIGH);
  } 
  else {
    digitalWrite(switch7,LOW); 
  }
  }
  
  {
  if (400 < analogRead((analogInPin1)-(analogInPin2)) <= 600) 
    {
    digitalWrite(switch8,HIGH);
  } 
  else {
    digitalWrite(switch8,LOW); 
  }
  }
  
  {
  if (600 < analogRead((analogInPin1)-(analogInPin2)) <= 800) 
    {
    digitalWrite(switch9,HIGH);
  } 
  else {
    digitalWrite(switch9,LOW); 
  }
  }
  
  {
  if (800 < analogRead((analogInPin1)-(analogInPin2)) <= 1023) 
    {
    digitalWrite(switch10,HIGH);
  } 
  else {
    digitalWrite(switch10,LOW); 
  }
  }
}

OK Ignoring your unusual choice of variable names (switches are generally input devices). The first line I find with real problems is this one.

if (-200 < analogRead((analogInPin1)-(analogInPin2)) <= 1)

So if we evaluate all of the brackets, bit by bit in order of precedence.

AnalogInPin2 = A2
AnalogInPin1 = A1

so (analogInPin1)-(analogInPin2) = -1

You are therefore making a call to analogRead(-1);
I’m not even sure what analogRead will do with that but I’m guessing that
this will return a value of somewhere between 0 and 255; (we’ll call this value x for now)

(-200 < x) will always be true (which equates to 1)
1 <= 1 will also be true EVERY TIME.

 {
  if (-200 < analogRead((analogInPin1)-(analogInPin2)) <= 1) 
    {
    digitalWrite(switch1,HIGH);
  } 
  else {
    digitalWrite(switch1,LOW); 
  }
  }

I am not exactly a pro at this but i’m not sure what the first and last bracket set are for. You dont need to wrap the entire if/else within brackets.
http://arduino.cc/en/Reference/Else

Your if statement reads very strangely to me and there has to be an && in there to test for two items if im not mistaken.
http://arduino.cc/en/Reference/Boolean

I am not certain if you can cheat on the analog read and double it up or not. Might work… but i dont have my hardware here to test it.

I would change your statement around a bit as below if it were in my code.

  if ((analogRead(analogInPin1)-analogRead(analogInPin2)) >-200 && <= 1) 
  {
    digitalWrite(switch1,HIGH);
  } 
  
  else 
  {
    digitalWrite(switch1,LOW); 
  }

Goofballtech:
I would change your statement around a bit as below if it were in my code.

  if ((analogRead(analogInPin1)-analogRead(analogInPin2)) >-200 && <= 1)

You haven’t said what is equal or less than 1.

Thank you for the responses, they gave me something to go on. After creating the code piece by piece I have come up with a solution that works rather well, a bit lengthy though. It turns out that yes, you can measure the resistance of multiple Pots as well as actively take those differences and apply them. Here is a sample of the prototype code that works, which I will have to add to eventually. If you have a way to simplify it I would love to see it, but for now this one is operational:

const int analogInPin1 = A0;
const int analogInPin2 = A1;
int switch1 = 1;
int switch2 = 2;
int switch3 = 3;
int switch4 = 4;
int switch5 = 5;
int switch6 = 6;
int switch7 = 7;
int switch8 = 8;
int switch9 = 9;
int switch10 = 10;

void setup() 
{
  //declaring the switches as outputs
  pinMode(switch1, OUTPUT);
  pinMode(switch2, OUTPUT);
  pinMode(switch3, OUTPUT);
  pinMode(switch4, OUTPUT);
  pinMode(switch5, OUTPUT);
  pinMode(switch6, OUTPUT);
  pinMode(switch7, OUTPUT);
  pinMode(switch8, OUTPUT);
  pinMode(switch9, OUTPUT);
  pinMode(switch10, OUTPUT);
  
  pinMode(analogInPin1, INPUT);
  pinMode(analogInPin2, INPUT);
}

void loop() 
 {
 {
 if ((analogRead(analogInPin1) - analogRead(analogInPin2)) < -1)
{
  digitalWrite(switch1, LOW);
}
else if ((analogRead(analogInPin1) - analogRead(analogInPin2)) >200)
{
  digitalWrite(switch1, LOW);
}
else
{
  digitalWrite(switch1, HIGH);
}
 }
 
 {
 if ((analogRead(analogInPin1) - analogRead(analogInPin2)) < -400)
{
  digitalWrite(switch5, LOW);
}
else if ((analogRead(analogInPin1) - analogRead(analogInPin2)) > -200)
{
  digitalWrite(switch5, LOW);
}
else
{
  digitalWrite(switch5, HIGH);
}
 }
 }

So after defining all those “switches” you only ever use two of them? (switch1 and switch5)

BTW if you’re using a pin for analogReading you don’t even need to declare the pinmode for it.

Bearing in mind these issues and removing reference to your variables (as they add nothing to the readability of the sketch) I come up with this.

void setup() 
{
  //declaring the switches as outputs
  pinMode(1, OUTPUT);
  pinMode(5, OUTPUT);
}

void loop() 
 {
 int difference = analogRead(A0) - analogRead(A1);

if ( (difference < -1) || (difference>200) )
    digitalWrite(1, LOW);
  else
    digitalWrite(1, HIGH);
  
if ( (difference < -400)||(difference > -200) )
   digitalWrite(5, LOW);
else
  digitalWrite(5, HIGH);
 }

Wave222222:
when a proportional difference of resistance between 2 potentiometers is shown.

Where does the proportional part come in? Far as I can see, you're interested merely in the absolute difference.

I will have to add 8 more groups, split between the two groups that I figured out. It will take some messing around but I believe the hard part is over. Thanks for the analog pin tip Ken!

Jimbo, the proportional difference that I was referring to was in relation to which switch needed to be activated: If the difference is great, then a switch in control of a larger air valve would open. If the difference is small, then a switch in control of a smaller air valve would open. The whole point is to prevent the jerking action of air cylinders and provide more of a "cushion" when operating them. If you have 5 different sized air valves that operate in a step-down fashion when getting the cylinder rod close to the desired position, then hopefully the jerking motions will subside.

Ah, I thought you meant when you said a proportional difference between two pots, you meant a proportional difference between two pots. :wink:

(ie, a difference of 100 at one end of the pot's range would have a different effect to 100 at the other end.)

Hi, can I make a suggestion.

const int analogInPin1 = A0;
const int analogInPin2 = A1;
int switch1 = 1;
int switch2 = 2;
int switch3 = 3;
int switch4 = 4;
int switch5 = 5;
int switch6 = 6;
int switch7 = 7;
int switch8 = 8;
int switch9 = 9;
int switch10 = 10;

CHANGE TO:

const int ShaftPos = A0;
const int SetPoint = A1;
int OutputL10 = 1;
int OutputL20 = 2;
int OutputL30 = 3;
int OutputL40 = 4;
int OutputL50 = 5;
int OutputR10 = 6;
int OutputR20 = 7;
int OutputR30 = 8;
int OutputR40 = 9;
int OutputR50 = 10;

The outputs I understand are the different pressure valves, 5 to move shaft to left, 5 to right.
Or is he shaft spring loaded an all ten valves used to push against the spring.
Also

analogRead((analogInPin1)-(analogInPin2))

Change to

int ShaftInput= analogRead(ShaftPos);
int ShaftSetpoint= analogRead(Setpoint);
int Error= ShaftInput-ShaftSetpoint;

Now use Error variable instead of

analogRead((analogInPin1)-(analogInPin2))

Repeating the same functions over and over is as boring as someone saying the same words to you in every sentence.
It makes sketches much easier to read if long terms are kept to a minimum, also the tendency to produce spelling errors that cause compilation problems.

Good to see you having a go, just need some advice on getting it readable.

In the IDE in the Tool bar the is a tab TOOLS, click on it , then click on AUTO FORMAT.
It will help with indenting.

Tom....... :slight_smile: