Motor shield reversed with joystick

UNO R3, Arduino motor shield R3, dc brushed motor, analog joystick.

Joystick to left (532-1023) motor moves as expected, joystick right (492-0), motor reverses as expected but duty cycle is reversed (motor starts full speed, reduces as stick moved right).

Any help will be greatly appreciated
Dennis

/****
 * This is an attempt to control a Motor Shield with a joystick
 * Processor board is an Arduino UNO v3
 * Shield is an Arduino Motor Shield r3
 * 
 */
int analogInPin = A0;

int directionPin = 12; //asign direction pin

int brakePin = 9;  //asign brake pin

int pwmPin = 3;  //asign pwm pin

int joystickValuex = 0; //center of pot x axis

//int joystickValuey = 127.5; //center of pot y axis

int outputValue = 0; //start with "0" output



void setup()

{

  Serial.begin(9600);

  //define pins
  pinMode(directionPin, OUTPUT);
  pinMode(pwmPin, OUTPUT);
  pinMode(brakePin, OUTPUT);

}

void loop()

{

  joystickValuex = analogRead(analogInPin) / 1;

  outputValue = map(joystickValuex, 0, 4096, 0, 100);

  analogWrite(pwmPin, joystickValuex) ;

  //scale value to between 0 and 180
  //joystickValuex = map(joystickValuex, 0, 1000, 0, 200); 

  if (joystickValuex >= 4)


  {

    //example

    digitalWrite(brakePin, LOW);

    digitalWrite(directionPin, HIGH);

    analogWrite(pwmPin, joystickValuex);

    delay(10);

  }

  else

  { digitalWrite(brakePin, LOW);

    digitalWrite(directionPin, HIGH);

    //Serial.print(directionPin);

    analogWrite(pwmPin, joystickValuex);

  }

  delay(30);
  
  // print out the value you read:
  Serial.println(joystickValuex);
  // Serial.println(joystickValuey);
}

An analogRead() gives you an integer between 0 and 1023. An analogWrite() requires an integer between 0 and 255 for the PWM value.

My best guess:

/****
   This is an attempt to control a Motor Shield with a joystick
   Processor board is an Arduino UNO v3
   Shield is an Arduino Motor Shield r3

*/
const int analogInPin = A0;
const int directionPin = 12; //asign direction pin
const int brakePin = 9;  //asign brake pin
const int pwmPin = 3;  //asign pwm pin


void setup()
{
  Serial.begin(9600);

  //define pins
  pinMode(directionPin, OUTPUT);
  pinMode(pwmPin, OUTPUT);
  pinMode(brakePin, OUTPUT);
}

void loop()
{
  int joystickValuex = analogRead(analogInPin);

  if (joystickValuex < 493)
  {
    digitalWrite(brakePin, LOW);
    digitalWrite(directionPin, HIGH); // Right
    analogWrite(pwmPin, map(joystickValuex, 492, 0, 0, 255));
  }
    else if (joystickValuex > 531)
  {
    digitalWrite(brakePin, LOW);
    digitalWrite(directionPin, LOW); // Left
    analogWrite(pwmPin, map(joystickValuex, 532, 1023, 0, 255));
  }
  else
  {
    // Dead zone
    digitalWrite(pwmPin, 0); // Stop
    digitalWrite(brakePin, HIGH);  // Apply brake
  }

  delay(30);

  // print out the value you read:
  Serial.println(joystickValuex);
  // Serial.println(joystickValuey);
}

@johnwasser

This worked after i re assigned A0 to A2. On the motor shield A0 & A1 are used for current sensing, if used as potpins the results are seemingly unpredictable.

Thank you for pointing out the else if statement where i was just using multiple if's.

Any pointers on your re write are welcome. Im here to learn the language. Long time RS274 (CNC g-code) programmer here, so some of it makes sense... im learning.

@ToddL1962

I appreciate youre input, im not sure how it ties in here, im a total rookie.

If you could tie it in, I'd appreciate it, as I'm here to learn.

Thanks in advance.

I would also like to add another analog input, I'm still trying (for no good reason) to not use a .h file.

If anyone can point me in the right direction...

Thanks

Like a Y axis? What do you want it to do?

I'd like it to control the second axis the same exact way the first (X axis) on the motor shield is controlled.

I'm starting to read the array section. I'm assuming that's what I need?

With just two, arrays might be overkill. I usually recommend arrays if you have three or more of the same object all doing roughly the same thing. For two, it's easy enough to come up with names for the variables.

So would i add additional "if" or "els if" blocks for the y axis?

I added definitions for y, but think i need some additional action.

Yes. Do the same as for X. Get the value from the analog input pin and use 'if' and 'else if' to decide what the various ranges of value do.

It works in two axis, or both motor channels in dc brushed motor configuration.

So basically, i can use "else if" as many times as needed?

You're great for helping out. Thank you.

Here's the code.

`/**** This is an attempt to control a Motor Shield with a joystick Processor board is an Arduino UNO v3 Shield is an Arduino Motor Shield r3 */

const int analogInPin0 = A2;
const int directionPin0 = 12; //asign direction pin
const int brakePin0 = 9; //asign brake pin
const int pwmPin0 = 3; //asign pwm pin
const int analogInPin1 = A3;
const int directionPin1 = 13; //asign direction pin
const int brakePin1 = 8; //asign brake pin
const int pwmPin1 = 11; //asign pwm pin
void setup()
{
  Serial.begin(9600); //define pins
  pinMode(directionPin0, OUTPUT);
  pinMode(pwmPin0, OUTPUT);
  pinMode(brakePin0, OUTPUT);
  pinMode(directionPin1, OUTPUT);
  pinMode(pwmPin1, OUTPUT);
  pinMode(brakePin1, OUTPUT);
} void loop()
{
  int joystickValuex = analogRead(analogInPin0) ;
  int joystickValueY = analogRead(analogInPin1) ;
  if (joystickValuex < 493)
  {
    digitalWrite(brakePin0, LOW);
    digitalWrite(directionPin0, LOW); // Right
    analogWrite(pwmPin0, map(joystickValuex, 492, 0, 0, 140));
    Serial.println ("X RIGHT");
  }

    else if (joystickValueY < 493)
    {
      digitalWrite(brakePin1, LOW);
      digitalWrite(directionPin1, LOW); // Right
      analogWrite(pwmPin1, map(joystickValueY, 492, 0, 0, 140));
      Serial.println ("Y DOWN");
    }
    else if (joystickValuex > 531)
    {
      digitalWrite(brakePin0, LOW);
      digitalWrite(directionPin0, HIGH); // Left
      analogWrite(pwmPin0, map(joystickValuex, 532, 1023, 0, 140));
      Serial.println ("X LEFT");
    }
    else if (joystickValueY > 531)
    {
      digitalWrite(brakePin1, LOW);
      digitalWrite(directionPin1, HIGH); // Left
      analogWrite(pwmPin1, map(joystickValueY, 532, 1023, 0, 140));
      Serial.println ("Y UP");
    }
    else   { // Dead zone

      digitalWrite(pwmPin0, 0); // Stop
      digitalWrite(brakePin0, HIGH); // Apply brake
      digitalWrite(pwmPin1, 0); // Stop
      digitalWrite(brakePin1, HIGH); // Apply brake
    }
    delay(10); // allow brae to act before reversing
    Serial.println(joystickValuex); //print out the value
    // Serial.println(joystickValuey);
  }
 `

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.