I am building a quad-copter, the problem is of stability and for better stability lesser sampling time is required. For lesser sampling time I need to have faster loop() function. But the Pulsein() command takes very large time which in turn expands the sampling time. Is there any method to read the rc channels in background and read them from an integer.
|
The keyword you are looking for here is interrupt. What is a pulse but a signal that goes from low to high followed by going from high to low, with some period of time between them? Instead of sitting there waiting for the pulse to arrive then measuring how long it is, which is essentially what If the signal has risen from low to high then you record the time it happened. If the signal has fallen from high to low then you look what the time is, and subtract the time you remembered from when the signal rose. That difference is then the width of the pulse, which you can store in an integer to read in your main loop. If you have a number of signals you want to monitor you might want to look into using the pin change interrupt which allows you to use any pin as an interrupt source. |
|||||||||||||||||||||
|
You may find my answer to another question – Read RC receiver channels using Interrupt instead of PulseIn – useful. It includes the sketch shown below, which is intended for background timing of four RC channels attached to pins A0-A3. See the other answer for further comments about the code, example output, etc.
|
|||
|