Hello Together
I'm working on a PS3 controlled RC Car. I am communicating over two arduino uno and two xbee S2 modules.
Now to my problem:
How can i get two bytes from the serial line and match them to the right servo. The first byte(value0-180) is for the steering. The second is for Speedcontrol ESC.
I wrote a code for only one byte and that works quite well.
Greatings
Fabrice
#include <Servo.h>
Servo myservo;
byte incomingByte = 90;
void setup() {
myservo.attach(7);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// see if there's incoming serial data:
if (Serial.available()>0)
{
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
myservo.write(incomingByte);
}
}
sender:
#include <PS3USB.h>
USB Usb;
PS3USB PS3(&Usb); // This will just create the instance
byte potvallenkung = 90;
int potvalspeed = 90;
int potvalbreak = 90;
byte speedval = 90;
String datapacket;
uint8_t state = 0;
void setup() {
Serial.begin(9600);
while (!Serial); // Wait for serial port to connect
if (Usb.Init() == -1)
{
Serial.print(F("\r\nOSC did not start"));
while (1); //halt
}
Serial.print(F("\r\nPS3 USB Library Started"));
}
void loop()
{
Usb.Task();
//Auswertung Daten Lenkung
potvallenkung = PS3.getAnalogHat(LeftHatX); // Linker Stick X-Achse auslesen
potvallenkung = map(potvallenkung, 0, 255, 0, 180); // Analoger wert umrechnen für Servolibrary
potvallenkung = constrain(potvallenkung, 5, 175); // Sicherheitsbereich für servo definieren min 5° max 175° variable als byte
//Auswertung Daten Speedcontrol
potvalspeed = PS3.getAnalogButton(R2);
potvalspeed = map(potvalspeed, 0, 255, 90, 180);
potvalspeed = constrain(potvalspeed, 90, 180);
potvalbreak = PS3.getAnalogButton(L2);
potvalbreak = map(potvalbreak, 0, 255, 90, 0);
potvalbreak = constrain(potvalbreak, 0, 90);
speedval = ((potvalspeed + potvalbreak)-90);
Serial.write(potvalspeed);
Serial.write(speedval);
/*datapacket = (String((potvallenkung+100))+String((speedval+100)));
int dataint = datapacket.toInt();
Serial.println(dataint);
delay(20);*/
}