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);
}