Im working on creating a robotic arm that is controlled by buttons on a app, so far i have got all of that working, but, currently i am trying to implement a save feature that uses TinyDB to store servo positions and then send them to the arduino when a button is pressed. I am able to get the data to send, however, the some values are wrong once they reach the arduino (eg instead of 90 it becomes 14). I also have the problem of some data being received from the arduino going to the wrong variables in the app.
These are the two main blocks involved with sending and receiving data, under the clock block I cannot get the data being received from the arduino to not enter the global saved positions variable.
Below will be the code that intends to receive and store the data being sent from the application.
I am hoping to get some help solving these problems as i have not been able make any progress in the past couple of days.
if (dataIn == 255){
m = 40;
Serial.println(m);
memset(servo01SP, 0, sizeof(servo01SP)); // Clear the array data to 0
memset(servo02SP, 0, sizeof(servo02SP));
memset(servo03SP, 0, sizeof(servo03SP));
memset(servo04SP, 0, sizeof(servo04SP));
memset(servo05SP, 0, sizeof(servo05SP));
memset(servo06SP, 0, sizeof(servo06SP));
while (m == 40) {
// if (Bluetooth.available() > 0) { // tell the code how many positions have been saved.
saves = Bluetooth.read();
Serial.print("Saves value: ");
Serial.println(saves);
if (saves > 0) {
hold = 1;
m = 0;
}
//Bluetooth.write(99);
index = 0;
}
if ( hold == 1) {
for (int i = 0; i <= saves; i++) {
index++;
Serial.println("New Loop");
if (i == 0) {
servo01SP[i] = start; // Set start positions.
servo02SP[i] = start;
servo03SP[i] = start;
servo04SP[i] = start;
servo05SP[i] = start;
servo06SP[i] = start;
}
while (servo01SP[i] <= 0) {
servo01SP[i] = Bluetooth.read(); // save position into the array
Serial.print("servo01");
Serial.println(servo01SP[i]);
delay(100);
}
Bluetooth.write(205);
while (servo02SP[i] <= 0) {
servo02SP[i] = Bluetooth.read();
Serial.print("servo02");
Serial.println(servo02SP[i]);
delay(100);
}
Bluetooth.write(205);
while (servo03SP[i] <= 0) {
servo03SP[i] = Bluetooth.read();
Serial.print("servo03");
Serial.println(servo03SP[i]);
delay(100);
}
Bluetooth.write(205);
while (servo04SP[i] <= 0) {
servo04SP[i] = Bluetooth.read();
Serial.print("servo04");
Serial.println(servo04SP[i]);
delay(100);
}
Bluetooth.write(205);
while (servo05SP[i] <= 0) {
servo05SP[i] = Bluetooth.read();
Serial.print("servo05");
Serial.println(servo05SP[i]);
delay(100);
}
Bluetooth.write(205);
while (servo06SP[i] <= 0) {
servo06SP[i] = Bluetooth.read();
Serial.print("servo06");
Serial.println(servo06SP[i]);
delay(100);
}
Bluetooth.write(205);
}
servo01SP[index] = start; // Set final positions.
servo02SP[index] = start;
servo03SP[index] = start;
servo04SP[index] = start;
servo05SP[index] = start;
servo06SP[index] = start;
for (int i = 0; i <= index; i++) {
Serial.println(index);
Serial.print("Servo 1: ");
Serial.println(servo01SP[i]);
Serial.print("Servo 2: ");
Serial.println(servo02SP[i]);
Serial.print("Servo 3: ");
Serial.println(servo03SP[i]);
Serial.print("Servo 4: ");
Serial.println(servo04SP[i]);
Serial.print("Servo 5: ");
Serial.println(servo05SP[i]);
Serial.print("Servo 6: ");
Serial.println(servo06SP[i]);
Serial.println("Next index");
}
Serial.println("entering runSteps");
runSteps();
}
}
currently, as it stands this is what is being printed by serial.print
Saves value: -1
Saves value: -1
Saves value: -1
Saves value: -1
Saves value: -1
Saves value: 3
New Loop
New Loop
servo0114
servo0290
servo03-1
servo03-1
servo03-1
servo03-1
servo03-1
servo03-1
servo03-1
servo03-1
servo03-1
I have been able to get all the data to print properly without this infinite loop but the values were still wrong and i think there is something ive done wrong in terms of my approach to the problem.
Happy to answer any questions or provide any more information as there is alot of other content, but, the stuff posted is what hasnt been working, everything else works fine.