I am still a intermediate beginner so my code may not be perfect:
I am thinking of building a robot arm and controlling suing a PS3 controller, bluetooth dongle, and USB host shield
I wrote the code for it, but I am not sure if it does what I want it to do.
I want the robot to go left when, on the left joystick X - axis moves left, and I want it to stay at that position not move back as the joystick moves back to the center
Here is the code:
#include <Servo.h>
#include <PS3BT.h>
#include <usbhub.h>
#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>
#endif
USB Usb;
BTD Btd(&Usb);
PS3BT PS3(&Btd);
bool printTemperature;
bool printAngle;
Servo servoT;
Servo servoY;
int position;
void setup()
{
}
void loop() {
if((PS3.getAnalogHat(LeftHatX)< 128 ) && (PS3.getAnalogHat(LeftHatX) >= 0 )){
for(position = 90; position >= 0; position--) {
servoT.write(position);
delay(20);
}
}
if((PS3.getAnalogHat(LeftHatX) > 128 )&& (PS3.getAnalogHat(LeftHatX))<= 255){
for(position = 90; position <= 180; position++) {
servoT.write(position);
delay(20);
}
}
if((PS3.getAnalogHat(RightHatX) < 128 )&& (PS3.getAnalogHat(RightHatX) >= 0 )){
for(position = 90; position >= 0; position--) {
servoY.write(position);
delay(20);
}
}
if((PS3.getAnalogHat(RightHatX) > 128 )&& (PS3.getAnalogHat(RightHatX) <= 255 )){
for(position = 90; position <= 180; position++) {
servoY.write(position);
delay(20);
}
}
}