Hello, I have been trying to detect zero cross with interrupts but it does not work. If I use attachInterrupt it does function, however if i use this code, it does not:
PIN_INTERRUPT is not defined any where.
I think interrupts are enabled by default in arduino or you could use interrupts() function.
I don't see where last_CH1_state is ever set to 1.
Missed a line where PIN_INTERRUPT is defined, you are right, I did not set CH1 state to 1, I have some code for reference and it doesn't set CH1 to 1. Even then I should get the println that zero cross is detected. (the void zeroCrossing() is from when I used attachInterrupt btw.)
You forgot to mark 'zero_cross_detected' as 'volatile'.
Since you are enabling only one Pin Change Interrupt and you appear to want to set 'zero_cross_detected' each time the pin changes, you can simplify your ISR to:
Does the interrupt happen if you disconnect your zero-crossing detector and manually connect Pin 2 to +5V or Gnd? That would indicate that your zero-crossing detector hardware isn't working.
You do realize that, unless you have true zero-crossing detection hardware (some kind of comparator that can actually detect a zero-crossing, rather than just a TTL logic level), using a raw input pin will NOT give you a zero-crossing interrupt? The actual threshold will be more like 1-2V.
Yes, I saw that. I haven't used pin change interrupts myself, so didn't realize there are 2 working interrupt methods for pin D2. The OP mentioned attachInterrupt works, which (I believe) uses INTO. Anyways, INT0 gets the highest priority for servicing interrupts if timing accuracy is a critical consideration ...