Arduino DualShock 4 C ++

I am trying to control an Arduino uno board using DualShock 4 (PS4). I find it difficult to program joysticks; PS4.getAnalogHat(LeftHatY)I want to control the engine with a joystick; I want the engine to move forward when I push up ( ++i), back when I press ( --i), and without speed when I do not move the joystick. I can move the engine in one direction, and the speed increases, but I can not get the other direction to work. I do not see the connection between the values ​​of the joystick (PS4.getAnalogHat(LeftHatY) > 137 || PS4.getAnalogHat(LeftHatY) < 117)and the values ​​of the engine ( 0- 255).

I use a USB screen and a motor shield.

I need help defining the first if statement.

Here is the code that I still have:

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

I want to increase the M3 value while increasing the joystick angle:

for (int i=0; i<=255; ++i)
    M3->setSpeed(i);
+4
2

min(117) max(137) . , posibles

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

, .

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

, . Dualshock 4 Arduino Motor. . 3D- /, .

0

Source: https://habr.com/ru/post/1673930/


All Articles