Arduino Dualshock 4

I’m trying to control an Arduino uno board using a DualShock 4, I’m having difficulty programming the joysticks “PS4.getAnalogHat(LeftHatY)” I want to control a motor using the joystick; I want to 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 the other direction to work. I can’t seem make a connection between the joystick value (PS4.getAnalogHat(LeftHatY) > 137 || PS4.getAnalogHat(LeftHatY) < 117) and the motor values (0 - 255).

I’m using a USB Shield and a Motor Sheild

here’s the code I have so far.



(Libray & void setup… etc)

void loop ()
{
Usb.Task();

if (PS4.connected())
{
if ( PS4.getAnalogHat(LeftHatY) > 137 || PS4.getAnalogHat(LeftHatY) < 117)
{
M3->setSpeed(PS4.getAnalogHat(LeftHatY));
PS4.setLed(Green);
PS4.setLedFlash(100 ,100);
}

if (PS4.getAnalogButton(R2))
{
M3->setSpeed(PS4.getAnalogButton(R2));
PS4.setLed(Yellow);
PS4.setLedFlash(50 , 50);
}

if (PS4.getButtonClick(CROSS))
{
M3->run(BACKWARD);
M3->setSpeed(255);
PS4.setLed(Blue);
PS4.setLedFlash(10, 10);

}

if (PS4.getButtonClick(CIRCLE))
{
M3->setSpeed(0);
PS4.setLed(Red);

PS4.setLedFlash(1 ,1);
}
}
}

M3->setSpeed(PS4.getAnalogHat(LeftHatY));

will set the motor speed to a value greater than 137 or less than 117

Is that what you want or do you need a different range ?

I want the range to be from -255 to 255.

When I push the joystick down I want to have full backward (-255)

When I push the joystick up I want to have full forward (255)

when I don’t move the joystick I want to have full stop (0)

the 137 and 117 come from the example I can’t get it to work if I change these numbers .

So :

if the value is >137
output 255
else
if the value is < 117
output -255
else
output 0

Looks like a series of if/else tests to me

Thanks for the suggestion this is what I got:

if ( PS4.getAnalogHat(LeftHatY) > 137 )
{
M3->setSpeed(15);
M3->run(FORWARD);
PS4.setLed(Green);
PS4.setLedFlash(100 ,100);
}

if ( PS4.getAnalogHat(LeftHatY) < 117 )
{
M3->setSpeed(15);
M3->run(BACKWARD);
PS4.setLed(Yellow);
PS4.setLedFlash(100 ,100);
}

I get an error when I put:

else M3->setspeed(0);

I only get rotation in on direction but when I omit the else statement I the joystick working in both directions but it’s continuous it won’t stop when I don’t move the joystick.

nevermind.

the else statement works!!!
my speed values were too low (couldn’t register) I was using 15 instead of 255, :stuck_out_tongue:

else M3->setSpeed(0); /////////works fine

if ( fix1 != PS4.getAnalogHat(LeftHatY) > 137 || fix2 != PS4.getAnalogHat(LeftHatY) < 117 )
{
M3->setSpeed(0); //////////boolean also works
}

Thanks A Lot!!!

return 0;