So iv'e been using a ps3 controller to control servos, I connected everything via A USB host shield and A dongle for wireless feasibility. It all works great and I was Able to control 2 servos at once with the 2 joysticks, but then I encountered a problem.
I need to control 6 different servos in my project, and since the ps3 controller doesn't have 6 joysticks i need to use buttons (X,O,square...) to control the servos.
I then relised I have no Idea how to do that
my question to you guys is how do I make it so When I press a certain button on the PS3 cont, the allocated servo moves in consideration of long the button is being pressed down. So like for example every 1 second the button is pressed the servo moves 10 degrees.
I'm not a proficient coder... but I would do something like this.. Code below is just an example...
If(ButtonState==high){ //if button is pushed
if(ButtonStart==0){ // see if it's the first loop it was on high
ButtonStart = Millis(); // set the time it was first pushed
}
}
else{ // button not pushed anymore
if(ButtonStart > 0){ // see if we have a time for when it was pushed
ButtonTime = Millis() - ButtonStart; // use time from when button was pushed until now
ServoMove = ButtonTime /100; // 1 second = 1000 ms so 1000/100 = 10 (may want to round ServoMove to nearest whole number)
ButtonStart=0; // reset our start time..
}
}
The code above wont move the servo until the button is released
If(TRIANGLEState==high){ //if button is pushed
if(TRIANGLEStart==0){ // see if it's the first loop it was on high
TRIANGLEStart = Millis(); // set the time it was first pushed
}
}
else{ // button not pushed anymore
if(TRIANGLEStart > 0){ // see if we have a time for when it was pushed
TRIANGLETime = Millis() - TRIANGLEStart; // use time from when button was pushed until now
ServoMove = TRIANGLETime /100; // 1 second = 1000 ms so 1000/100 = 10 (may want to round ServoMove to nearest whole number)
TRIANGLEStart=0; // reset our start time..
}
}
The PS3 class has (or should have) methods for reading all the controls on the PS3 thing.
It does as it knows "TRIANGLE" is the button triangle but in order to get more complicated with the Buttons and make them move by how long im holding the buttons down I need to define the "ButtonState" and "ButtonTime".
I just don't know how to make the arduino software know im talking about the Triangle button when defining the state and Time
Here is my current code in which I was able to move the servo 180 degrees with the Button "TRIANGLE" and back 180 degrees with the button "CROSS"
#include <PS3BT.h> //Include the necessary libraries.
#include <Servo.h>
USB Usb;
BTD Btd(&Usb);
PS3BT PS3(&Btd);
Servo servo1; //Create instances of type Servo. servo1 is the steering servo and servo2 is the ESC.
Servo servo2;
int pos = 0;
void setup() {
Serial.begin(115200);
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while(1); //halt
}
Serial.print(F("\r\nPS3 Bluetooth Library Started"));
servo1.attach(7);
servo2.attach(6);
pinMode(TRIANGLE,INPUT);
}
void loop()
{
Usb.Task();
for(pos = 0; pos < 180; pos += 1)
if(PS3.PS3Connected || PS3.PS3NavigationConnected){
if (PS3.getButtonClick(TRIANGLE)==HIGH)
servo1.write(pos);
}
for(pos = 180; pos>=1; pos-=1)
if(PS3.getButtonClick(CROSS)==HIGH)
servo1.write(pos);
if (PS3.getButtonClick(PS)) {
Serial.print(F("\r\nPS"));
PS3.disconnect();
}
}
Consistent use, and placement of curly braces would make your code more readable. Random indenting sucks. Tools + Auto Format takes care are that, with little effort on your part.
Testing for the state of the switch inside the for loop does not make sense. Testing in one case that the device is connected, but not in the other, does not make sense.
if(PS3.PS3Connected || PS3.PS3NavigationConnected)
{
if(PS3.getButtonClick(TRIANGLE)==HIGH)
{
for(int pos = 0; pos < 180; pos++)
{
servo1.write(pos);
delay(5); // Give the servo time to move
}
}
else if(PS3.getButtonClick(CROSS)==HIGH)
{
for(int pos = 1800; pos >= 0; pos--)
{
servo1.write(pos);
delay(5); // Give the servo time to move
}
}
}
I just don't know how to make the arduino software know im talking about the Triangle button when defining the state and Time
Look at the state change detection example. That it deals with a switch that is wired directly to a pin is irrelevant. You can easily change digitalRead(somePin) to PS3.getButtonClick(someName).
Anyway I found a way to Make the TRIANGLE be defined as "button"
const int button= (PS3.getButtonClick(TRIANGLE));
But after I upload this code
if(PS3.PS3Connected || PS3.PS3NavigationConnected){
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
}
In the serial monitor the push counter goes uncontrollable and the button in seconds, the counter reaches thousands without me pushing any button.