Last active
December 9, 2015 22:13
-
-
Save timvisee/681e6ed58524723abf0d to your computer and use it in GitHub Desktop.
Number Guess Timer example, Arduino code.Sample code for a HHS challenge.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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