Skip to content

Instantly share code, notes, and snippets.

@vishal-rana
Created April 13, 2017 02:53
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/dcb530bab41862bf9871584a284478f0 to your computer and use it in GitHub Desktop.
Save vishal-rana/dcb530bab41862bf9871584a284478f0 to your computer and use it in GitHub Desktop.
//written for teensy, if using on a non-teensy platform, you must include #include <elapsedMillis.h> http://playground.arduino.cc/Code/ElapsedMillis
#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#include "pitches.h"
#include "tones.h"
#define startPin 9
#define finishPin 10
const int numSensors = 7;
int sensorPins[numSensors] = {A0,A1,A2,A3,A4,A5,A6}; //define sonal panel sensing pins here
int sensorThreshhold[numSensors];
Adafruit_7segment matrix = Adafruit_7segment();
int displayBlinkRate=0;
boolean timerRunStatus;
elapsedMillis currentRun;
elapsedMillis hasOneSecondTicked;
elapsedMillis lastPenalty;
elapsedMillis updateDisplay;
void setup()
{
Serial.begin(38400);
pinMode(startPin, INPUT_PULLUP); //SINCE THE START PIN BUTTON'S NORMALLY OPEN //PIN BROKE, THE NORMALLY CLOSED PIN AND //HENCE THE CORRESPONDING LOGIC IS USED.
pinMode(finishPin, INPUT_PULLUP);
calibrateSolarPanels();
matrix.begin(0x70);
timerRunStatus=false;
currentRun=0;
update_display();
}
void loop()
{
if(timerRunStatus==false){
check_start_button();
currentRun=0;
}
else{
check_solar_panels();
check_finish_button();
}
if ((hasOneSecondTicked>=1000)&&(timerRunStatus==true)){
hasOneSecondTicked=hasOneSecondTicked-1000;
second_ticked_beep();
}
if ((updateDisplay>=50)&&(timerRunStatus==true)){
updateDisplay=updateDisplay-50;
update_display();
}
}
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) {
for (int i = 0; i < numSensors; i++) {
sensorThreshhold[i]+=(analogRead(sensorPins[i]));
}
samples++;
delay(1);
}
for (int i = 0; i < numSensors; i++) {
sensorThreshhold[i]=(sensorThreshhold[i]/samples);
}
Serial.println(sensorThreshhold[0]);
}
void check_start_button(){
int pinStatus=digitalRead(startPin);
if(pinStatus==HIGH){//button is pressed
currentRun=0;
update_display();
countdown_beep();
currentRun=0;
displayBlinkRate=0;
lastPenalty=0;
hasOneSecondTicked=0;
timerRunStatus=true;
}
}
void check_solar_panels()
{
for (int i =0; i < numSensors; i++) //iterate through sensor pins and read analog values
{
int cur_pin = sensorPins[i];
int sensorVal = analogRead(cur_pin);
Serial.println(i,sensorVal);
if ((sensorVal < (sensorThreshhold[i]*.85)) && (lastPenalty>1000))
{
lastPenalty=0;
penalty_timer();
penalty_beep();
break;
}
}
}
void check_finish_button(){
int pinStatus=digitalRead(finishPin);
if(pinStatus==LOW){//button is pressed
displayBlinkRate=1;
update_display();
victory_beep();
timerRunStatus=false;
}
}
void penalty_timer()
{
currentRun+=5000;// add 5 sec (or whatever) time penalty here
}
void update_display(){
matrix.blinkRate(displayBlinkRate);
float currentRunSec=currentRun/1000.0;
matrix.print(currentRunSec);
matrix.writeDisplay();
delay(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment