Unexpected Sensor Readings

Hi all!

Working on my first arduino project and running into some problems. Basically I am reading the position of a potentiometer as a steering wheel and adjusting the speed on one of two motors to turn. I have included my code (please excuse its condition. its not complete and still has commented out Serial.print’s from debugging) I am having two issues. The first is the readings from the POT. Since I am reading it from the A1 pin its value should be between 0 and 1023, which it is. What confuses me is that 513 is not at the centre of the knobs travel. Should it be? :~ Second, with the ‘if’ statement before the function call I don’t understand why the function is being call continuously. I’m sure I should include more info, but its getting late and Im not sure what. Any help is GREATLY appreciated!!!

int oldValue = 0;

void setup() {}

void loop()
{

int potValue = analogRead(A1); //Read value from POT

if ( potValue != oldValue ) { //If POT value has changed

dualMotorSteering ( potValue ); //adjust motor speeds to control direction

oldValue = potValue ;

}

}

void dualMotorSteering (int sensorValue) {

Serial.begin(9600); // open serial communication (for dubugging)

int switchVal = 0;
int speedA = 256;
int speedB = 256;

//Serial.println (“in”);

if (sensorValue < 512) { // Determine if the potentiometer is in a Center, LOC or ROC position
switchVal = 1;
}
else {
switchVal = 2;
}

switch(switchVal) {

case 1: // POT is LOC

speedB=(sensorValue /4); // Convert POT val to Speed val

if (speedB <= 4) { // If speed is below min, set to min
speedB = 1; }

if (speedB >= 256) { // If speed is above max, set to max
speedB = 256; }

// Serial.println (“c1”);

break;

case 2: // POT is ROC

speedA=(sensorValue /4); // Convert POT val to Speed val

if (speedA <= 4) { // If speed is below min, set to min
speedA = 1; }

if (speedA >= 256) { // If speed is above max, set to max
speedA = 256; }

// Serial.println (“c2”);

break;

analogWrite(speedBpin, speedB);

}

Serial.print (sensorValue, DEC);
Serial.print (" : “);
Serial.print (speedB, DEC);
Serial.print (” : ");
Serial.println (speedA, DEC);

//Serial.println (“out”);
return;

}

Maybe you have a Logarithmic Potentiometer ?

good possibility. didn't even know there was such a thing. It does seem to taper. I will check its resistance with the multimeter tomorrow. thank you!

And, a tip: Use the when you post code. It's much easier to read that way.

Good luck with the project :)

ok...so i grabbed the meter tonight but now am even more confused. The POT reaches peak resistance near the middle 1.46 (with meter at 20k), reads 0.00 at far right and 0.23 at far left. ???

Sounds like you have something in parallel. Maybe you could post a schematic or a simple sketch of how you've wired it together?

Well...lets see if I have the image posted right. if so, it isnt the best, but it should do. btw, I tested directly off the leads of the pot so i dont know if the circuit would make a difference or not.

ok. lets try the pic one more time.

That looks exactly right. I've got to get to bed (06 AM) now, but I'm sure some hardware guy will spot the area of concern a lot quicker than me.

Good luck!

thanks for your time. really appreciate it!!!

I’m not sure what your hardware problem is, although I suspect a bad potentiometer. There are some things about the code that bug me, though.

The call to Serial.begin() in the dualMotorSteering() function is the first thing. That call belongs in setup().

  if ( potValue != oldValue ) {  //If POT value has changed
    dualMotorSteering ( potValue ); //adjust motor speeds to control direction
    oldValue = potValue ;
   }

oldValue only gets assigned a new value if dualMotorSteering gets called. It should get assigned a new value regardless of whether dualMotorSteering get called or not. Move that statement after the }.

             if (speedB <= 4) {     // If speed is below min, set to min       
             speedB = 1;  }

Is the minimum 1 or 4?

A value is potentially computed for speedA, but never used. Why not?

Hi PaulS. I was beginning to wonder about the pot myself. Ill stop into the shack on my way to work to replace it. as far as the code goes...

Serial.begin() is placed in the function primarily because I plan on using it as a copy and paste funtion in the future. once everything is working I plan on the function having a debug option. dualMotorSteering(int sensorValue, boolean deBug) where if the debug option is used it sends the motor output values to the serial monitor.

oldValue was being assigned a value outside of the if statement but I moved it. Im not sure why, but the function is being called whether the pot value changes or not. Moving the assignment location didn't help.

The minimum speed value is 1. the pot returns a value between 0 and 1023 but my motor driver limits are 0 to 256 ( or 1/4 of the pot value) I put that if statement in to prevent getting less than whole numbers. the code is adapted from my first time playing with the driver and im sure isn't the best.

speedA isn't used since I haven't gotten that far yet ;) the first code I had written used the pot and 1 motor. That seemed to work so I moved on to the second.

thanks for the help and criticism!

thanks for the help and criticism!

Critique, not criticism.

Serial.begin() is placed in the function primarily because I plan on using it as a copy and paste funtion in the future. once everything is working I plan on the function having a debug option. dualMotorSteering(int sensorValue, boolean deBug) where if the debug option is used it sends the motor output values to the serial monitor.

This is OK, except that Serial.begin() should be called just once, not every time you need to send output. Using something like:
#define SerDebug
at the top of the program, and
#ifdef SerDebug
Serial.println(something useless);
#endif
would allow you to turn debug output off by just commenting out the #define statement.