OK Update time:
putting cmd = BTSerial.readString(); in the middle of the while loop, it stops now
but the servos have some type of delay after the two for() function. Can't seem to do anything with it. Here's the brand new code. It compiles this time
#include <SoftwareSerial.h>
#include <Servo.h>
Servo rightleg;
Servo leftleg;
int bluetoothTx = 5;
int bluetoothRx = 6;
int pos = 0;
SoftwareSerial BTSerial(bluetoothTx, bluetoothRx);
int status = 2;
void setup()
{
rightleg.attach(9);
leftleg.attach(10);
Serial.begin(9600);
BTSerial.begin(9600);
}
void loop(){
String cmd;
cmd = BTSerial.readString() ;
if(BTSerial.available()==0){
}
if(cmd == "front" || cmd == "frontfront" ) {
while(status==2 ){
cmd = BTSerial.readString() ;
if(BTSerial.available() != 0){
Serial.println(cmd);
}
for (pos = 0; pos <= 45; pos += 2) {
rightleg.write(pos);
leftleg.write(pos);
delay(10);
}
for (pos = 45; pos >= 0; pos -= 2) {
rightleg.write(pos);
leftleg.write(pos);
delay(10);
}
if (cmd == "stop" && status != 0 || cmd == "stopstop" && status != 0){
status = 0;
}
if (cmd == "front" && status != 2 || cmd == "frontfront" && status != 2) {
status = 2;
}
}
}
if (cmd == "front" && status != 2 || cmd == "frontfront" && status != 2) {
status = 2;
}
Serial.println(cmd);
}
until now your style of learning programming seems to be head fast forward
trying different things until it works.
This means your knowledge about the functions that you use is rather small.
If you look up the documentaion of the function readString()
You can read there that the function readString() has a timeout
calling readStr() wais for a complete string to be received until the line-ending character is received or if the timeout-waitingtime is over
Your coe will aöways react immidiately after you have learned
what non-blocking timing is and how it works
what state-machine are and how state-machines work
writing your customised serial receive-function derived from robin2 serial input basics
You can invest 100 hours into trying this, trying that
and after these 100 hour still not really knowing how to code it
or you can invest 30 hours of time to learn the things mentioned above and have a quick-responsive code
@StefanL38 Thank you for typing out that beautiful paragraph but I'm probably not going to do that because i need a lot of time to actually figure out the walking sequence and "engineer" the physical legs. I'll get back to you guys.
Thanks!