Pages: [1]   Go Down
Author Topic: Unexpected Sensor Readings  (Read 1243 times)
0 Members and 1 Guest are viewing this topic.
Ohio
Offline Offline
Newbie
*
Karma: 2
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?   smiley-confuse 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;

}
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe you have a Logarithmic Potentiometer ?
Logged

Ohio
Offline Offline
Newbie
*
Karma: 2
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Good luck with the project smiley
Logged

Ohio
Offline Offline
Newbie
*
Karma: 2
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. ???
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Ohio
Offline Offline
Newbie
*
Karma: 2
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

  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.
Logged

Ohio
Offline Offline
Newbie
*
Karma: 2
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok.  lets try the pic one more time.


* IMAGE_012.jpg (189.27 KB, 1200x1600 - viewed 30 times.)
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Ohio
Offline Offline
Newbie
*
Karma: 2
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for your time.  really appreciate it!!!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 50243
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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().

Code:
  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 }.

Code:
             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?
Logged

Ohio
Offline Offline
Newbie
*
Karma: 2
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink 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!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 50243
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
thanks for the help and criticism!
Critique, not criticism.

Quote
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.
Logged

Pages: [1]   Go Up
Jump to: