Switch Microcontroller on and off with a button and mofset

hi guys
I need turn on/off arduino NANO by pressing a Puch button for 2 seconds each time
I used this schematic and now I need help writing the code to make it work

First, when I hold the push button, the mofast is connected and the device turns on, now the analog pin 0 is low, and in order for the device to stay on, digital pin 6 needs to high.
When the analog pin 0 button is pressed again for 2 seconds, the digital pin 6 will be reduced until the device is turned off.
I wrote some code but it didn't work
Please help me in writing the code
Thanks

Upload the code you wrote, it is always more facial to correct an error than for us to start from scratch.

Then put the controller into deep sleep when the condition is met. Make sure that the selected pin also allows to wake up the controller from deep sleep.

thanks for answer gonpezzi
now I write new code
It seems to work properly
By holding the key, the device turns on and stays on
The only problem I have is that the arduino turns off after a shortly press button.
How can I define that the digital pin 6 reduce just by holding the key(A0) for 2 seconds?

my new code

const int START = A0, ON = 6;
int state = 0, ONstate=0;

void setup()
{
pinMode(START, INPUT_PULLUP);
pinMode(ON, OUTPUT);

}
void loop()
{
if (state == 0 && digitalRead(START) == LOW) {
state = 1;
ONstate=!ONstate;
}
if (state == 1 && digitalRead(START) == HIGH) {
state = 0;
}
digitalWrite(ON, ONstate);

}

The most appropriate is to use millis to measure the pulse time. Try this code.

const int START = A0, ON = 6;
int state = 0, ONstate = 0;
unsigned long Timer;

void setup() {
  pinMode(START, INPUT_PULLUP);
  pinMode(ON, OUTPUT);
}

void loop() {
  if (state == 0 && digitalRead(START) == LOW) {
    Timer = millis();
    while (digitalRead (A0) == LOW);
    if (millis() - Timer > 2000) { 
      state = 1;
      ONstate = !ONstate;
    }
  }
  if (state == 1 && digitalRead(START) == HIGH) {
    state = 0;
  }
  digitalWrite(ON, ONstate);
}

i tested this code
after press button (A0 LOW) while tuening on ardunio digitalpin 6 not high and ardunio turn off after unpress button

Try setting pin 6 HIGH in setup immediately after setting it to OUTPUT.
Might also want to wait for A0 to go HIGH before leaving setup.

It's the right thing to do, I overlooked it.

It would not recognize the off keystroke inside the loop. You would have to repeat the code also in the loop.

However, the code could look like this:

const int START = A0, ON = 6;
int state = 0, ONstate = 0;
unsigned long Timer;

void setup() {
  pinMode(START, INPUT_PULLUP);
  pinMode(ON, OUTPUT);
  digitalWrite (6, HIGH);
  on_off();
}

void loop() {
   on_off();
  //rest of the code
}

void on_off (){
 if (state == 0 && digitalRead(START) == LOW) {
    Timer = millis();
    while (digitalRead (A0) == LOW);
    if (millis() - Timer > 2000) {
      state = 1;
      ONstate = !ONstate;
    }
  }
  if (state == 1 && digitalRead(START) == HIGH) {
    state = 0;
  }
  digitalWrite(ON, ONstate);  
}

1 Like

No need to overly complicate things, once the output is HIGH there is no need to constantly be rewriting that, the only time another digitalWrite is needed is when the power is shut off. It should be sufficient to just keep track of whether the button has been held down for two seconds.

const int START = A0;
const int ON = 6;
unsigned long timer;

void setup() {
  pinMode(START, INPUT_PULLUP);
  pinMode(ON, OUTPUT);
  digitalWrite(ON, HIGH);
  while(digitalRead(START) == LOW){
    //wait for power button to be released
    };
  timer = millis(); //initialize timer
}

void loop() {
  if (digitalRead(START) == LOW){
    if ((millis() - timer) > 2000ul){
      digitalWrite(ON, LOW); //turn off power
      while(true){}; //do nothing while power shuts off
    }
  } else {
    timer = millis(); //reset timer
  }
}
1 Like
void on_off (){
 if (state == 0 && digitalRead(START) == LOW) {
    Timer = millis();
    while (digitalRead (A0) == LOW);
    if (millis() - Timer > 2000) {
      state = 1;
      ONstate = !ONstate;
    }
  }
  if (state == 1 && digitalRead(START) == HIGH) {
    state = 0;
  }
  digitalWrite(ON, ONstate);  
}

Something else to be careful of. The code is waiting for START to go high before checking for the 2000ms time interval, so the input will be high at that point. If the power does not die quickly, there may be enough time to go through loop again and turn the power back on before the processor shuts down. Not a problem if there is some more code to keep loop from repeating rapidly, but may be a problem with this test sketch.

1 Like

Right, and there it sits, no further instructions are executed until you lift your finger to check the elapsed time and toggle the output.

And I think that your code from post #10 is not what the PO wants, to my understanding it works like this:

  • On pressing it turns on, waits for the release of the button, assigns Timer the value of millis and does not make any comparison of the time passed until another press of the button. It can be activated without having passed the two seconds.
  • Once inside the loop and possibly after many laps of the loop Timer will surely be much greater than 2000 and when pressed it turns off immediately.

For both cases of on/off, the 2 seconds are never taken into account.

1 Like

I missed the part about having to hold the button for two seconds to turn the power on. Would help if the OP would clarify which of the following should happen:

1 > button pressed, power comes on, arduino waits for release of the button, then begins code execution if button has been held for two seconds or more. (code in this case being code other than the part for latching the power ON/OFF).
2 > button pressed, power comes on, arduino begins code execution after button has been pressed for two seconds, but does not wait for button to be released.
3 > button pressed, power comes on, arduino begins code execution immediately, but stops and shuts power off if button is released after being pressed for less than two seconds.

1 Like

Timer is set to the value of millis() whenever the button input is HIGH, so it keeps getting updated every time though loop until the button is pressed.

1 Like

Yes, better to wait, and not waste time uselessly, I had understood an on/off switch when the button is pressed for at least two seconds.

1 Like

Thanks everyone for the help me
I test this code. works
Only when I hold the key to turn on and the device turns on, until I release the key, the device does not run the code.
Is it possible for the arduino dont waits for release of the button?
I removed this part to solve this issue

  while(digitalRead(START) == LOW){//wait for power button to be released
    };

And I noticed that after test on code, the code is executed immediately, but if the key is held for a longer time, the arduino turns off again

See if this works properly, it should allow the code to run at power-on, but the button has to be released and pressed again to shut down.

const int START = A0;
const int ON = 6;
unsigned long timer;
bool startup = true;

void setup() {
  pinMode(START, INPUT_PULLUP);
  pinMode(ON, OUTPUT);
  digitalWrite(ON, HIGH);
}

void loop() {
  if (digitalRead(START) == LOW) {
    if (startup) {
      timer = millis();
    } else {
      if ((millis() - timer) > 2000ul) {
        digitalWrite(ON, LOW); //turn off power
        while (true) {}; //do nothing while power shuts off
      }
    }
  } else {
    startup = false;
    timer = millis(); //reset timer
  }
}
1 Like

thanks for write code
worked whell :+1:

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