Hello,
Me and my team are working on a bipedal robot, and we are in the middle of coding the thing.
it's gonna take forever but this bug we've been stuck with for a week.
Here's the code:
#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;
servopos = BTSerial.readString() ;
if(BTSerial.available()==0){
}
if(cmd == "front" || cmd == "frontfront" ) {
while(staus==2 ){
Serial.println(cmd);
for (pos = 0; pos <= 35; pos += 1) {
rightleg.write(pos);
leftleg.write(pos);
delay(15);
}
for (pos = 0; pos >= 35; pos -= 1) {
rightleg.write(pos);
leftleg.write(pos);
delay(15);
}
if (cmd == "stop" || cmd == "stopstop"){
status = 0;
delay(6900);
}
}
if (cmd == "stop" || cmd == "stopstop"){
status = 0;
delay(6900);
}
Serial.println(cmd);
}
the while() part starts properly when it's supposed to. i.e when it recieves "front" in the bluetooth terminal. but no matter what, the servos don't stop when "stop" is sent. Also there is
while(cmd =="stop" || cmd == "stopstop"){} there because the app we use sometimes sends the text twice
we've debugged enough to know this much:
the serial terminal revieves all commands before the command "front" is given
after the front is given, the serial port keeps saying that the value of "cmd" is always "front"
it never shows any other text (in this case "stop" needs to be recognised) other than front even when we are spamming stop button on the other side.
I'll keep this forum updated and you can clarify why mu code is like this.
What changes the status to 4 or 10 or 0 or 4.569?
Did you try to print out the cmd?
Serial.println(cmd); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<let's you see the deal.
if (cmd == "stop" || cmd == "stopstop"){
status
while (staus == 2 )
This while loop will start when staus equals 2 and will continue until staus does not equal 2
Where in the while loop is the value of staus changed ? In fact, where in the sketch is staus ever declared and defined ? Did you mean status by any chance ?
I note too that servopos is not declared in the sketch so the sketch as posted will not compile anyway
bheeduino:
String cmd;
How/when is the variable filled? Where in your program does something read something and put the something into the variable cmd?
I stopped looking after finding 2 undeclared variables so there may be more
gcjr
November 23, 2022, 12:34pm
6
shouldn't the BTSerial.read() be inside the if (BRSerial.available()) condition when available?
where does "cmd" get set?
why test for "front" and "frontfront"?
looks like you somehow read the "front" cmd and the while loop repeatedly drives the legs until the "stop" cmd is read and resets status.
but again, where do you read "cmd" and with this construction you need to read it inside loop
bheeduino:
int status = 2;
why is "status" initialized to 2 instead of 0 and wait for the "front" cmd
please study the following. i think it is closer to what you intended to do. try to understand how the code works and try changing it for your needs.
an obvious shortcoming is the for loops for driving the legs need to complete before another cmd can be read
#define MyHW
#ifdef MyHW
struct SoftwareSerial {
SoftwareSerial (int tx, int rx) { }
int available (void) { return Serial.available (); }
void begin (int bps) { Serial.begin (bps); }
String readString (void) { return Serial.readString (); }
};
struct Servo {
void attach (int pin) { }
void write (int pos) { Serial.println (pos); }
};
#else
#include <SoftwareSerial.h>
#include <Servo.h>
#endif
Servo rightleg;
Servo leftleg;
int bluetoothTx = 5;
int bluetoothRx = 6;
int pos = 0;
SoftwareSerial BTSerial(bluetoothTx, bluetoothRx);
int status = 0;
String cmd;
void setup()
{
rightleg.attach(9);
leftleg.attach(10);
Serial.begin(9600);
BTSerial.begin(9600);
}
void loop()
{
if (BTSerial.available() != 0) {
cmd = BTSerial.readString();
Serial.println(cmd);
}
if (cmd == "stop" || cmd == "stopstop") {
status = 0;
}
if(cmd == "front") {
status = 2;
}
Serial.print (" status: ");
Serial.println (status);
if (status) {
for (pos = 0; pos <= 35; pos += 1) {
rightleg.write(pos);
leftleg.write(pos);
delay(15);
}
for (pos = 0; pos >= 35; pos -= 1) {
rightleg.write(pos);
leftleg.write(pos);
delay(15);
}
}
}
I had to make changes before getting the code on the internet.
"servopos" = "cmd"
i thought it would be confusing so i changed all but one.
Here's the refined code that is identical to the one i used:
#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(staus==2 ){
Serial.println(cmd);
for (pos = 0; pos <= 35; pos += 1) {
rightleg.write(pos);
leftleg.write(pos);
delay(15);
}
for (pos = 0; pos >= 35; pos -= 1) {
rightleg.write(pos);
leftleg.write(pos);
delay(15);
}
if (cmd == "stop" || cmd == "stopstop"){
status = 0;
delay(6900);
}
}
if (cmd == "stop" || cmd == "stopstop"){
status = 0;
delay(6900);
}
Serial.println(cmd);
}
And im sure the "status" gets changed in
if (cmd == "stop" || cmd == "stopstop"){
status = 0;
delay(6900);
}
i know it can be. I guess i can change it. It is what it is
Your so called "refined" code
needs additional error-correction
#include <SoftwareSerial.h> // had to add this line of code
had to correct this line here
//while (staus == 2 ) { // I guess you mean status ot "staus"
while (status == 2 ) { // I guess you mean status ot "staus"
imagine unlocking your smartphone by typing a PIN
You tap on "3" on the display and you get a "4"
imagine
You call your best friend and your smartphone connects you to the police
Would you ever buy a smartphone with such silly malfunctions?
surely not!
You are posting a code-version that does not even compile
you don't care about proper indention
and others shall analyse a code-version that does not even compile ??!!
and is hard to read because if improper indention??!
Boy the next thing to do is stopping beeing so lazy .
Are you sure of that because you can see it changing in your debugging or are you sure because you think that's what the code does?
There's nothing inside the "while(status == 2)" loop to change status so it looks like you'll never exit that loop?
it was there in the post but it turned out i put that line with the " ``` " part of the thing
That i definitely should try to at least
The last part of this:
if(cmd == "front" || cmd == "frontfront" ) {
while(staus==2 ){
Serial.println(cmd);
for (pos = 0; pos <= 35; pos += 1) {
rightleg.write(pos);
leftleg.write(pos);
delay(15);
}
for (pos = 0; pos >= 35; pos -= 1) {
rightleg.write(pos);
leftleg.write(pos);
delay(15);
}
if (cmd == "stop" || cmd == "stopstop"){
status = 0;
delay(6900);
}
shouldn't it ?
Well, it was. until i decided to make changes to nonsense variable names as of to upload it to the "humble" internet
and no it wasn't anything inappropriate it was just really confusing
bheeduino:
shouldn't it ?
It should. But does it? Don't just assume it works.
Okay. I read the unedited part of that and my brain refused to work.
All i know is that the serial monitor spits out "front" every second but not stop, so it never recieves "stop" in the while loop
Several posts have already told you this but for some reason you are not getting it.
bheeduino:
String cmd;
When you do that what does cmd =? Does it equal "I am the thing" or does it equal ""? Note ''' is equal to null. Nothing. Nadda. Got it?
If you did String cmd= "QWERTYUIOP"; what does cmd equal? Does it equal nothing or does it equal "QWERTYUIOP"?
If your code. Yup your code. Does not MAKE cmd equal anything then how can your code equal "stop" if you do not make a cmd="stop";. To have cmd equal anything a value MUST be placed into cmd.
Sorry - I got a bit confused by your indentation and wasn't sure if I was explaining it right.
Here's your code with correct indentation and easier to read:
if(cmd == "front" || cmd == "frontfront" ) {
while(staus==2 ){
Serial.println(cmd);
for (pos = 0; pos <= 35; pos += 1) {
rightleg.write(pos);
leftleg.write(pos);
delay(15);
}
for (pos = 0; pos >= 35; pos -= 1) {
rightleg.write(pos);
leftleg.write(pos);
delay(15);
}
if (cmd == "stop" || cmd == "stopstop"){
status = 0;
delay(6900);
}
}
So if the cmd is front you enter the loop.
You loop while status is 2.
Status only gets changed if cmd is stop.
But you only entered the loop because cmd was front and it doesnt change inside the loop so it'll never be stop. So status will never change.
cmd will always be "front" and status will always be 2, there's no logical path for these to change once inside the loop.
i don't wanna be rude but what does the very next line, i.e :
cmd = BTSerial.readString() ;
do?
it does a fine job. until the while loop starts.
before sending the command "front" the serial monitor spits out all other values just right.
Not rude at all. I was refreing to post#12. The latest code you posted
i am using an external bluetooth device to send "front" "stop" "right" or "left"