hello, still very new on C++ , on the following func moveServo() i'm try to get back a simple 0 or 1 if there is any obstacle within 20 cm.
the func work perfecly and my servo keep turning left or right and print the distance it read.
i modify the func to return an int moveServo() , but when i insert return word in the func the servo not move anymore.
look like it stop the for loop.
int moveServo(){
for (pos = 30; pos <= 160; pos += 10) {
if (readPing() >= 20 ){
servo_motor.write(pos);
int dist = readPing();
Serial.println(dist);
analisi(dist, pos);
return 0; // here the problem, if i remove retur the servo move correctly
}else {
servo_motor.write(pos);
int dist = readPing();
Serial.println(dist);
analisi(dist, pos);
return 1; // here the problem, if i remove retur the servo move correctly
}
}
for (pos = 160; pos >= 30; pos -= 10) {
if (readPing() >= 20 ){
servo_motor.write(pos);
int dist = readPing();
Serial.println(dist);
analisi(dist, pos);
return 0; // here the problem, if i remove retur the servo move correctly
}else {
servo_motor.write(pos);
int dist = readPing();
Serial.println(dist);
analisi(dist, pos);
return 0; // here the problem, if i remove retur the servo move correctly
}
}
}
Your 'if' and 'else' contain many lines that are the same. That is terrible programming. It is like saying, "If it is raining, take an umbrella. Otherwise, take an umbrella".
Getting rid of trash in your program, is the first step to making it work.
Your problem is that when you are doing those for loops the processor can't be doing anything else so any processing you want to do has to be inside the loop...like your Serial.prints were, and that's why they worked. Once you do a return() the moveServo function is completed.
If you're thinking that you can be doing something else while the servo is still moving and the sensor is still reading then you will need to change the overall logic of your program.
give an overview about what you want to do in the end.
We are talking about details. Please give an overview over your whole project.
in mimimum 70% of all cases knowing the whole thing offers completely different and much better working solutions.
This is like
Newbee: "I want to do better cutting please help me sharpening. "
Expert: Sure I can help you. What kind of cutting-tool are you using?
Newbee: With a scissor.
Expert: OK take this sharpening tool
Newbee: Yea works great Next question How can I make it cut faster I need to finish faster.
expert: Motorised scissors.
newbee Yea works great though still not fast enough.
expert: Ok can you give an overview about what you are cutting?
newbee: the green of a football-arena.
expert: Oha! take a big mowing tractor with a seven boom spindel-mower and GPS-steering
In the beginning the newbee alsways just told details.
The expert was assuming the newbee knows that his basic approach is well suited.
which turns out to be very bad suited
that's the reason why it is always a good idea to give an overview and to explain what shall happen in the end.
Yes, the code I posted in reply #6 assumes that you want to complete all the sweeps before returning a value. That may be right or wrong, I don't know, I just took a cue from what you're already doing. Hence it would be a good idea to give a higher level overview of the requirements.