I'm trying to create a simulink model that counts the number of pulses of a square wave, and depending on that number, the model turns on or off the square pulse. In order to count the number of pulses, I include a Count block, whose input is the square wave. Then I compare the number of counts with a fixed value. If the number of counts is bigger, then, the input signal changes to a zero signal. In that step I included a Switch block. When I simulated the model, using Simulink R2019b, the counts signal had jumps at expected times, but with random heights.
The Count block is set up to count either edge, and count data type is double. The model is using VariableStepDiscrete solver, with auto(0.1) step. I simply removed the switch block, and built a simpler model, that just counted the number of cycles, while comparing the number of counts to reference value. Turns put that model works fine.
Why this happens? How can I switch input signals depending on the number of counts? How can I reset the number of counts depending on the cycles counted? By the way, the idea of this aplication is to use optointerrupters combined with an encoder wheel to measure the angle swept by the wheel. Thanks in advance.