You should use a flag variable…
As I said originally, relays aren’t designed to be pulsed and generally don’t respond well when you do this.
When a relay is energised a solenoid system pulls the contacts closed. All physical switch contacts ‘bounce’ when they are made/broken, resulting in a random stream of rapid make/break events. This is electrical noise, and is particularly prevalent with mechanical relays.
You will struggle to differentiate between the electrical noise pulses, and the actual high/low pulses that you are attempting to decode.
In addition, your relay won’t perform consistently over time. Electrical noise tends to increase as the contacts become dirty, and the mechanical operation will vary as the internal solenoids wears, along with the return spring. So, what works now, with one particular relay, won’t work the same way in future or when the relay is replaced with a different one.
Maybe. Most of these level shifters are 5v to 3.3v but this one looks like it can tolerate 24v input, but it seems to want an output VCC of 12v. If it will work with a 5v output VCC then you’ll be fine.
Pete.