I am building a solar tracking system. It reads 4 digital sensors (endstops) and 4 analog sensors, controls 6 relays according to these readings and prints data onto an OLED. So nothing very complex. Would I want to use interrupts for the endstops or is that overly complicated for what I’m doing? Using ESP32 and Micropython.
You are ok about using interrupts with endstops.
However, keep in mind that the handler interrupt function should be preferably used to change the value of a variable and don’t do complex things inside (avoid print statements, for example).
Then, in the loop you should handle what happens when the value of the variable changes (for example, activate a relay).
I hope this helps.
Thanks. I am not sure I understand what is the difference between an interrupt event changing a flag and then have the reaction to the flag value happen somewhere in the loop, vs. immediately reacting to the result of polling within the loop. In both cases the reaction to the endstop being pressed happens when the program reaches the relevant line in the loop. Wouldn’t it make more sense to have the reaction to the endstop being pressed (aka stopping the motor movement) in the interrupt routine itself? Sorry, I never could get my head around interrupts.
You can stop the motor in the interrupt handling routine if it’s an action that doesn’t block the code. For example, if you add delays in the interrupt handling routine, or if your task has any delays, it can crash the program.
You can experiment stopping the motor on the interrupt handling routine and see what you get – if it behaves as expected, you can use it that way.
One more question about this: Having 2 motors with 2 limit switches per motor, I created a motor class with a move function, that will move the motor depending on the value read on the ADC inputs. How do I actually handle a situation where I have more than 1 interrupt pin – 4 interrupt pins in this case? Do I need several functions? Or just several irq objects? Would it be easier if I used a different move function for each movement direction? Problem is I do not want to stop the movement completely, just the movement in the direction of the associated limit switch for the respective motor instance.
If you have 4 interrupt pins and all should trigger the same function, I think you just need one function and several irq objects (but I haven’t tried it).
It depends on how your move() function is created and if it accepts arguments or not. I think it may be a good idea to have several functions to run in different directions, but it really depends on how your project is structured.