2 ISRs to sleep and wake on button presses

Hi! I'm having some trouble with ISRs.

I am a student using an Elegoo Uno R3 which is supposed to work like the Arduino Uno.

I'm trying to make it so that the Uno goes to sleep on a button press and then wakes up when a different button is pressed. Thus far, I've found article after article about a single button press and waiting to wake up, using serial to wake it up, etc.

Basically, I need to be running the program like normal, but when "offButton" is pressed, the Uno goes to sleep indefinitely. Then, when "onButton" is pressed, the Uno wakes up and continues operating as normal (or restarts the code, whichever is easier to implement).

I have the avr/sleep.h library as that seems to be a constant factor in all the solutions I've attempted so far.

I appreciate any simple code fixes that I might be overlooking/didn't think of. Thanks!

Post the code, using code tags, after reading "How to use this forum".

To wake from sleep, your switch needs to be attached to an interrupt, either the external interrupts on pins 2 and 3, or using the pin change interrupt on just about any pin.

jremington:
Post the code, using code tags, after reading "How to use this forum".

HI, thanks for the response!

I read the "how to use this forum" before posting, but because I don't have code applicable to the question I'm posting, I don't have code to post. The ISRs are completely separate and have no effect on the rest of the code other than to start and stop it, so the rest of the code doesn't matter.

At this point, I already provided the names that I'll be using for the ISRs as "offButton" and "onButton".

I appreciate any simple code fixes that I might be overlooking/didn't think of.

I'm puzzled. You don't have any code, but are looking for "code fixes".

jremington:
I'm puzzled. You don't have any code, but are looking for "code fixes".

I'm looking for "Project Guidance" which the "How to use this forum" post said would be the correct place to ask question on approaches to coding something that you have researched but failed to find a solution for.

To make sure I'm in the right place, I'll post the forum path I used to get here:

Arduino Forum > Using Arduino > Project Guidance > 2 ISRs to sleep and wake on button presses

I hope this clears any confusion you had.

all the solutions I've attempted so far.

So, to further avoid confusion, you've got code, because an attempt must involve code.

So, post the code, and your observations of what went right, and what went wrong.

Otherwise, it looks like you want us to do your homework.

In Variable declaration:

const int offButton = 2;
const int onButton = 3;
volatile int offButtonState = 0;
volatile int onButtonState = 0;

In the setup:

  attachInterrupt(0, offPinISR, CHANGE);
  attachInterrupt(1, onPinISR, CHANGE);

At the bottom of the code:

void offPinISR(){
  offButtonState = digitalRead(offButton);
  set_sleep_mode(SLEEP_MODE_IDLE);
  sleep_enable();
  }

void onPinISR(){
  onButtonState = digitalRead(onButton);
  sleep_disable();
  detachInterrupt(0);
  }

From what I could research before coming here, and in the time thus spent on this forum, I typed this up. This code doesn't work as when uploaded to the board nothing happens. I read that "IDLE" requires a predetermined amount of time so I held the buttons down to no avail.

Please read this, carefully.

It's not the mark of the sharpest spoon in the kitchen drawer, to report a moderator to the moderator, but I'll try to pinpoint it for you - read this, particularly the bit about not posting snippets.

I'll leave you to it now - I'm out.

AWOL:
It's not the mark of the sharpest spoon in the kitchen drawer, to report a moderator to the moderator, but I'll try to pinpoint it for you - read this, particularly the bit about about not posting snippets.

I'll leave you to it now - I'm out.

AWOL:
Please read this, carefully.

Just because you're a moderator doesn't mean you can't bean a jerk or unhelpful. Your station or position doesn't exclude you from being elitist. Nor does it mean that you're the only moderator that would see the report and inquire about the thread I reported from.

As for the part on snippets:

"Sometimes we can diagnose an issue with just a snippet of code, but more often than not, the complete code will reveal to us problems that can't be found with just a snippet. If you can't post the complete code, schematics, or other parts of your project (due to any number of issues), then you may find that we won't be of much help."

I am afraid all I have pertaining to the question is snippets. I don't have a github that has the code in question so I can't post a link. I read the document.

After rereading that, I still don't see how redirecting me there is helpful or useful. I stated the board board model, the library in question, and posted code within code tags, and stated the direction I'm attempting to take with this one small part of the project as a whole.

Note that the document doesn't even say that I have to post code to post on this forum because it's for guidance, but I typed up something that may be used as a jumping off point for you, but a full stop for someone like me that isn't experienced with ISRs.

You can even see in the code that I did post, that I took your advice and attached the ISRs to pins 2 and 3 for int0 and int1 since I'm using an UNO.

dmp5k2:
This code doesn't work as when uploaded to the board nothing happens.

dmp5k2:
I am afraid all I have pertaining to the question is snippets.

Those two statements are mutually exclusive. To proceed I suggest you select the one that bears the truth.

You're asking for code fixes, yet you don't post complete code nor a description of what it does and how it's different from what you want it to do. Therefore, there's nothing to fix.

You talk about uploading code, yet you claim you don't have code. Then you post code snippets. That's odd.

Using an interrupt to wake up the Arduino is the correct approach. Using an interrupt on the sleep button however is almost certainly not. You can just poll the button state and react to it in loop().

Besides the link given to you already, I'd also recommend you to read this and this. And you may consider to be more friendly and cooperative towards people whom you're asking free help from.

Thus far, I've found article after article about a single button press and waiting to wake up, using serial to wake it up, etc.

set_sleep_mode(SLEEP_MODE_IDLE);

In your explorations of the sleep mode, did you never run across this?

From Nick Gammon https://www.gammon.com.au/power

Note: In IDLE mode, the clocks are running. This means that (unless you disable it) the normal Timer 0 clock used to count millis() will be running, and thus you will wake up approximately every millisecond.

If you read through that tutorial , I'm sure you will figure out how to put the Arduino to sleep and wake it up.