Hi everyone,
Is there a simple way to exit a specific for loop? if I use break it exits all of the loops, I’ve also tried “return” but I think it did the same thing.
I’ll explain more: it’s a car controlled by bluetooth. when I press ‘1’ (forward on the android), the Arduino activate a function called “forward()”, when I release the button it’s sends ‘0’ to the arduino and activate the function “stopm()”.
In the function forward() the speed goes up slowly inside of a for loop (to accelerate gradulay) and write it to the motors. And here is the problem - inside the for loop there is a condition that if button is release break the for loop, and what suppose to happen is that the plan will go back to the main loop, and go to the “else” and call the function “stopm()”
here is the relevant part of the code:
void forward(){
SpeedR=1500;
if(Serial.available() > 0){
data = Serial.read();
}
for (SpeedL=1500;SpeedL<1700;SpeedL++){
victorL.writeMicroseconds(SpeedL);
victorR.writeMicroseconds(SpeedR);
delay(15);
Serial.println(SpeedL);
SpeedR--;
if(Serial.available() > 0){
data = Serial.read();
Serial.print(data);
if(data!='1' && data!='5' && data!='8'){ /////////////////////////// STOP FORWARD
stopm();
break;
}
}
}
}
////////////////////////////////////////////////// MAIN LOOP
void loop()
{
if(Serial.available() > 0){ // Send data only when you receive data:
data = Serial.read(); //Read the incoming data and store it into variable data
Serial.print(data); //Print Value inside data in Serial monitor
Serial.print("\n");
if(data == '1') {
forward();
}
else if(data == '3') {
back();
}
else if(data == '4') {
left();
}
else if(data == '2') {
right();
}
else if(data == '5') {
}
else if(data == '6') {
}
else if(data == '7') {
}
else if(data == '8') {
}
else{
stopm();
}
}
}