I am working on a "drag strip" sketch. So far, I have wrote code for one lane only. I have made use of "millis" to capture the time from the start of the race to each sector. I have not put this on a board yet and I am still working out details.
My plan: 1, start the race using a start switch and a staging beam "push buttons for now".
2, Light yellow and then green leds to show start of race.
3, Time each sector and serial print it to the monitor.
4, Reset everything to restart the race. "I may not be done with that".
5, Once 1-4 are working I will add a second lane with separate LEDs and timers.
Have a look at the code and please share any ideas that you would like,
Mark
//********************************Time holders*********************
unsigned long starttime = 0; // will store start time
unsigned long sixty = 0; // storage for distance time
unsigned long twohundred = 0;
unsigned long fourhundred = 0;
unsigned long traptime = 0;
unsigned long finish = 0;
//*******************************O's***********************
const int yellowled = 9; // the number of the LED pins
const int greenled = 10;
const int stageled = 11;
const int ledPin = 13;
//*****************************I,s************************
const int stagebeampin = 2; // the numbers of the input pins
const int startpin = 4;
const int sixtypin = 5;
const int twohundredpin = 6;
const int fourhundredpin = 7;
const int finishpin = 8;
//******************************variables*******************
// Variables will change:
int start = 0; //the start switch status
int greenstate = 0; //is the green light on?
int beamstate = 0; //is the beam tripped?
int laneone = 0; // keeps status of lane
//***********************************************************
void setup() {
// set the digital pins as output:
pinMode(ledPin, OUTPUT);
pinMode(yellowled, OUTPUT);
pinMode(greenled, OUTPUT);
pinMode(stageled, OUTPUT);
// set the digital pins as input:
pinMode(stagebeampin,INPUT);
pinMode(startpin,INPUT);
pinMode(sixtypin,INPUT);
pinMode(twohundredpin,INPUT);
pinMode(fourhundredpin,INPUT);
pinMode(finishpin,INPUT);
Serial.begin(9600);
}
void loop()
{
Serial.print ("We are now ready to start the race!");
start = digitalRead(startpin); // read the start switch
beamstate = digitalRead(stagebeampin); // read the stagebeam state and store
if (beamstate == HIGH & start == HIGH);
{ // Lets start this race
digitalWrite(yellowled,HIGH);
delay(400);
digitalWrite(yellowled,LOW);
digitalWrite(greenled,HIGH);
greenstate = 1; // read the greenlight state and store
}
if (!greenstate || ! beamstate){ // while greenlight is out or stagebeam is not broken laneone is inactive.
laneone = 0;
}
else
{
laneone = 1;
digitalWrite(ledPin, HIGH);
}
if(laneone == 1){ //Begin the race timer
starttime = millis();
if (sixtypin){
sixty = (millis() - starttime); //Capture 60 foot time
}
if (twohundredpin) {
twohundred = (millis() - starttime); //Capture 200 meter time
}
if (fourhundredpin){
fourhundred = (millis() - starttime); //Capture 400 meter time
}
if (finishpin){
finish = (millis() - starttime); //Capture finish time
traptime = (finish - fourhundred); //Set trap time
}
Serial.print(" sixty "),(sixty),(" 200 "),(twohundred),(" 400 "),(fourhundred),(" trap "),(traptime),(" total "),(finish);
}
// set the LEDS OFF
digitalWrite(greenled,LOW);
digitalWrite(ledPin, LOW);
//reset greenstate
greenstate = 0;
}