Hi everyone.
I have the program code to be used to set the PID controller value via bluetooth.
My problem is that I can not set these values through the terminal in my phone.
The phone connects to the HC-05 module, but I can not send a data frame to set kp kd kps and kis kp.
The HC-05 module connects and receives data from the telephone.
I am sending the program code. Do you know what commands should be sent to save data in the eeprom and read them?
I send a query in the form: 12kp1ki2kd3kps4kis5 is an example. Am I doing it right?
I have eeprom memory functions in the program. I just do not know how to issue commands in the terminal
setup bluetooth
//BLUETOOTH joystick android
SoftwareSerial BT1(10, 11); // RX | TX
#define STX 0x02 // ASCII START OF TEKST
#define ETX 0x03 // ASCII END OF TEKST
#define START_CMD_CHAR '*' // 0x2A *
#define END_CMD_CHAR '#' // 0x23 #
#define DIV_CMD_CHAR '|' // 0x7C |
#define CMD_DIGITALWRITE 10
#define CMD_ANALOGWRITE 11
#define CMD_TEXT 12
#define CMD_READ_ARDUDROID 13
#define MAX_COMMAND 20 // max command number code. used for error checking.
#define MIN_COMMAND 10 // minimum command number code. used for error checking.
#define IN_STRING_LENGHT 40
#define MAX_ANALOGWRITE 255
#define PIN_HIGH 3
#define PIN_LOW 2
byte cmd[8] = {0, 0, 0, 0, 0, 0, 0, 0};
int joyX,joyY;
int stan;
float joyXX,joyYY;
void setup
Serial.begin(115200);
Wire.begin();
BT1.begin(57600);
while(BT1.available()) BT1.read();
bluetooth
void bluetooth()
{
if(BT1.available())
{ // dane otrzymane z aplikacji na telefonie
cmd[0] = BT1.read(); //Serial.write( cmd[0]);;return;
if(cmd[0] == STX) // jesli cmd[0] == STX START OF TEXT 0x02 HEX
{
int i=1;
while(BT1.available())
{
cmd[i] = BT1.read();
if(cmd[i]>127 || i>7) break; // max 127 (HEX 7F)
if((cmd[i]==ETX) && (i==2 || i==7)) break; // jesli cmd[i] == ETX END OF TEXT 0x03 HEX
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 >
}
int ard_command = 0;
int pin_num = 0;
int pin_value = 0;
if (cmd[0] != 42) // cmd[0] != * DEC system dziesiętny
{
return; // Jeśli brak '*' na początku polecenia
}
ard_command = BT1.parseInt(); // wyszukuje w ciągu znaków liczbę
delay(25);
pin_num = BT1.parseInt(); // odczytany pin
delay(25);
pin_value = BT1.parseInt(); // odczytana wartosc
if (ard_command == CMD_TEXT) // ard_command == 12
{
String s = GetLine();
if (s == "h") {
BT1.print("kp,ki,kd,kps,kis");
stan=0;
return;
}
if (s=="s") {
stan=0;
zapis_zmiennych ();
return;
}
if (s=="kp") {
stan=1;
BT1.print(" Kp:");BT1.print(Kp);
return;
}
if (s=="ki") {
stan=2;
BT1.print(" Ki:");BT1.print(Ki);
return;
}
if (s=="kd") {
stan=3;
BT1.print(" Kd:");BT1.print(Kd);
return;
}
if (s=="kps") {
stan=4;
BT1.print(" Kps:");BT1.print(Kps);
return;
}
if (s=="kis") {
stan=5;
BT1.print(" Kis:");BT1.print(Kis);
return;
}
if (s=="kds") {
stan=6;
BT1.print(" Kds:");BT1.print(Kds);
return;
}
if (stan==1) {
Kp = string_float(s);
BT1.print(" Kp:");BT1.print(Kp);
return;
}
if (stan==2) {
Ki = string_float(s);
BT1.print(" Ki:");BT1.print(Ki);
return;
}
if (stan==3) {
Kd = string_float(s);
BT1.print(" Kd:");BT1.print(Kd);
return;
}
if (stan==4) {
Kps = string_float(s);
BT1.print(" Kps:");BT1.print(Kps);
return;
}
if (stan==5) {
Kis = string_float(s);
BT1.print(" Kis:");BT1.print(Kis);
return;
}
if (stan==6) {
Kds = string_float(s);
BT1.print(" Kds:");BT1.print(Kds);
return;
}
}
/**if (ard_command == CMD_READ_ARDUDROID)
{ BT1.print(" Analog 0 = ");
BT1.println(analogRead(A0)); // Leemos A0
return; // Done. return to loop();
}
if (ard_command == CMD_DIGITALWRITE)
{ processDW(pin_num, pin_value);
return;
}
if (ard_command == CMD_ANALOGWRITE)
{ analogWrite( pin_num, pin_value );
// add your code here
return; // De vuelta alloop();
}**/
}
}
void processDW(int pin_num, int pin_value)
{ if (pin_value == PIN_LOW)
pin_value = LOW;
else if (pin_value == PIN_HIGH)
pin_value = HIGH;
else
return; // Błedna wartosc pinu.
digitalWrite( pin_num, pin_value);
return;
}
String GetLine()
{ String S = "" ;
if (BT1.available())
{ char c = BT1.read();
delay(25) ;
c = BT1.read();
while ( c != END_CMD_CHAR)
{ S = S + c ;
delay(25) ;
c = BT1.read();
}
return( S ) ;
}
}