AnalogRead issues with two sensors

Hello all,

I’m having an issue with a project I’m working on. I have a pressure transducer and a potentiometer wired to the analog inputs on a ChipKit UNO32. I read the values from both sensors and use the data to control a hydraulic needle valve hooked to a stepper motor. This system will eventually be wired to a National Instruments DAQ to provide automated pressure control on a hydraulic fixture.

My problem is as follows: As long as the difference in voltage between the two sensors (Both are 0-5v output) is within a certain window the system works fine and will maintain whatever pressure it is set to in spite of small pressure fluctuations. However if the voltage difference increases beyond that stable window the stepper motor will just continuously turn in one direction unless I turn the potentiometer quickly and “catch” it. My goal is to be able to send the controller a 0-5 volt signal from my DAQ and have the valve respond in order to produce that pressure. I’m fairly new to programming in this language, any help is appreciated.

int potPin = 1;
int pressPin = 0;
int dirPin = 6;
int stepperPin = 5;
int ledPin = 8;
int ledPin2 = 9;

int potVal = 0;
int potTotal = 0;
int pressVal = 0;
int pressTotal = 0;
int diff = 0;

void setup() {
  pinMode(dirPin, OUTPUT);
  pinMode(stepperPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(potPin, INPUT);
  pinMode(pressPin, INPUT);

void step(boolean dir,int steps){
  for(int i=0;i<steps;i++){
    digitalWrite(stepperPin, HIGH);
    digitalWrite(stepperPin, LOW);

void loop(){
  for (int i=0; i<=5; i++) {    
    potVal = analogRead(potPin);
    pressVal = analogRead(pressPin);   
    potTotal = potTotal + potVal;
    pressTotal = pressTotal + pressVal; 
    potTotal = potTotal/5;
    pressTotal = pressTotal/5;
    diff = potTotal - pressTotal;
  if (diff >=-8 && diff<=8) {
    digitalWrite (ledPin, HIGH);
    digitalWrite (ledPin2, LOW);
  else if (diff>>8) {
    digitalWrite (ledPin, LOW);
    digitalWrite (ledPin2, LOW);
  else if (diff<<-8) {
    digitalWrite (ledPin, LOW);
    digitalWrite (ledPin2, LOW);
  else {
    digitalWrite (ledPin, LOW);
    digitalWrite (ledPin2, HIGH);
    digitalWrite (ledPin2, LOW);

You are using << and >> - what do you think those operators do? (Hint, not comparisons…)

Ah… that is rather embarrassing, bit shifting wasn’t really what I was going for :grin:

Thanks for your help Mark.

Actually I had to look twice... :wink: