I have the following simulator code
What I'm trying to do is that when the button is first pressed the LED at A0 will be light on and will stay on and a the time of the button press will be store into a variable.
when the button is pressed once more the LED A0 will be turned off to indicate the button is pressed of the second time and the time of that second press will be store to another variable.
right now when I store a the time of millis when count is = 1 the time variable is keep update and stop only when count is 2 or != 1 (which make sense).
How can I store only the time when the button was first push? That it won't update it over and over
// Counts number of button presses
// output count to serial
byte switchPin = 13; // switch is connected to pin 2
byte ledPin = A0; // led on pin 13
byte buttonPresses = 0; // how many times the button has been pressed
byte lastPressCount = 0; // to keep track of last press count
unsigned long firstPressMillis;
unsigned long SecondPressMillis;
void setup() {
pinMode(switchPin, INPUT_PULLUP); // Set the switch pin as input
digitalWrite(switchPin, HIGH); // set pullup resistor
Serial.begin(9600); // Set up serial communication at 9600bps
}
void loop()
{
if (digitalRead(switchPin) == LOW) // check if button was pressed
{
buttonPresses++; // increment buttonPresses count
delay(250); // debounce switch
}
if (lastPressCount != buttonPresses) // only do output if the count has changed
{
Serial.print ("Button press count = "); // out to serial
Serial.println(buttonPresses, DEC); // wait again
lastPressCount = buttonPresses; // track last press count
}
if(lastPressCount == 1)
{
firstPressMillis = millis();
Serial.println (firstPressMillis);
}
}
Thanks