Skip to content

Instantly share code, notes, and snippets.

@timvisee
Last active December 9, 2015 22:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timvisee/681e6ed58524723abf0d to your computer and use it in GitHub Desktop.
Save timvisee/681e6ed58524723abf0d to your computer and use it in GitHub Desktop.
Number Guess Timer example, Arduino code.Sample code for a HHS challenge.
/**
* Number Guess Timer example.
* Sample code for a HHS challenge.
*
* @author Tim Visée
* @website http://timvisee.com/
*/
#include <Arduino.h>
/** Timer timeout, period in milliseconds to break the loop if nothing happens. 1 second in this case. */
int TIMER_TIMEOUT = 1000;
/** The actual timer, this specifies the time to break the loop at in milliseconds. */
long timer = -1;
/**
* Start the timer, and set the proper timeout.
* This method can be used multiple times to update the timeout time.
*/
void startTimer() {
timer = millis() + TIMER_TIMEOUT;
}
/**
* Check whether the timer is finished.
* If the timer isn't started, or if it has been reset, false will be returned.
*
* @return True if the timer has been started and has finished, false otherwise.
*/
bool isTimerFinished() {
return millis() >= timer && timer >= 0;
}
/**
* Stop the timer.
* Change the timeout value to minus one, to ignore the whole timer.
*/
void stopTimer() {
timer = -1;
}
/**
* Called once on setup.
*/
void setup() { }
/**
* Called each loop.
*/
void loop() {
// Start the timer
startTimer();
// Loop until the timer has been finished
while(!isTimerFinished()) {
// TODO: Check whether the button is pressed, replace variable in the if-statement below:
if(buttonPressed) {
// TODO: Add one to the entered number here.
// Reset and start the timer again
startTimer();
}
// TODO: Do some other stuff here, if you'd like.
}
// Reset and stop the timer
stopTimer();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment