Need help with Potentiometer

Hallo,

i use a Motor Shield and want control the Motor with Poti (10K)

Poti from middle to left (backward with PWM)
Poti middle (brake)
Poti from middle to right (forward with PWM)

#define PWM_A 3          //Motor Shield
#define BRAKE_A 9        //Motor Shield
#define DIR_A 12         //Motor Shield
 
int JoyStick_Y = 5; //Y
 
void setup()
{
  pinMode(PWM_A, OUTPUT);
  pinMode(BRAKE_A, OUTPUT);
  pinMode(DIR_A, OUTPUT);
  Serial.begin(9600); // 9600 bps
}
void loop() {
  JoyStick_Y = analogRead(5);
  JoyStick_Y = map(JoyStick_Y, 512, 1023, 0, 255); //Forward
  JoyStick_Y = map(JoyStick_Y, 0, 512, 0, 255);    //Backward
  Serial.println(JoyStick_Y);
  
  if ((JoyStick_Y < 512) && (JoyStick_Y > 512)) {
    brake();
  }
  
  if (JoyStick_Y > 512) {
    forward();
  }
  
  if (JoyStick_Y < 512) {
    backward();
  }
 }

void forward() {
  digitalWrite(DIR_A, HIGH);
  digitalWrite(BRAKE_A, LOW);
  analogWrite(PWM_A, JoyStick_Y);
  delay(50);
}

void backward() {
  digitalWrite(DIR_A, LOW);
  digitalWrite(BRAKE_A, LOW);
  analogWrite(PWM_A, JoyStick_Y);
  delay(50);
}

void brake() {
  digitalWrite(BRAKE_A, HIGH);
  analogWrite(PWM_A, 0);
}

What’s the trouble?

I see you are using the serial monitor. Are you getting the JoyStick_Y values you expect?

Have you tried “hard coding” the motor speed and direction, bypassing the pot? And/or hard-coding JoyStick_Y values instead of reading & maping them from the actual pot?

I see a couple of “odd” things…

JoyStick_Y = map(JoyStick_Y, 512, 1023, 0, 255); //Forward
JoyStick_Y = map(JoyStick_Y, 0, 512, 0, 255);    //Backward

Every trime, right after you map “forward” you re-map “backward”. I don’t think that’s going to work.

 if ((JoyStick_Y < 512) && (JoyStick_Y > 512)) {

You are NEVER going to see a value that’s less than 512 AND greater than 512!!! :smiley:

In fact, you’ll find it very hard to set the pot for exactly 512 anyway. You might want to look for a small range around 512.

Hmm,

512 - 772 -> Motor turn forward 773 - 810 -> Motor stopped ??? 811 - 1023 -> Motor turn forward

287 - 512 -> Motor turn backward 257 - 286 -> Motor stopped ??? 0 - 256 -> Motor turn backward

 yVal1 = map(JoyStick_Y, 0, 511, 255, 0);       //Backward
 yVal2 = map(JoyStick_Y, 513, 1023, 0, 255);    //Forward

This code works

http://www.bajdi.com/arduino-rc-car-part-1-motor-control/

// http://www.bajdi.com
// Potentiometer controlling small dc motor through standalone L298n board
 
const int in1 = 2;    // direction pin 1
const int in2 = 4;    // direction pin 2
const int ena = 3;    // PWM pin to change speed
 
int pot;              // integer for potentiometer
int fspeed;           // forward speed
int bspeed;           // backward speed
 
void setup() {
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(ena, OUTPUT);
}
 
void loop() {
  pot = analogRead(A0);
 
  if (pot >= 480 && pot <= 540)
  {
    stop();
  }
  if (pot < 500)
  {
    fspeed = map(pot, 479, 0, 70, 250);
    forward(fspeed);
  }
  if (pot > 520)
  {
    bspeed = map(pot, 541, 1023, 70, 250);
    backward(bspeed);
  }
}
 
void stop()
{
  analogWrite(ena, 0);
}
 
void forward(int fspeed)
{
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(ena, fspeed);
}
 
void backward(int bspeed)
{
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(ena, bspeed);
}