Reputation: 41
I'm trying to control an Arduino uno board using a DualShock 4(PS4). I'm having difficulty programming the joysticks PS4.getAnalogHat(LeftHatY)
I want to control a motor using the joystick; I want the motor to go forward when I press up(++i
), backward when I press down(--i
), and no speed when I don't move the joystick. I'm able to move the motor in one direction and the speed increases but I can't get the other direction to work. I can't seem make a connection between the joystick values (PS4.getAnalogHat(LeftHatY) > 137 || PS4.getAnalogHat(LeftHatY) < 117)
and the motor values (0
- 255
).
I'm using a USB Shield and a Motor Shield.
I need help figuring out the first if statement.
Here's the code I have so far:
if (PS4.connected())
{
if (PS4.getAnalogHat(LeftHatY) > 137)
{
M3->setSpeed(255));
PS4.setLed(Green);
PS4.setLedFlash(100 ,100);
}
}
I want the value for M3 to increase as I increase joystick angle:
for (int i=0; i<=255; ++i)
M3->setSpeed(i);
Upvotes: 4
Views: 1135
Reputation: 41
uint16_t s = PS4.getAnalogHat(RightHatY);
if (PS4.getAnalogHat(RightHatY) < 117 )
{
s = map (s, 117, 0, 0, 250);
M3->run(FORWARD);
M3->setSpeed(s);
PS4.setLed(Green);
PS4.setLedFlash(100, 100);
}
if (PS4.getAnalogHat(RightHatY) > 137)
{
s = map (s, 137, 0, 0, -250);
M3->run(BACKWARD);
M3->setSpeed(s);
PS4.setLed(Green);
PS4.setLedFlash(100, 100);
}
This is the solution to the problem I originally asked. Variable speed control of a motor using a Dualshock 4 & Arduino Motor shield. Thanks a lot for the help. I was 3d Printing parts for the robot/chassis which is why I took time off programming.
Upvotes: 0
Reputation: 487
You need to add the condition of min(117)
and max(137)
tolerance to move motor. And then, map the posibles values of joystick to the range of motor's speed
if (PS4.connected())
{
int analogValue = PS4.getAnalogHat(LeftHatY);
if (analogValue > 137 || analogValue < 117)
{
int motorValue = y = map(x, 117, 137 , 0, 255);
M3->setSpeed(motorValue);
PS4.setLed(Green);
PS4.setLedFlash(100 ,100);
}
}
It could be better with some error handling but you could start with it.
Upvotes: 1