Holding a button to force reset my Arduino Nano

Hi everyone!

If I’m doing something here by posting this, that is out of place, such as having the wrong format, etc. Let me know.

On to the problem; I have the following code. No real problems but I’m looking to add a feature where I can hold down the same monetary button used and have it force reset the arduino.

Any help would be dope, thanks.

P.S. If you see aything else that can be improved upon, let me know.

P.P.S. Their may be some redudant bits of code in there. I hacked up codes from everywhere to put this together.

//initialize and declare variables
const int ledPinTL = 12; //top left led
const int ledPinTM = 11; //top middle led
const int ledPinTR = 10; //top right led
const int ledPinMM = 9; //middle middle led
const int ledPinBL = 8; //bottom left led
const int ledPinBM = 7; //bottom middle led
const int ledPinBR = 6; //bottom right led

const int buttonPin = 2; //push button attached to this pin

int buttonCounter; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

//----------------------------------------------------------------------------------------------------------------

void setup() {

//set the mode of the pins…
pinMode(ledPinTL, OUTPUT);
pinMode(ledPinTM, OUTPUT);
pinMode(ledPinTR, OUTPUT);
pinMode(ledPinMM, OUTPUT);
pinMode(ledPinBL, OUTPUT);
pinMode(ledPinBM, OUTPUT);
pinMode(ledPinBR, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);

}

//---------------------------------------------------------------------------------------------------------------

void loop() {

buttonState = digitalRead(buttonPin);

if (buttonState != lastButtonState) {

if (buttonState == HIGH) {

} else {

buttonCounter++;
Serial.println("Button count: ");
Serial.println(buttonCounter);

}

delay(50);

}

lastButtonState = buttonState;

if(buttonCounter > 8) buttonCounter = 1;

if(buttonCounter == 1){
digitalWrite(ledPinTL, HIGH);
digitalWrite(ledPinTM, LOW);
digitalWrite(ledPinTR, LOW);
digitalWrite(ledPinMM, LOW);
digitalWrite(ledPinBL, HIGH);
digitalWrite(ledPinBM, LOW);
digitalWrite(ledPinBR, LOW);
}
else if(buttonCounter == 2){
digitalWrite(ledPinTL, LOW);
digitalWrite(ledPinTM, HIGH);
digitalWrite(ledPinTR, HIGH);
digitalWrite(ledPinMM, HIGH);
digitalWrite(ledPinBL, HIGH);
digitalWrite(ledPinBM, HIGH);
digitalWrite(ledPinBR, LOW);
}
else if(buttonCounter == 3){
digitalWrite(ledPinTL, LOW);
digitalWrite(ledPinTM, HIGH);
digitalWrite(ledPinTR, HIGH);
digitalWrite(ledPinMM, HIGH);
digitalWrite(ledPinBL, LOW);
digitalWrite(ledPinBM, HIGH);
digitalWrite(ledPinBR, HIGH);
}
else if(buttonCounter == 4){
digitalWrite(ledPinTL, HIGH);
digitalWrite(ledPinTM, LOW);
digitalWrite(ledPinTR, HIGH);
digitalWrite(ledPinMM, HIGH);
digitalWrite(ledPinBL, LOW);
digitalWrite(ledPinBM, LOW);
digitalWrite(ledPinBR, HIGH);
}
else if(buttonCounter == 5){
digitalWrite(ledPinTL, HIGH);
digitalWrite(ledPinTM, HIGH);
digitalWrite(ledPinTR, LOW);
digitalWrite(ledPinMM, HIGH);
digitalWrite(ledPinBL, LOW);
digitalWrite(ledPinBM, HIGH);
digitalWrite(ledPinBR, HIGH);
}
else if(buttonCounter == 6){
digitalWrite(ledPinTL, HIGH);
digitalWrite(ledPinTM, HIGH);
digitalWrite(ledPinTR, LOW);
digitalWrite(ledPinMM, HIGH);
digitalWrite(ledPinBL, HIGH);
digitalWrite(ledPinBM, HIGH);
digitalWrite(ledPinBR, HIGH);
}
else if(buttonCounter == 7){
digitalWrite(ledPinTL, LOW);
digitalWrite(ledPinTM, HIGH);
digitalWrite(ledPinTR, HIGH);
digitalWrite(ledPinMM, LOW);
digitalWrite(ledPinBL, LOW);
digitalWrite(ledPinBM, LOW);
digitalWrite(ledPinBR, HIGH);
}
else if(buttonCounter == 8){
digitalWrite(ledPinTL, HIGH);
digitalWrite(ledPinTM, HIGH);
digitalWrite(ledPinTR, HIGH);
digitalWrite(ledPinMM, HIGH);
digitalWrite(ledPinBL, HIGH);
digitalWrite(ledPinBM, HIGH);
digitalWrite(ledPinBR, HIGH);
}
}

// Take note: if any additional code is made that attaches itself to the buttonCounter,
// button press 1 is also button press 9. All other numbers count as they should.
// 9 is necessary for the code to loop back over itself without problems

Button_counter.ino (4.5 KB)

Why do you need to reset the board ?

You can use the watchdog timer to reset the MCU.
In principle, you define an interval after which the MCU is reset. In the loop, you keep canceling the current reset countdown. You define a button which, on pressing, blocks the sketch (say while(true) ; ) so the reset countdown continues to the end and the MCU is reset.

I suppose I'm just curious really. I haven't been able to find an answer as of yet.

I would like to be able to hold down the same push button I'm already using, for around three seconds and have that reset the board.

I'll look up watch dog then and see how that goes. Thanks.

If you have any more ideas, please let me know.

Atlas28:
I would like to be able to hold down the same push button I'm already using, for around three seconds and have that reset the board.

It would be a lot more usual to have a separate reset button because it should only be necessary to reset the Arduino if something goes drastically wrong.

There should be no need to reset the Arduino in the normal course of operation.

...R

Atlas28:
Hi everyone!

If I’m doing something here by posting this, that is out of place, such as having the wrong format, etc. Let me know.

Since you asked:

Read this before posting a programming question…

Post your complete sketch (program code)! If you don’t you waste time while people ask you to do that.
When you post your code put it between

 ...

tags. You can do that by hitting the </> button above the posting area.

What are you trying to accomplish by resetting the board? Do you just want buttonCounter to be set to 0 and all outputs be set to LOW?

If so, you could capture the time the button becomes pressed and the time the button becomes released and calculate the difference between the two. If it is less than 3 seconds, increment buttonCounter. If it is greater or equal to 3 seconds, set buttonCounter to 0, and set all outputs LOW.

__asm ("jmp 0x0000 \n");

static unsigned long stateChangeTime = 0;
if (buttonState != lastButtonState)
{
  stateChangeTime = millis();
  if (buttonState == HIGH)
  {
  }
  else
  {
    buttonCounter++;
    Serial.println("Button count: ");
    Serial.println(buttonCounter);
  }
}
else // Button state has not changed
{
  if (buttonState == LOW && millis() - stateChangeTime > 3000)
  {
    // Button has been pressed for three seconds
    // Put your reset code here
  }
}