Thank you so much @SteveJG Your tips are so useful and I am just using and testing them. Honestly, I will use the entire course!
At this moment I would like to have concrete help if it is possible. I used the 4° example in Bluetooth HC-06. Arduino. Send. Receive. Send text file. Multitouch. Image
I am sending Data from APP.
If data is 1 then the servo will move continuously based on the threshold of the imu.
If data is 2 then the servo will be turned off.
If data is 3 then I would like to let Arduino understand that it has to expect, after "3", a string with the format #,#,#*.
The problem is that this feature doesn't work and fix with the others. Those 3 numbers(#,#,#) are respectively P,I,D.
Can you help me with the structure of the code, please?
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(7,8);
#include <Arduino_LSM6DS3.h>
#include <DynamixelShield.h>
const uint8_t DXL_ID = 1;
const float DXL_PROTOCOL_VERSION = 2.0;
DynamixelShield dxl;
//This namespace is required to use Control table item names
using namespace ControlTableItem;
#define DEBUG_SERIAL Serial
char caracter;
String palabra;
String P;
String I;
String D;
int ind1;
int ind2;
int ind3;
int PID_state=0;
int walk_state=0;
int read_acc=0;
float Object_position_X = 170;
float Object_position_Y = 60;
float Athr = 0.22;
float Bthr = -0.1;
void setup(){
DEBUG_SERIAL.begin(57600);
delay(2000);
BTSerial.begin(57600);
delay(2000);
IMU.begin();
delay(2000);
dxl.begin(57600);
// Set Port Protocol Version. This has to match with DYNAMIXEL protocol version.
dxl.setPortProtocolVersion(DXL_PROTOCOL_VERSION);
// Turn off torque when configuring items in EEPROM area
dxl.torqueOff(DXL_ID);
dxl.setOperatingMode(DXL_ID, OP_POSITION);
// dxl.writeControlTableItem(CURRENT_LIMIT, DXL_ID, 15);
// dxl.writeControlTableItem(POSITION_P_GAIN, DXL_ID, 200);
// dxl.writeControlTableItem(POSITION_I_GAIN, DXL_ID, 4);
// dxl.writeControlTableItem(POSITION_D_GAIN, DXL_ID, 25);
dxl.torqueOn(DXL_ID);
DEBUG_SERIAL.print("Connect the device via bluetooth") ;
}
String Data;
void loop(){
if (BTSerial.available()>0){
Data=BTSerial.readString();
}
///WALK///
if(walk_state==0 && Data == "1"){
DEBUG_SERIAL.println(Data);
DEBUG_SERIAL.println("FUNZIONE WALKING ABILITATA");
PID_state=0;
walk_state=1;
read_acc=1;
}
if(read_acc==1){
float x,y,z;
IMU.readAcceleration(x, y, z);
DEBUG_SERIAL.print(y);
DEBUG_SERIAL.print("\t");
if (y>Athr){
dxl.setGoalPosition(DXL_ID, 170,UNIT_DEGREE);
DEBUG_SERIAL.println("stance");
}
else if(y<Bthr){
dxl.setGoalPosition(DXL_ID, 60,UNIT_DEGREE);
DEBUG_SERIAL.println("swing");
}
else {
DEBUG_SERIAL.println();
}
}
if(walk_state==1 && Data == "2"){
DEBUG_SERIAL.println(Data);
DEBUG_SERIAL.println("FUNZIONE WALKING DISABILITATA");
walk_state=0;
read_acc=0;
delay(1000);
dxl.setGoalPosition(DXL_ID, 90,UNIT_DEGREE);
}
if(PID_state==0 && Data == "3" ){
DEBUG_SERIAL.println(Data);
DEBUG_SERIAL.println("define PID");
PID_state=1;
pidvalues();
}
}
void pidvalues() {
caracter = BTSerial.read();
palabra = palabra + caracter;
DEBUG_SERIAL.println(caracter);
DEBUG_SERIAL.println(palabra);
if(caracter == '*') {
palabra = palabra.substring(0, palabra.length() - 1); // Delete last char *
Serial.println(palabra);
ind1 = palabra.indexOf(',');
P = palabra.substring(0, ind1);
ind2 = palabra.indexOf(',', ind1+1 );
I = palabra.substring(ind1+1, ind2);
ind3 = palabra.indexOf(',', ind2+1 );
D = palabra.substring(ind2+1);
Serial.print("P = ");
Serial.println(P);
Serial.print("I = ");
Serial.println(I);
Serial.print("D = ");
Serial.println(D);
Serial.println();
palabra = "";
delay(10);
}
}