Page 1 of 2
How do I get out of the WaituntilHigh macro?
Posted: Sun Oct 27, 2024 8:31 am
by ChrisT66
Hello, to measure a pulse length I query the pin status. When the status becomes 1, I measure the length. To wait for the 1, I use the WaituntilHigh macro to query a button. My problem now is that if no signal is connected, I get stuck in this routine.Is there a simple way to prevent this if no signal arrives without affecting the subsequent length measurement?
Many thanks for any tips! Chris
Re: How do I get out of the WaituntilHigh macro?
Posted: Sun Oct 27, 2024 9:24 am
by mnfisher
Can you post your code?
One option is to just read the current state of the button - usually in a loop until it returns 1 or times out.
Martin
Re: How do I get out of the WaituntilHigh macro?
Posted: Sun Oct 27, 2024 10:00 am
by chipfryer27
Hi
Following on from Martin above, and without seeing your chart, it may also be able to use an interrupt. Your main does something until your pulse triggers the interrupt.
Regards
Re: How do I get out of the WaituntilHigh macro?
Posted: Sun Oct 27, 2024 11:43 am
by ChrisT66
The method with the loop already works, thought there was a more elegant method. A timeout parameter for this macro would be nice...
I have attached the chart.
I'm reading the signal lengths of four inputs. You have already helped me with this routine

. If a signal is present, I let an LED flash, 1x for signal 1, 2x for signal 2 etc.
I'm about to go mad, because after switching on, the first available signal always flashes twice, so for signal 1 it flashes once and then again. If all signals are connected, the first one flashes twice. Even though there is no loop programmed. How does that work?
It works with the automatic start after programming, but not after switching the power off and on.
It only works if I insert a pause of approx. 1.5 seconds at the beginning. What could be the reason for this?
Re: How do I get out of the WaituntilHigh macro?
Posted: Sun Oct 27, 2024 4:24 pm
by mnfisher
It should be possible to do what you want - and there a few things you can do to make the code simpler.
1) LED - take a number of 'flashes' - and then have a loop (so flash(2) - flashes twice)
2) The code for the button presses is duplicated into 4 (nearly) identical macros - it would be nice to have one macro ButtonPressed() - returns 0 or button pressed (1..4) for example. What is to happen if multiple buttons are pressed at one time?
3) It would be possible to read all the buttons at one time (all on port C) - and FC helpfully has a component (SwitchArray) that will read them all at one time - (Set input mode to custom pins) - so you can either read all the pins at once (and check for '1' bits in the result)
Or read individual pins in a loop - and then you can wait for the pin to clear using the index (WaitForLow(index))
You have attempted to add a timeout - I would probably add a small delay to these loops (1ms maybe) - looping 20000 times might not take very long?
What do you want to do - for example you could 'flash' the LED as soon as the button is pressed, then wait until it is released. Wait for the button to be released then flash the LED?
Your code - just checks each button - then sets LED to 50% brightness. Should it do this in a loop?
Martin
Re: How do I get out of the WaituntilHigh macro?
Posted: Sun Oct 27, 2024 4:45 pm
by mnfisher
Something like:
Which works in simulation
Martin
Re: How do I get out of the WaituntilHigh macro?
Posted: Mon Oct 28, 2024 2:30 pm
by ChrisT66
Hello Martin, thank you for your quick feedback as always.
You are right, it would be better to read in all four ‘buttons’ in every program run. There are actually four signals and the routine measures the length of the signals. If no signal is connected anywhere, the length does not change and I could ignore these signals.
If signals are present, I evaluate when the pulse length changes. In order to be able to recognize whether the pulse length has changed, I read out the lengths when the program starts and remember them as a ‘normal’ reference. (The signals do not change at the start).
When this ‘initialization’ is complete, the LED lights up and the main program can start. The LED is there to recognize when the signals can be changed....
Actually, I am not interested in the exact pulse length but only in the size of the change.
Best regards Chris
Re: How do I get out of the WaituntilHigh macro?
Posted: Mon Oct 28, 2024 8:13 pm
by mnfisher
I think I see. Roughly how long are the signals?
Thinking aloud (in pseudocode):
Code: Select all
btns = ReadAll() // Assume they are bits 0..3 (might need a shift >> 2 for C2..C5)
if btns != 0
StartTimer()
loop
btnsNow = ReadAll() // Shifted if needed
for bit = 0..3 // For all four signals
if bit has changed 1 -> 0 then signal stopped // If btn was pressed in btns and now released
times[bit] = CurrentTimer()
(Clear bit in btns)
until btnsNow == 0 // All signals off
StopTimer()
Compare times[0..3] with previousTimes[0..3]
Which should be relatively straightforward to code - and allow 4 (or up to 8) signals to be timed consecutively with pretty good accuracy. If required then a timeout could be added (btn not released - timeout at time > 2s for example)
Incidentally - you mention a signal (as opposed to a button) - if they are 'digital' signals (rather than a button) we might not need to worry about bounce at all (and we can measure the times more accurately)
Martin
Re: How do I get out of the WaituntilHigh macro?
Posted: Mon Oct 28, 2024 8:40 pm
by mnfisher
Which converts to FC something like....
It gets harder to test in simulation though (need a way to set the state of the buttons before start of the program)
Looking at the source of the component - I don't think we need to shift the result of ReadAll (which Reads the individual pins one at a time a builds a return value - if this was for a digital signal then we could just read PortC) I'm not sure in the case of having the switch array in port mode - have played safe here...
Martin
Re: How do I get out of the WaituntilHigh macro?
Posted: Tue Oct 29, 2024 5:52 am
by ChrisT66
Thanks Martin, very nice compact solution for reading the signal lengths.
By the way, the signal is a PWM signal with a period of 20ms and a pulse length between 1-2ms.
In the main programme, however, I use all six PWM outputs of the processor and two of them also run via Timer0. Do they then probably influence each other? I'll have to try it out...