I have another problem with my code. I have a car controlled with bluetooth that i want to drive it manually and auto, auto runs on 4 ultrasonic sensors. The problem that i have its i cannot exit the while loop, don't know how to make it work, to return to the SWITCH commands. I need the loop to take actions based on the ultrasonic sensors.
I was thinking using if command in while but sometimes it works, sometimes it doesn't work. What can i do? Basically I want to return from the loop at serial read by sending a data from bluetooth, lets say number 5 . Thanks
void loop() {
delay(60);
if (Serial.available() > 0) {
comanda = Serial.read();
switch (comanda) {
case 'F':
forward();
perie();
break;
case 'B':
back();
perie();
break;
case 'L':
stanga();
perie();
break;
case 'R':
dreapta();
perie();
break;
case 'A':
while ('A')
{
if (Serial.available() == 5) {
break;
Stop();
}
else {
aut();
}
}
}
}
}
to exit a loop (while, for, case etc.) use break;Why is the while loop in there? would if work better instead? Why start a new thread for each problem? Just keep the dialog going with those who are already familiar with your project.
Yes, i know i need to use break, but how to implement it so when i go in auto mode i can exit it using data send through arduino from my mobile device?
How could I use IF command so the aut(); commands enters in a loop? Can you give a example? thanks
Aylee:
Yes, i know i need to use break, but how to implement it so when i go in auto mode i can exit it using data send through arduino from my mobile device?
How could I use IF command so the aut(); commands enters in a loop? Can you give a example? thanks
if(btBreakCommand) break; Are you able to receive bluetooth commands while inside the while loop?
I'm going to weigh in here on your switch case.... I just said earlier today in another post in another thread on this forum that I personally never take an input into the switch for a switch-case. I think you would be better served to have your commands in a if-else block of code. Leave switch-case arguments specifically for state machines.
Here's a rough example of what I mean by separating inputs from cases...
char btCommand;
enum RobotState : char {RobotStopped = 0, RobotForward = 1, RobotBack = 2, RobotLeft = 3, RobotRight = 4, RobotAutoPilot = 5};
RobotState robotSwitch = RobotStopped;
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
decodeBluetooth();
stateMachine();
}
void decodeBluetooth() {
//do whatever it takes to get the ASCII commands from bluetooth and validate them as a complete and valid protocol....
// update btCommand when valid command is received.
}
void stateMachine() {
switch (robotSwitch) {
case RobotStopped: {
if (btCommand == 'F') {
//whatever you need to do "immediately"
robotSwitch = RobotForward;
}
else if (btCommand == 'B') {
}
else if (btCommand == 'L') {
}
else if (btCommand == 'R') {
}
else if (btCommand == 'A') {
}
else if (btCommand == 'S');//already stopped. do nothing.
break;
}
case RobotForward: {
break;
}
case RobotBack: {
break;
}
case RobotLeft: {
break;
}
case RobotRight: {
break;
}
case RobotAutoPilot: {
break;
}
}
}
Ugh... it's kind hard for me to understand, it's too much for my mind xD. I will post all the code of my program, maybe someone will figure it out what could I do.
I have a Stop button too on my mobile device, so when i press it i want the car to stop completely and do nothing.
For controlling the engines i use l293d shield if it helps
It's hard to understand because english it's not my native language and it's hard to translate
L.E. I think i've understanded what you did there, separate input from cases. I will try tommorow this. And if i go to manual mode, it will stop from looping? if using separate input from cases?
Aylee:
Ugh... it's kind hard for me to understand, it's too much for my mind xD.
If you send 'L' from your bluetooth, do you want to do stanga() and prairie() only once? Or, do you want to do them until 'R' or 'A' is received? if only once, R, L, F and B are not really cases, and should be handled by if statements and A is the default (and only) state. If you want to do them until the next command is received, then you need to remain in the case, but you must do all your calls, receive serial included, within that case. Your headache is that you do not understand the difference between a conditional branch and a state machine.
Perehama:
If you send 'L' from your bluetooth, do you want to do stanga() and prairie() only once? Or, do you want to do them until 'R' or 'A' is received? if only once, R, L, F and B are not really cases, and should be handled by if statements and A is the default (and only) state. If you want to do them until the next command is received, then you need to remain in the case, but you must do all your calls, receive serial included, within that case. Your headache is that you do not understand the difference between a conditional branch and a state machine.
No, I want stanga() and perie() to run continously till another command it's received. I want to do the same thing with 'A' command if possible.
All commands I want them to run continously till another command is received through bluetooth.
Perehama:
Newsflash! case IS a while statement.... you have to figure out how to exit case R,L,B,F and A, and it's all going to be the same way, and you can do different things in each. But, you can't receive serial data unless you do it from within the case. Hence, you should not base your switch on data.
How it comes it only does a thing from the void aut(); if it's a while statement? I want the aut(); to loop through the IF's all the time, to get informations from sensors.
Aylee:
No, I want stanga() and perie() to run continously till another command it's received. I want to do the same thing with 'A' command if possible.
Newsflash! case IS a while statement.... you have to figure out how to exit case R,L,B,F and A, and it's all going to be the same way, and you can do different things in each. But, you can't receive serial data unless you do it from within the case. Hence, you should not base your switch on data.
Perehama:
Ok, give me a bit of time to fix it. i will work on it and post maybe tomorrow.
Oh, thank you, hope you can do something. I need to do this as my final exam and it kinda gives me headaches, today i worked on it 10 hours. First i had problems with the ultrasonic sensors, now this ...
Perehama:
Here's what I think you are trying to do... Not sure why the delay and stop commands are in your loop, but I know I've gotten you close...
Nice, I will try it when i get home from work.
The delay was for the ultrasonic sensors, they need at least 60ms to have a good measurment.
Hope it works. Thank you very much for your time and effort! I really, really apreciate it! And thanks for the goolge translate ^^
L.E. Whoaaaaaaa, it works like a charm, THANK YOU SO MUCH! This community is awesome. If it weren't you I probably still looking how to exit the while loop xD. thank you thank you!
Aylee:
The delay was for the ultrasonic sensors, they need at least 60ms to have a good measurment.
I know you've probably put this all behind you now, but for future reference, move the non-blocking delay code into the aut() function for the sensors, and you can use the same delay code where it's at for a speed adjustment.