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);
}
// 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);
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?
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.
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:
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"? 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?
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".
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.
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.
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?