How do I get out of the WaituntilHigh macro?

For general Flowcode discussion that does not belong in the other sections.
ChrisT66
Posts: 103
http://meble-kuchenne.info.pl
Joined: Mon Dec 07, 2020 12:47 pm
Been thanked: 1 time

How do I get out of the WaituntilHigh macro?

Post 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

mnfisher
Valued Contributor
Posts: 1628
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 142 times
Been thanked: 761 times

Re: How do I get out of the WaituntilHigh macro?

Post 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

chipfryer27
Valued Contributor
Posts: 1685
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 374 times
Been thanked: 583 times

Re: How do I get out of the WaituntilHigh macro?

Post 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

ChrisT66
Posts: 103
Joined: Mon Dec 07, 2020 12:47 pm
Been thanked: 1 time

Re: How do I get out of the WaituntilHigh macro?

Post 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?
Attachments
Test.fcfx
(46.15 KiB) Downloaded 180 times

mnfisher
Valued Contributor
Posts: 1628
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 142 times
Been thanked: 761 times

Re: How do I get out of the WaituntilHigh macro?

Post 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

mnfisher
Valued Contributor
Posts: 1628
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 142 times
Been thanked: 761 times

Re: How do I get out of the WaituntilHigh macro?

Post by mnfisher »

Something like:
LEDFlash.fcfx
(14.99 KiB) Downloaded 218 times
Which works in simulation :-)

Martin

ChrisT66
Posts: 103
Joined: Mon Dec 07, 2020 12:47 pm
Been thanked: 1 time

Re: How do I get out of the WaituntilHigh macro?

Post 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

mnfisher
Valued Contributor
Posts: 1628
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 142 times
Been thanked: 761 times

Re: How do I get out of the WaituntilHigh macro?

Post 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

mnfisher
Valued Contributor
Posts: 1628
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 142 times
Been thanked: 761 times

Re: How do I get out of the WaituntilHigh macro?

Post 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
Attachments
TimeSignals.fcfx
(17.18 KiB) Downloaded 152 times

ChrisT66
Posts: 103
Joined: Mon Dec 07, 2020 12:47 pm
Been thanked: 1 time

Re: How do I get out of the WaituntilHigh macro?

Post 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...

Post Reply