Ok, found a mistake. Forgot to set onetime to 1 in the switch case.
but the double flashing at the beginning remains
Click counter
-
- Posts: 103
- http://meble-kuchenne.info.pl
- Joined: Mon Dec 07, 2020 12:47 pm
- Been thanked: 1 time
Re: Click counter
By the way, OUT1 and OUT2 are also LEDs, I think I forgot to mention that.
So here is the working version
So here is the working version
- Attachments
-
- Testclick.fcfx
- (36.2 KiB) Downloaded 89 times
-
- Valued Contributor
- Posts: 1607
- Joined: Thu Dec 03, 2020 10:57 am
- Has thanked: 357 times
- Been thanked: 565 times
Re: Click counter
Hi
If you run and get no input within your count of 20,000 you enter an endless loop. You may want to try and prevent that.
Looks like you would benefit from using timers rather than just counting up based on the run time it takes to complete a loop.
Button presses etc typically are counted in milliseconds (hundreds) so your chip can easily use timers to capture pulse lengths with accuracy, eliminating "bounce". For example lets say with bounce you get some pulses coming in for anything up to 10mS before settling, with a normal button press being between 20 and 40mS in length. Same for a Switch but this time the length is 80 to 120mS. These are just example values and in reality you would need to experiment.
You could (for example) set up Interrupt On Change. Interrupt on High will start a timer, Low will stop.
When interupt occurs (say Button 1 going high) you start timer (tick = tick +1, with time interval of say 1mS)
Enable IOC on High.
When Tick = 12 you test button. If low stop timer and go back to beginning.
If still high then carry on, and enable IOC on low
When button goes Low IOC then stops timer
Tick equals the number of mS that elapsed. If up to 40 then it's a button, if above 80 then its a switch so to speak.
Obviously the times above are not realistic but it's just to give an example of one way to measure pulse duration.
Regards
If you run and get no input within your count of 20,000 you enter an endless loop. You may want to try and prevent that.
Looks like you would benefit from using timers rather than just counting up based on the run time it takes to complete a loop.
Button presses etc typically are counted in milliseconds (hundreds) so your chip can easily use timers to capture pulse lengths with accuracy, eliminating "bounce". For example lets say with bounce you get some pulses coming in for anything up to 10mS before settling, with a normal button press being between 20 and 40mS in length. Same for a Switch but this time the length is 80 to 120mS. These are just example values and in reality you would need to experiment.
You could (for example) set up Interrupt On Change. Interrupt on High will start a timer, Low will stop.
When interupt occurs (say Button 1 going high) you start timer (tick = tick +1, with time interval of say 1mS)
Enable IOC on High.
When Tick = 12 you test button. If low stop timer and go back to beginning.
If still high then carry on, and enable IOC on low
When button goes Low IOC then stops timer
Tick equals the number of mS that elapsed. If up to 40 then it's a button, if above 80 then its a switch so to speak.
Obviously the times above are not realistic but it's just to give an example of one way to measure pulse duration.
Regards
Re: Click counter
Hi,
I would like to work with an IOC. The only problem here is that I don't have a button or switch on a port pin but a permanent PWM pulse and I have to react to the change in pulse width with a button/switch click.
But thanks for the suggestion.
Regards
I would like to work with an IOC. The only problem here is that I don't have a button or switch on a port pin but a permanent PWM pulse and I have to react to the change in pulse width with a button/switch click.
But thanks for the suggestion.
Regards
-
- Valued Contributor
- Posts: 1607
- Joined: Thu Dec 03, 2020 10:57 am
- Has thanked: 357 times
- Been thanked: 565 times
Re: Click counter
Hi
I think IOC is the way to go using a timer.
You are receiving a signal of varying duration or duty-cycle. When the signal goes High you start a "tick" stopping when going low. The count is then your duration. By measuring this duration you should be able to establish what is causing the change, assuming of course whatever causes the change causes a repeatable duration change.
If you expect 100% duration though then you may need to adjust the approach.
regards
I think IOC is the way to go using a timer.
You are receiving a signal of varying duration or duty-cycle. When the signal goes High you start a "tick" stopping when going low. The count is then your duration. By measuring this duration you should be able to establish what is causing the change, assuming of course whatever causes the change causes a repeatable duration change.
If you expect 100% duration though then you may need to adjust the approach.
regards