This sketch is working really well but I have expo jumper installed on the ESC . If not, it's to sensitive with the joy stick commander app
Is it OK with the expo jumper ??
The signal can also be tuned within the Arduino code
I am powering my rover with a 3s lipo (4.1v x 3= 12.3 v fully charged) so I assume I will need to solder in a jumper off the main feed and tie it into an analog pin on the uno?
Don't do that you will fry your Uno
Analog pins accept no more than 5 Volts, you need a voltage divider
Back to full blown code, with feedback to smartphone
This is AndroTest_Servo_fullspool V1.1, with Dead Man implementation
#define VERSION "\n\nAndroTest_Servo_fullspool V1.x - @kas2016\ndemo for servo's"
// V1.1 added Dead Man feature
// V1.0 Basic implementattion
// Android BT Commander settings:
// Options/Options for advanced users/Data Range >>> -100 to +100
// Options/Options for advanced users/Refresh interval >>> 50ms
// Arduino pin #0 to TX BlueTooth module
// BT TX to be disconnected from D0 during sketch upload
#include <Servo.h>
#define pinServo_X 9 //<< ServoX pin
#define pinServo_Y 10 //<< ServoY pin
#define STX 0x02
#define ETX 0x03
#define ledPin 13
Servo myservoX; // create servo objects
Servo myservoY;
byte cmd[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // bytes received
byte buttonStatus = 0; // first Byte sent to Android device
long previousMillis = 0; // will store last time Buttons status was updated
long sendInterval = 750; // interval between Buttons status transmission (milliseconds)
long deadManInterval = 1100; // interval between Dead Man checks (milliseconds)
boolean deadManTimeout = false; // dead man flag
String displayStatus = "xxxx"; // message to Android device
int servo_L, servo_R = 90; // servo's positioning values
void setup() {
Serial.begin(57600);
myservoX.attach(pinServo_X);
myservoY.attach(pinServo_Y);
pinMode(ledPin, OUTPUT);
while(Serial.available()) Serial.read(); // empty RX buffer
}
void loop() {
if(Serial.available()) { // data received from smartphone
deadManTimeout = false; // reset Dead Man
delay(2);
cmd[0] = Serial.read();
if(cmd[0] == STX) {
int i=1;
while(Serial.available()) {
delay(1);
cmd[i] = Serial.read();
if(cmd[i]>127 || i>7) break; // Communication error
if((cmd[i]==ETX) && (i==2 || i==7)) break; // Button or Joystick data
i++;
}
if (i==2) getButtonState(cmd[1]); // 3 Bytes ex: < STX "C" ETX >
else if(i==7) getJoystickState(cmd); // 6 Bytes ex: < STX "200" "180" ETX >
}
}
sendBlueToothData();
checkDeadMan();
motorsControl();
}
void checkDeadMan() { // stop engines if signal is lost
static long previousMillis = 0;
long currentMillis = millis();
if(currentMillis - previousMillis > deadManInterval) { // check is performed every deadManInterval ms
previousMillis = currentMillis;
if(deadManTimeout) {
myservoX.write(90); // stop motors
myservoY.write(90);
}
deadManTimeout = true;
}
}
void motorsControl() {
myservoX.write(servo_L/2);
myservoY.write(servo_R/2);
}
void sendBlueToothData() {
static long previousMillis = 0;
long currentMillis = millis();
if(currentMillis - previousMillis > sendInterval) { // send data back to smartphone
previousMillis = currentMillis;
// Data frame transmitted back from Arduino to Android device:
// < 0X02 Buttons state 0X01 DataField#1 0x04 DataField#2 0x05 DataField#3 0x03 >
// < 0X02 "01011" 0X01 "120.00" 0x04 "-4500" 0x05 "Motor enabled" 0x03 > // example
Serial.print((char)STX); // Start of Transmission
Serial.print(getButtonStatusString()); Serial.print((char)0x1); // buttons status feedback
Serial.print(GetdataInt1()); Serial.print((char)0x4); // datafield #1
Serial.print(GetdataFloat2()); Serial.print((char)0x5); // datafield #2
Serial.print(displayStatus); // datafield #3
Serial.print((char)ETX); // End of Transmission
}
}
String getButtonStatusString() {
String bStatus = "";
for(int i=0; i<6; i++) {
if(buttonStatus & (B100000 >>i)) bStatus += "1";
else bStatus += "0";
}
return bStatus;
}
int GetdataInt1() { // Data dummy values sent to Android device for demo purpose
static int i= -30; // Replace with your own code
i ++;
if(i >0) i = -30;
return i;
}
float GetdataFloat2() { // Data dummy values sent to Android device for demo purpose
static float i=50; // Replace with your own code
i-=.5;
if(i <-50) i = 50;
return i;
}
void getJoystickState(byte data[8]) {
int joyX = (data[1]-48)*100 + (data[2]-48)*10 + (data[3]-48); // obtain the Int from the ASCII representation
int joyY = (data[4]-48)*100 + (data[5]-48)*10 + (data[6]-48);
joyX = joyX - 200; // Offset to avoid
joyY = joyY - 200; // transmitting negative numbers
if(joyX<-100 || joyX>100 || joyY<-100 || joyY>100) return; // communication error
if(joyY >-10) { // differential steering
servo_L = (joyY - (3*joyX)/4);
servo_R = (joyY + (3*joyX)/4);
} else {
servo_L = (joyY + (3*joyX)/4);
servo_R = (joyY - (3*joyX)/4);
}
servo_L = map(servo_L, -100, 100, 0, 180); // << adjust to change motor direction
servo_R = map(servo_R, -100, 100, 0, 180);
servo_L = constrain(servo_L, 0, 180);
servo_R = constrain(servo_R, 0, 180);
}
void getButtonState(int bStatus) {
switch (bStatus) {
// ----------------- BUTTON #1 -----------------------
case 'A':
buttonStatus |= B000001; // ON
displayStatus = "LED <ON>";
digitalWrite(ledPin, HIGH);
break;
case 'B':
buttonStatus &= B111110; // OFF
displayStatus = "LED <OFF>";
digitalWrite(ledPin, LOW);
break;
// ----------------- BUTTON #2 -----------------------
case 'C':
buttonStatus |= B000010; // ON
displayStatus = "Button2 <ON>";
break;
case 'D':
buttonStatus &= B111101; // OFF
displayStatus = "Button2 <OFF>";
break;
// ----------------- BUTTON #3 -----------------------
case 'E':
buttonStatus |= B000100; // ON
displayStatus = "Button3 <ON>";
break;
case 'F':
buttonStatus &= B111011; // OFF
displayStatus = "Button3 <OFF>";
break;
// ----------------- BUTTON #4 -----------------------
case 'G':
buttonStatus |= B001000; // ON
displayStatus = "Button4 <ON>";
break;
case 'H':
buttonStatus &= B110111; // OFF
displayStatus = "Button4 <OFF>";
break;
// ----------------- BUTTON #5 -----------------------
case 'I': // configured as momentary button
displayStatus = "Button5: <pushed>";
break;
// ----------------- BUTTON #6 -----------------------
case 'K':
buttonStatus |= B100000; // ON
displayStatus = "Button6 <ON>"; // Demo text message
break;
case 'L':
buttonStatus &= B011111; // OFF
displayStatus = "Button6 <OFF>";
break;
}
// ---------------------------------------------------------------
}
I have no possibilities to test the code for the moment, but at least, it compiles
Make me a favor, edit your last post and embed your code as such
[c ode] ... code... [/c ode] (ommit the two spaces)
or use the </> button at the top left of the "Post reply" dialog
Post will be more legible