• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Should I use interrupts for this application?

Q&A Forum › Category: ESP32 › Should I use interrupts for this application?
0 Vote Up Vote Down
Anna Sarah Fazendeiro asked 5 years ago

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.

Question Tags: ESP32, Interrupts
6 Answers
1 Vote Up Vote Down
Best Answer
Sara Santos Staff answered 5 years ago

Hi Anna.

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.

Regards,
Sara

0 Vote Up Vote Down
Anna Sarah Fazendeiro answered 5 years ago

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.

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi Anna.
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.
Regards,
Sara

0 Vote Up Vote Down
Anna Sarah Fazendeiro answered 5 years ago

Super, thanks!

0 Vote Up Vote Down
Anna Sarah Fazendeiro answered 5 years ago

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.

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi Annah.
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.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.