now its time to work out how long between the button presses
if (stopTime != 0 && stopTime != 0) {
interval = stopTime - startTime;
}
millis is only zero during boot-up so to stop interval being calculated before we have both start and stop we are using a simple if.
If start and stop not equal zero (neither equals 0) then do the calculation to work out what interval is
next we are going to cut and paste code from blink with out delay. I added the extra flag pause on to this code.
If the flag is set to 1 then the blink with out delay code will not be executed (the led will not blink)
If the flag is set to 0 then the blink with out delay code will not be executed (the led will blink at the timing entered by interval)
unsigned long currentMillis = millis();
if (pause == 0) {
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (ledState == LOW)
{
ledState = HIGH;
}
else
{
ledState = LOW;
}
}
} else {
ledState = LOW;
}
you will have to add this line to make it blink
digitalWrite(ledPin, ledState);
so that's button one taken care off
now for button two
we want it to pause the code but not to reset interval. we already have the flag to start the code so to pause the code we set pause to 1
if (buttonState2 == LOW) {
ledState = LOW;
pause = 1;
}
you will notice we only look at button2 if its pressed (low) and we don't need to remember we pushed it
ledState = LOW was added as we have no idea what state the ledState will be in when we stop the bwod code so we are just saying turn the led off when paused
button 3 we want to pause the bwod code then reset all the flags to 0. we also need to clear the timestamps (start/stop) and interval. Last but not least we need to reset buttonUse to zero (same place it starts at after a reboot)
if (buttonState3 == LOW) {
pause = 1;
buttonUse = 0;
interval = 0;
startTime = 0;
stopTime = 0;
oneTime1 = 0;
oneTime2 = 0;
}
so that's the basic code. Its not pretty as I wrote something you should understand.
I will give you the globals to help you get started
const byte ledPin = 13;//led onboard
const byte button1 = 10;//pin number input 1
const byte button2 = 9;//pin number input 2
const byte button3 = 8;//pin number input 3
byte pause = 1;
byte ledState = 0;
unsigned long previousMillis = 0;
unsigned long startTime;
unsigned long stopTime;
unsigned long interval;
byte buttonUse = 0;
byte prevButtonState1 = 0;
byte oneTime1 = 0;
byte oneTime2 = 0;
you will have to do the setup of the pins
pinMode (ledPin, OUTPUT);
pinMode (button1, INPUT_PULLUP);//set as a pullup
pinMode (button2, INPUT_PULLUP);
pinMode (button3, INPUT_PULLUP);
and add this code to read the button presses in the main loop
byte buttonState1 = digitalRead (button1);//read the button
byte buttonState2 = digitalRead (button2);
byte buttonState3 = digitalRead (button3);