Resetting Arduino after pressing the buttons 16 times

Good afternoon,

I'm trying to make my Arduino reset itself after pressing the pushbutton 16 times, however this is not working out at all. It is pressing the buttons but will not reset itself after 16 times. Can any of you please help me?

My code:

// this constant won't change:
const int buttonPin = A1; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int count = 0;

void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}

void loop() {
// read the pushbutton input pin:
buttonState = !digitalRead(buttonPin);

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;

// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

//Source: Yours for the making - Instructables
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
count++; // add 1 to the count
} else (count >= 17); {
count = 0;
}
}

DanielBnl:
Good afternoon,

I’m trying to make my Arduino reset itself after pressing the pushbutton 16 times, however this is not working out at all. It is pressing the buttons but will not reset itself after 16 times. Can any of you please help me?

try using this MultiPress library

you can easily define an action for 16 presses…

the function would look like this:

void B_ButtonActions(const int value)  // example of registering Multi-Presses
{
  Serial.print(F("Button B:\t"));
  switch(value)
  {
    case -1:
      Serial.println(F("Long Press"));
      break;
    case 1:
      Serial.println(F("One Press"));
      break;
    case 2:
      Serial.println(F("Two Presses"));
      break;
    case 3:
      Serial.println(F("Three Presses"));
      break;
    case 16:
      // RESET ARDUINO HERE<<<<<<<<<<
      break;
    default:
      Serial.println(F("Whole Lotta Presses"));
      break;
  }
}

Is there a simpler way? using 'basic' commands? Your way looks advanced to me and I'm trying to keep it as simple as possible.

how about pressing the reset button?

DanielBnl:
Is there a simpler way? using 'basic' commands? Your way looks advanced to me and I'm trying to keep it as simple as possible.

Connect a digital output pin to the reset pin.

The resetting has to happen automatically after I press the pushbutton counter 16 times. I forgot to mention that I’m using an Multi Function Shield for this as this is required.

Is this a school assignment? You never told us what was wrong with your posted code.

You didn’t use code tags to post your code. It’s important that you use them - if you don’t, the forum software sometimes interprets sequences of characters in the code as directives to format text in some way. It won’t display those sequences, and it will unexpectedly reformat the rest of the text. When that happens, and someone tries to copy your code and paste it into the IDE, it often throws an error, and readers will complain that the code fails to compile. Using code tags also makes it easier to read, and can be copied with a single mouse click.

Unless the sketch is too large, it’s better if you post your code, rather than attach it. When it’s attached, we have to download it, open it in a text editor, then copy it to the IDE. It’s much easier to just view the code in your post.

The code tags make the code look

like this

when posting source code files.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the [code] and [/code] metatags.

When you are finished that, please read this post:

How to use this forum - please read.

DanielBnl:
The resetting has to happen automatically after I press the pushbutton counter 16 times. I forgot to mention that I’m using an Multi Function Shield for this as this is required.

this would be the entire (un-tested) code using that library:

#include <avr/wdt.h>
#include "MultiPress.h"

#define LEDPIN 13

#define BUTTON 3

void buttonActions(const int value);

SimplePress pushButtonSwitches[] = {
  {BUTTON, 300, buttonActions},
};

void setup()
{
  MCUSR = 0;
  WDTCSR |= _BV(WDCE) | _BV(WDE);
  WDTCSR = 0;
  Serial.begin(115200);
  pinMode(LEDPIN, OUTPUT);
  Serial.println(SimplePress::getCount());
  SimplePress::beginAll();
  SimplePress::setDebounceAll(50);
}

void loop()
{
  SimplePress::update();
}

void buttonActions(const int value)  // example of registering Multi-Presses
{
  Serial.print(F("Button B:\t"));
  Serial.println(value);
  switch (value)
  {
    case 16:
      Serial.println(F("RESETTING"));
      wdt_enable(WDTO_1S); while (1) {}
      break;
  }
}

Do you specifically want to reset the Arduino or would it be good enough to start counting again ?

What exactly are you trying to do ?

Why does your code appear to use two different variables to hold the count ?
I assume that you have seen the StateChangeDetection example in the IDE

You might like to examine this portion of your code and determine what is wrong with it

 else (count >= 17); 
  {
    count = 0;
  }

Why do you sometimes set buttonstate to digitalRead(buttonPin) and sometimes to !digitalRead(buttonPin) ? Seems a bit odd.

Steve

Oke an summary of what I'm trying to do.

I'm trying to reset my Arduino whenever the pushbutton has been pressed 16 times. I need to use the StateChangeDetection example (which I'm using right now) to make the code work. There is something wrong with my code that makes sure that my Arduino will not reset automatically.

DanielBnl:
There is something wrong with my code that makes sure that my Arduino will not reset automatically.

...and what is that "something"? :frowning: In other words, please describe what it does or doesn't do. Please answer my question, is this for school? If it does reset, how will you know that it has?

@aarg

Yes, this is for an school assignment (sorry for not reacting to your question) that ‘something’ is that it’s counting past 16 without not resetting the Arduino automatically. You mentioned how I’ll see if my Arduino is resetting. The way I’m trying to do it is let an message pop up on the serial monitor that let’s me know that the Arduino is going to reset.

You may think you're being clear but let's try for even clearer. When you say you want to "reset my Arduino" do you mean
a) do the same as pressing the Reset button would do i.e initialise everything and run setup() again?
or
b) reset just the count so it restarts counting from zero?

Which one ? Only I can't see any code which is even attempting to do the first of those...and that's what most people understand by "resetting the Arduino".

Steve

it's counting past 16 without not resetting the Arduino automatically.

See reply #8

@Steve

I thought it through and resetting my Arduino may not be the best of solutions especially if the assignments say's ''make sure that when the buttoncounter reached 16, it starts at 0'' So I think resetting my counter works just fine.

 else (count >= 17); 
  {
    count = 0;
  }

Shouldn't there be an if in there somewhere ?
Why is there a semicolon on the of the else clause ?

I tried using it with if and else statements but that still did not work. Are there any other options that may work? Could any of you give me an example? I scoured the internet looking for examples and possible solutions and tried implementing them but they did not work unfortunately.

You can find explanations of if-else and a lot more here:
https://www.arduino.cc/reference/en/

DanielBnl:
You mentioned how I'll see if my Arduino is resetting. The way I'm trying to do it is let an message pop up on the serial monitor that let's me know that the Arduino is going to reset.

Wouldn't it be a good idea to actually add that to your code, then?