1

enter image description here
I am working on Arduino Mega project where I want to add a PUSHBUTTON (Any other can be suggested) that will be programmed in my arduino code, such that if I would press the button arduino code should stop working and when I will press the button back the code should resume from the moment where it stopped. Button will be attached to 5v pin so that will turn off/on the arduino.

4
  • 2
    Possible duplicate of RESET and RESUME case in Arduino mega 2560 code Commented Nov 17, 2017 at 10:18
  • 2
    You asked the same thing already ... Commented Nov 17, 2017 at 10:19
  • yes but now i am trying to add a pushbutton at power supply pin (5v) of arduino board. It will work as auto power on/off of to the arduino. Commented Nov 17, 2017 at 10:32
  • Publish your schematic. I wonder what are you doing. Commented Nov 17, 2017 at 10:42

3 Answers 3

3

Your question is very badly written, and a lot of infos are missing. In any case, from what I understood from your comments, you want a button that works this way:

  • The arduino is not powered up
  • You press the button; the arduino turns on, and starts the operations.
  • You press the button; the arduino shuts down.
  • You press the button; the arduino turns on, and resumes the operations from where it was.

I'm not sure about the last point, so first of all I'll give you some hints about the first three points.

You can modify the circuit in a way similar to this:

schematic

simulate this circuit – Schematic created using CircuitLab

(I named the two arduino pins as A and B because it is not important which pins you use, just two separate pins).

The diodes can be any diode (e.g. 1N4148). The PMOS and NMOS are not critical, provided that they can fully turn on with a 4-5V gate voltage and that the PMOS can bear the current you need.

When the arduino is shut down, the NMOS is off, and so is the PMOS. When you press the button, the voltage on the NMOS gate becomes about 4.5V, so it turns on. the PMOS turns on too, and so the arduino sees some power. At the very first moment, it has to pull pin B to 5V. This way even when the button is not pressed anymore, the board will be powered. When the user presses the button, the arduino can sense it through pin A. This way it can go into shutdown mode. Here is a sort of base for handling this behavior. Please note that I'm using the Bounce2 library, since I particularly like it when dealing with buttons (it automagically handles bounces and also give more info about the edges).

#include <Bounce2.h>

const byte pinA = the pin A number;
const byte pinB = the pin B number;

Bounce pinA_debounced = Bounce(); 

void shutdown() {
    // Turn off the board
    digitalWrite(pinB, LOW);
    // Farewell, cruel world!
    while(1); // wait for a power off
}

void setup() {
    // Turn on the board
    pinMode(pinB,OUTPUT);
    digitalWrite(pinB, HIGH);

    // Initializate the debounced pin
    pinA_debounced.attach(pinA, INPUT_PULLUP);

    // Other setup instructions
}

void loop() {
    // Update the Bounce instance :
    pinA_debounced.update();

    if (pinA_debounced.rose())
        shutdown();

    // Other loop instructions
}

If you want to save your data in order to resume it, well, this highly depends on your actual code. You will have to identify what is your "current status", then save it to EEPROM.

Here is, for instance, a program which outputs on serial one sequential number (from 0 to 254, then restart from 0) every second. When you shut it down, it will start again from the last one:

#include <Bounce2.h>

const byte pinA = the pin A number;
const byte pinB = the pin B number;

const byte valueAddress = 0x10; // Any value is fine for this
// as long as it is in the available EEPROM

Bounce pinA_debounced = Bounce();
byte value;
unsigned long lastMillis;

void shutdown() {
    // Save status BEFORE turning down the power
    EEPROM.update(valueAddress, value);

    // Turn off the board
    digitalWrite(pinB, LOW);
    // Farewell, cruel world!
    while(1); // wait for a power off
}

void setup() {
    // Turn on the board
    pinMode(pinB,OUTPUT);
    digitalWrite(pinB, HIGH);

    // Initializate the debounced pin
    pinA_debounced.attach(pinA, INPUT_PULLUP);

    // Restore the current value from EEPROM
    value = EEPROM.read(valueAddress);
    Serial.begin(9600);
    lastMillis = millis() - 1000;
}

void loop() {
    // Update the Bounce instance :
    pinA_debounced.update();

    if (pinA_debounced.rose())
        shutdown();

    if ((millis() - lastMillis) >= 1000)
    {
        lastMillis += 1000;
        Serial.println(value);
        value++;
    }
}
1

If you rewrite the code to be a state machine so that loop() never blocks it's pretty easy to add:

bool paused = false;
void loop(){
    //read button
    if(buttonPressed){
        paused = !paused;
    }
    if(paused){
        return;
    }
    //rest of code written so it never blocks and returns as fast as possible
}

Then the resume part is handled by the extra already required by needing to be able resume on next call of loop.

If you go for full power off then you need to write out your state to non-volatile memory and read it back in on init.

2
  • The question is unclear. He is asking "Button will be attached to 5v pin so that will turn off/on the arduino." There is a power off in between Commented Nov 17, 2017 at 12:12
  • This is not an answer to the question asked - you've neither shown how to control the Arduino's power, nor how to suspend it to a low power mode as an alternative to literally switching it on/off. Commented Nov 17, 2017 at 17:07
-1

if I would press the button arduino code should stop working and when I will press the button back the code should resume from the moment where it stopped.

the simplest would be use an interrupt trigger by the button to set a flag and the execution would either loop around the flag or go straight through, depending on the state of the flag.

4
  • And how he will resume the execution at the point he stopped? Commented Nov 17, 2017 at 12:05
  • Just one question: why use an interrupt? I can understand if you have blocking code, so with an interrupt you can even never exit the interrupt function until the button is pressed again, but why use the interrupt like this? Commented Nov 17, 2017 at 14:52
  • @frarugi87 - Like most of "dannyf"'s posts this fails to address the question which is the topic of the page, but to answer your question, if you want to programmatically suspend a piece of code without its cooperation, an interrupt is in general your only choice. Commented Nov 18, 2017 at 2:08
  • @ChrisStratton I understand that, that's why I wrote "I can understand [you want to use interrupts] if you have blocking code". But danny's solution is to set a flag in the interrupt, then in the code loop on that flag until the flag is cleared. If you want to block an "uncooperating" code, you have to use an interrupt and never exit the interrupt until you want to resume operations, otherwise you can just poll the pin inside the program rather than checking for a flag.. Commented Nov 18, 2017 at 12:08

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.