While loop refusues to stop

those might mess up the receiving of data. What happens when you try?

Using delay is another thing you might want to get rid of and can be worked on when you try your code from post#41.

And I'd only include one read cmd statement or you risk the cmd being overwritten.

What happens when you send 3 commands in 6900m Seconds? Do they all get registered or do none of the get registered as being received?

okay I'll eliminate it, try and update by tomorrow.

It was supposed to be a joke. I do know 1 is enough

1 Like

And, as has been mentioned before, be sure you spell status correctly, in the while comparison you have written staus :slight_smile:

OK Update time:
putting cmd = BTSerial.readString(); in the middle of the while loop, it stops now :confetti_ball::tada::tada:
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);
  }

One delay you got is 2(delay(10) * the number of loop iterations) and another delay is Serial.println(cmd).

well done posting the code as a code-section.
There is another thing that you should follow.

press Ctrl-T

for autoformatting the indentions of the code
Your code with improper indentions

After

pressing Ctrl-T

the autoformatted code looks like this

The closing brace is in the same collum as the "i" of if in the same columm as the "f" of the for

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

it is up to you to decide which way to go

best regards Stefan

@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!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.