ISR for rotary encoder

Hi All,
This is my first post for assistance, I am self taught. This problem will be easy for mainstream so really hoping you can help me please. BTW I have spent hours on it literally before asking.

Purpose: Microphone monitors room noise, if noise threshold is exceeded a relay is actuated. Use case is dog bark or intruder making sound etc. Relay turns on light etc instead of motion.

Objective is to alter the noise threshold using a rotary encoder. I have managed to achieve this by placing the //encoder //end encoder content within the loop function, but I am told it is better coding practice to place it in an ISR outside of the loop with a setup attach... command .
I have done a succesfull ISR example sketch, but when I apply what I have learnt to my sketch , it does not work. I have tested the encoder for left, right and sw and it works fine. I spent hours trying to understand why it wont work and realise the code I am adding to the ISR is beyond my skill, (suspect whats allowed whats not).

The rotary code I have used is not essential, if there is more efficient code thats fine, I am all ears

To the user, the encoder is basically adjusting the mic sensitivity, (but we know its not)

Second point; I also dont fully understand how I mapped the encoder to the thresholdLevel, I am not sure what command told it to adjust it, it just worked, I'd love to understand why, rather than accept it.

"NoiseActuatedRelay.ino" is file I am trying to fix

Thanks Tim

Why not use the Arduino Encoder library? It is tried and tested, no need to roll your own.

Start by reading the documentation, wiring it correctly and running the examples.

For help with your code, please read and follow the instructions in the "How to get the best out of this forum" post, linked at the head of every forum category.

1 Like

I have used an ISR for this but all it did was set a flag that was checked in the main loop several times. If it was set the function cleared the flag and processed the rotation.

Thank you, I dont recall 'rolling my own' I suspect the code I have used has me doing this, based on the RotaryEncoder.h library.
I understand your advice is to use the encoder.h libray and read up on its doco for usage. Will do.
Ill go back to the "how to get the best out of...' as you advise.
Thanks
Tim

Are you sharing your struggles or did you play with my code and are providing feedback? Thx TIm

Whatever you are doing, it is not using the library that I recommended, which has been carefully optimized for Arduinos and exhaustively tested.

Did you write any of the code on the Github site linked in the first post? I don't know what to make of this comment:

"NoiseActuatedRelay.ino" is file I am trying to fix

This sort of confusion is what the forum guidelines, if followed, are supposed to reduce.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.