Using A Nextion to Drive an Interrupt

Hi

I am completely new to both Nextion and Arduino so please be kind

I want to have a button on the nextion display that triggers an interrupt to stop a piece of code from functioning - is this possible and some pointers on how to would be hugely appreciated

Can the Nextion change the state of say pin 2 to drive a change triggered interrupt ?

Thanks

An interrupt does just that! It interrupts the instruction flow and when the interrupt code is complete the instruction flow is begun again at the exact point it was interrupted. That is not what you are looking for. You need to code for the button push where you need to stop your code.

What's the best way to achieve that - I thought the interrupt was the only thing to stop code mid flight - still learning :slight_smile:

I think I wrote the only way is to add code to test for the switch and return from the function if the switch is on.

Unfortunately when you are learning and you have to look everything up its not that simple to understand

Try this: Using Nextion displays with Arduino

However, it is quite clear from your question that first you need to learn about writing non-blocking, multi tasking code, so read these first:

Using millis for timing
Demonstration for several things at the same time
Finite state machine tutorial

All the example code in my Nextion tutorial is non-blocking and multi tasking so you can also learn from that.

Edit:
I just found your other topic, I note it uses delay(), that has to go, see the tutorials above. I also note you are using the official Nextion library, I have yet to encounter anyone having much success with that. Finally I note that you have an awful lot of buttons and things, far too many for someone who is still learning the basics, it just looks way to complicated.

Thanks Perry - I'll go through what you have suggested - my project will control some nema motors on a millling machine - there is a lot to do but I'll get there bit by bit

Thanks for taking the time to respond

1 Like

Just been working on millis() and I can definately see the benefit of apparent concurrent operations and how you could check an input to stop code BUT

My delay is in microseconds not milliseconds so I am factor of 1000 out ?

Can millis be converted in some way to work ?

How on earth would we have any idea? :grimacing:

Do you know what you entirely forgot to do? :roll_eyes:

Hint. :+1:

From your other topic:

  delay(1000); // One second delay

That's an awful lot of microseconds...

You also have blocking loops like this:

  for(int x = 0; x < 800; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(500); 
  }

I don't know much about driving motors but there are others here who do, hopefully one of them can advise you how to eliminate loops like that.

Edit:
As commented I don't know much about driving motors, but if I did want to know I'd start by reading the posts in Motors, Mechanics, Power and CNC - Arduino Forum for ideas. If sub-millisecond pulses and accurate timing are required I'd read the data sheet for the processor, particularly the section on counters and timers, and work out how to use the timers to generate interrupts at the required interval to generate the pulses to the motor.

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