Skip to content

Instantly share code, notes, and snippets.

@vishal-rana
Created April 13, 2017 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vishal-rana/483f838d330d2ac0343bf7b368599591 to your computer and use it in GitHub Desktop.
Save vishal-rana/483f838d330d2ac0343bf7b368599591 to your computer and use it in GitHub Desktop.
class NerdyTimer
{
//declare pins for solar panels that are part of laser trip wire
int startPin;
int stopPin;
//values that determine if the laser has been tripped or not
int startPinThreshhold;
int stopPinThreshhold;
//race time trackers
unsigned long raceStartTime;
unsigned long raceElapsedTime;
boolean timerRunStatus;
//7-segment display variables
byte matrix_addr;
int displayBlinkRate;
Adafruit_7segment matrix = Adafruit_7segment();
public:
NerdyTimer(int _startPin, int _stopPin, byte _matrix_addr)
{
startPin = _startPin;
stopPin = _stopPin;
matrix_addr = _matrix_addr;
raceElapsedTime=0.0;
displayBlinkRate=0;
timerRunStatus=false;
startPinThreshhold=0;
stopPinThreshhold=0;
}
void calibrateSolarPanels(){
//assuming that the solar panels are "seeing" the laser when this routine takes place
// calibrate with 10 samples
int samples=0;
while (samples < 10) {
startPinThreshhold+=analogRead(startPin);
stopPinThreshhold+=analogRead(stopPin);
samples++;
delay(1);
}
startPinThreshhold=startPinThreshhold/samples;
stopPinThreshhold=stopPinThreshhold/samples;
}
void start_display(){
matrix.begin(matrix_addr);
}
void update_display(){
matrix.blinkRate(displayBlinkRate);
matrix.print(raceElapsedTime);
matrix.writeDisplay();
delay(1);
}
void update_time(){
raceElapsedTime = (millis()-raceStartTime)/10.0;//convert to seconds
}
void check_start_pin_status(){
int pinStatus = analogRead(startPin);
Serial.print(startPinThreshhold);
Serial.print(pinStatus);
if(pinStatus < (startPinThreshhold*.90)){ //race start laser has been tripped
raceStartTime = millis();
timerRunStatus = true; //start race
displayBlinkRate=0;
}
}
void check_stop_pin_status(){
int pinStatus = analogRead(stopPin);
if(pinStatus < (stopPinThreshhold*.80)){ //race finish laser has been tripped
timerRunStatus = false;
displayBlinkRate = 2;
}
}
void reset_status(){
timerRunStatus = false;
displayBlinkRate = 0;
raceElapsedTime = 0.0;
}
void update_race_status(){
if (timerRunStatus == false){ //if race hasn't started, keep polling the start pin
check_start_pin_status();
}
else{ //race has started
check_stop_pin_status();
update_time();
}
update_display();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment