I am currently working on a project with an Arduino UNO and it requires me to be able to call a function if the button is pulled high when the board is powered up, is this possible?
What I ultimately need to accomplish in the end is to have a button counter that counts the number of times a button is pressed and then when that button (same button) is held down for more than three seconds to save that value to the EEPROM.
I only want it to count the number of times the button has been pushed IF the button was pressed down when the board was powered on, other wise I want it to do something else.
I am relatively new to the Arduino/programming and I hope someone can point me in the right direction. I have the button counter working properly with debounce and all, and the next step is to be able to run the code on start up if the button is pressed and save the value to the EEPROM.
Off the top of my head, reading the button in setup() might be the answer. Seeing as setup() only runs once, a read there would tell you the pins state at power up, then have that set a flag for use later on, like in an if, deciding what to do.
something like this... I use a button on pin3 with the internal pull-up resistor and tested for LOW for at least 2 seconds during restart... try it and see if it works for you...
I tried structuring it in the void setup but i need it to enter a loop while it looks for button inputs and counts them. I guess I cant figure out how to jump from void setup into a loop if it notices pin2 pulled high on power up. I will probably have it do something similar so it needs to be pressed for 2 seconds before it would do anything . I feel like I'm missing something that could be stupid simple to do... hmm
Set a flag in setup() when it detects the button was pushed at startup. Then in loop(), where it's going to go anyway, have an if that does "this" if the flag is set, or "that" if it wasn't.
in setup():
if (buttonState== HIGH) pushedFlag = 1;
and in loop():
if (pushedFlag==1)
{
//do this
}
else
{
//do that
}
I cant seem to get it respond to weather the pin is pulled high in startup or not.. what am i doing wrong?
const int buttonPin = 2; // the pin that the pushbutton 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 pushedFlag = 0;
void setup()
{
// initialize serial communication:
Serial.begin(9600);
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
//If button is pressed on startup, set a value of "1"
if(buttonPin == HIGH) pushedFlag = 1;
}
void loop()
{
//If a value of 1 has been set, do this
if (pushedFlag== 1)
{
int reading = digitalRead(buttonPin);
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// helps to debounce
delay(100);
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println(buttonPushCounter);
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
else
{
}
}
// initialize serial communication:
Serial.begin(9600);
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
//If button is pressed on startup, set a value of "1"
if(digitalRead(buttonPin) == HIGH) pushedFlag = 1;
Good new, everything is working!
Bad news, the code is kind of a mangled mess...
I have another question, how do I exit the loop? specifically the button counting one. After I save the value to the EEPROM I want to essentially exit the button counting loop and move on to the pushedFlag = 0 code.
No matter how ugly the code is, I have been learning a lot, and having fun doing it.
Full code.
#include <EEPROM.h>
int buttonPin = 2; // the pin that the pushbutton is attached to
int duration = 0; // 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 pushedFlag = 0;
long int endTime;
int value = 0;
int pulseBegin = 0;
void setup()
{
// initialize serial communication:
Serial.begin(9600);
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
//If button is pressed on startup, set a value of "1"
if(digitalRead(buttonPin) == HIGH) pushedFlag = 1;
}
void loop()
{
//If a value of 1 has been set, do this
if (pushedFlag== 1)
{
int reading = digitalRead(buttonPin);
// 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 == HIGH)
{
// helps to debounce
delay(200);
//pulseBegin is = to millis time
pulseBegin = millis();
//while buttonPin is HIGH stay
while (digitalRead(buttonPin) == HIGH){}
//duration = millis time - the time sense start
duration = millis() -pulseBegin;
//Serial.println(duration);
if(duration >= 3000)
{
Serial.println(buttonPushCounter-1);
//write number of button presses to mem 25
EEPROM.write(25, buttonPushCounter-1);
//value = EEPROM.read(25);
//Serial.print(value);
delay(1000);
}
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println(buttonPushCounter);
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
if (pushedFlag== 0)
{
Serial.println("Main Program");
delay(3000);
}
}
After I save the value to the EEPROM I want to leave the loop, I tried creating another "pushedFlag" but it just messes everything up so I thought I'd ask for help.
if(duration >= 3000)
{
Serial.println(buttonPushCounter-1);
//write number of button presses to mem 25
EEPROM.write(25, buttonPushCounter-1);
//value = EEPROM.read(25);
//Serial.print(value);
delay(1000);
}
if(duration >= 3000 && !eepromWritten) // <<<<<<<< see change here, ! means "not"
{
Serial.println(buttonPushCounter-1);
//write number of button presses to mem 25
EEPROM.write(25, buttonPushCounter-1);
eepromWritten = true; //<<<<<<<<<<<<<<<< set the flag so next time it will fail the "if test"
// remember to declare the eepromWritten flag as false earlier in the code <<<<<<<<<<<<<<<<<<<<
//value = EEPROM.read(25);
//Serial.print(value);
delay(1000);
}