If been working on this project for my quadcopter for some time now and I just can't get the script make it work the way I need it to work. I want to controle 2 servo's using the ultrasonic sensor.
And when I keep my hand infront of the sensor the servo's do move.....but they keep moving. They don't stop at 0 degrees and 60 degrees. That's most likely because of my lack of programming skills....so here's my question:
After the code tells the servo to go to a position there is a line of code "delay(1000)" what means to wait for 1 second.
But wait for what??
It means do nothing for one second. In this context it may simply be there to ensure that the servos complete whatever move they were asked to do before more instructions are given.
In quadcopter code though, use of delay makes little sense unless it's in the startup code. Ignoring your sensors while actually flying sounds like a great recipe for a crash.
Oh...sorry...it's NOT for controll of the quad itself!
It's for the landing gear! I want to make an autonomous landing gear. If the sensor "finds" the ground and it's less the 50cm the gear must go down (and stay down!). On take off the distance gets bitter and when it's >= 50cm the gear must retract/go up.
So if I understand correctly: the "delay" putts the code/loop on "hold" for the duration of the given number? And then goes on with the rest of the code/loop?
Yes. The actual code of delay simply waits for millis() to advance the relevant number
of milliseconds - millis() output depends on the timer0 interrupt routine advancing the
millisecond count.
You would need to study BlinkWithoutDelay example sketch to see how to achieve
"delays" with using delay() - thus allowing the rest of the system to do stuff while
waiting.
Okay....I'll do that. but can I also leave the delay part out of the script/program?
Or is it a required part of the script and must I set it to delay(1)?
I ask this because I can't have it that the landing gear must wait untill the gear is up while...for some reason...the quad is going down again. If the distance gets under 50cm the gear must go down again....even if the servo did not get to the postion as mentioned in the program.
I don't see any reason for a delay in this situation.
Read the height. If it is above the threshold raise the landing gear, otherwise lower the landing gear. Repeat as fast as you like.
It would probably be sensible to apply a rolling average to the height reading before you apply the threshold, so that the landing gear doesn't keep glitching based on transient height readings.
I think I know what your saying: use 5 measurements and use the average outcome as value so the gear doesn't come down when I'm flying over treetops?
I was thinking of implementing something like that allready....but Let me get it working first! Hahahahahaha. XD
One step at the time. Hahahaha.