Potometer Reading

Hi! I am trying to use a potometer for the speed of my car. The car has 4 modes. stop: 0 volts, fast: 5 volts, normal: 2 volts, and pot: controlled by a potometer. The first 3 modes work but not the pot. Here is my code. Any help would be highly appreciated.

This is a part of the code.

void getPotVoltage(){
  potVal = analogRead(A0);
  // Convert the analog reading (w
  potVolts = potVal * (5.0 / 1023.0);
  analogWrite(motorPin, potVolts);
}

void ledToggle(){
  if(toggleNum==4){
     toggleNum = 0;
  }
    if(toggleNum==0){
     mode1();
  }
    if(toggleNum==1){
     mode2();
  }
    if(toggleNum==2){
     mode3();
  }
    if(toggleNum==3){
     mode4();
  }
}

void mode1(){
  stopLeds();
  digitalWrite(ledPins[0], HIGH);
  powerMotor(0);
}

void mode2(){
  stopLeds();
  digitalWrite(ledPins[1], HIGH);
  powerMotor(2);
}

void mode3(){
  stopLeds();
  digitalWrite(ledPins[2], HIGH);
  powerMotor(5);
}

void mode4(){
  stopLeds();
  digitalWrite(ledPins[3], HIGH);
  powerMotor(123);
  
}

void stopLeds(){
  for (int i = 0; i<=3; i++){
    digitalWrite(ledPins[i], LOW);
  }
}

void powerMotor(int a){
  if(a==0){
    analogWrite(motorPin, 0);
  }
  if(a==2){
    analogWrite(motorPin, 102);
  }
  if(a==5){
    analogWrite(motorPin, 255);
  }
  if(a==123){
    delay(10);
    buttonLock=0;
    while(buttonLock==0){
      buttonPress();
      getPotVoltage();
      Serial.println(potVolts);
    }
  }
}

Sorry about bad programming. I haven’t done arduino in a while.

potometer is a device used for measuring the rate of water uptake of a leafy shoot

I am guessing you mean a potentiometer?

Potentiometer?

// Convert the analog reading
potVolts = potVal * (5.0 / 1023.0) ;
analogWrite(motorPin, potVolts) ;

What is happening here?

What is the range of potVolts?

.

For the analogWrite(), you want a value between 0 and 255, not a voltage.
Try mapping the 0 to 1023 pot value to 0 to 255 with map():-

potVal = analogRead(A0);
PWMVal = map(potVal, 0, 1023, 0, 255)
analogWrite(motorPin, PWMVal);

Also, when you are converting the pot(entiometer) value to a voltage, you should really divide by 1024, not 1023:-potVolts = (potVal * 5.0) / 1024;
Edit: Larry posted as I was typing, but my answers are still valid. :slight_smile:

High OldSteve, are you shovelling snow down there :wink:

I was trying to move the OP at a slower rate.

.

larryd:
High OldSteve, are you shovelling snow down there :wink:

G'day Larry. No, it never snows here. Lowest it gets is 5C. Pretty cool at night right now though.

I was trying to move the OP at a slower rate.

Not me. Wham, bam....... :smiley:

Could do this too:
potVolts = (potVal * 255)/ 1023;

larryd:
Could do this too:
potVolts = (potVal * 255)/ 1023;

Not for potVolts. :smiley:
And for it to work to get the PWM value, it needs to be:-PWMVal = (potVal * 255.0)/ 1023;(I think that 1024 is the correct divisor, too, rather than 1023. Seems that’s open to debate, though.)

OldSteve:
Not for potVolts. :smiley:
And for it to work to get the PWM value, it needs to be:-PWMVal = (potVal * 255.0)/ 1023;(I think that 1024 is the correct divisor, too, rather than 1023. Seems that's open to debate, though.)

Not for potVolts. :smiley:
True

1023, 1024, what's 1 amongst friends ;D

If potVal was 1023:
PWMVal = (1023 * 255)/ 1023 = 255
PWMVal = (1023 * 255)/ 1024 = 254.75

How's woodworking?

.

larryd:
1023, 1024, what's 1 amongst friends ;D

If potVal was 1023:
PWMVal = (1023 * 255)/ 1023 = 255
PWMVal = (1023 * 255)/ 1024 = 254.75

1023 looks better in that situation, but I still think that 1024 is the right value to use. (I followed the thread on the subject a couple of days ago.)
Either way, the error is miniscule.

How's woodworking?

I haven't done any in ages, Larry. I gave it up due to the dust. My lungs aren't the best. I've been meaning to start selling off the machinery and tools, but it's hard to part with them.

OldSteve:
I haven't done any in ages, Larry. I gave it up due to the dust. My lungs aren't the best. I've been meaning to start selling off the machinery and tools, but it's hard to part with them.

Yes the data sheet says to use 1024.

I made an air filter box (with a large squirrel cage fan) for my cutting room to get rid of the dust, works great.

So, you can't cut wood, have wine, women or song.
Ok, maybe you can still sing a lot.

.