Hi Rosamunda
Here is the Arduino side code:
// Andro_Pan&Tilt V2.6
// Arduino test sketch for Joystick BT commander
// Controls two servo motors
// V2.6 can receive both Byte (Joystick BT commander V2.5)& Integer data (V3.0)
// V2.0: removed SoftwareSerial
// Arduino pin #0 to TX Bluetooth module
#include
#define DEBUG false
#define ledPin 13 // LED pin
#define pinServo_X 9
#define pinServo_Y 10
#define STX 0x02
#define ETX 0x03
#define MIN_Y 45 // vertical move limitation
#define MAX_Y 180 // horizontal move limitation
#define ZERO_Y 60
int i=0;
byte cmd[6] = {0, 0, 0, 0, 0, 0};
Servo myservoX; // create servo object
Servo myservoY;
void setup() {
Serial.begin(57600);
myservoX.attach(pinServo_X);
myservoY.attach(pinServo_Y);
pinMode(ledPin, OUTPUT);
if(DEBUG) Serial.println("AndroZinZin V2.5");
}
void loop() {
if(Serial.available()) { // received from smartphone
delay(5);
cmd[0] = Serial.read();
// if(DEBUG) Serial.println(cmd[0]); // ** DEBUG **
if(cmd[0] == STX) {
i=1;
while(Serial.available() && ((cmd[i]=Serial.read()) != ETX)) {
if(i>5) break;
// if(DEBUG) {Serial.print(i); Serial.print(": "); Serial.println(cmd[i]);} // ** DEBUG **
i++;
}
}
if(i==2) setLED(cmd[1]);
if(i==3) setPosition_Byte(cmd[1], cmd[2]);
if(i==5) setPosition_Int(cmd[1]*128+cmd[2], cmd[3]*128+cmd[4]);
}
delay(5);
}
void setLED(int LEDstatus) {
switch (LEDstatus) {
case '1':
Serial.println("Button_1: ON");
// your code...
break;
case '2':
Serial.println("Button_1: OFF");
// your code...
break;
case '3':
Serial.println("Button_2: ON");
// your code...
break;
case '4':
Serial.println("Button_2: OFF");
// your code...
break;
case '5':
Serial.println("Button_3: ON");
// your code...
break;
case '6':
Serial.println("Button_3: OFF");
// your code...
break;
case '7':
Serial.println("Button_4: ON");
// your code...
break;
case '8':
Serial.println("Button_4: OFF");
// your code...
break;
}
}
void setPosition_Byte(byte posX, byte posY) { // Joystick BT commander V2.5
posX = map(posX, 10, 110, 180, 0);
posY = map(posY, 10, 110, 0, 180);
posY+=ZERO_Y;
posY = constrain(posY, MIN_Y, MAX_Y);
if(DEBUG) { Serial.print("(Byte) "); Serial.print(posX); Serial.print(", "); Serial.println(posY); } // ** DEBUG **
myservoX.write(posX);
myservoY.write(posY);
}
void setPosition_Int(int posX, int posY) { // Joystick BT commander V3.0
posX = map(posX, 10, 210, 180, 0);
posY = map(posY, 10, 210, 0, 180);
posY+=ZERO_Y;
posY = constrain(posY, MIN_Y, MAX_Y);
if(DEBUG) { Serial.print("(Int) "); Serial.print(posX); Serial.print(", "); Serial.println(posY); } // ** DEBUG **
myservoX.write(posX);
myservoY.write(posY);
}
Let me know the outcome in the Project main thread
Enjoy