Pages: [1]   Go Down
Author Topic: Drag strip sketch in the works  (Read 1607 times)
0 Members and 1 Guest are viewing this topic.
Greenville, IL
Offline Offline
Edison Member
*
Karma: 15
Posts: 1328
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 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
Code:
//********************************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; 

}

Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25898
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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);
Well, I'd probably lose the semicolon on the "if".
I'd also probably not want to keep seeing "We are ready to start the race!" printed over and over again (and potentially meaning that I actually missed the start of the race!), so I think instead of an "if", I'd use a "while".
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Dubuque, Iowa, USA
Offline Offline
Edison Member
*
Karma: 45
Posts: 2474
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm working on same (snowmobiles). Given that it's summer for me I have a while before I have to worry about completing everything, but keep in touch.

The way I designed my code I looked at the race as being in three possible states: pre-stage (verify beam integrity / watch for faults), staging (christmas tree countdown / watch for faults), and racing (record beam breaks / calc timings / display time / etc.).

You need two buttons: one to reset the race back to pre-stage and one to start the christmas tree countdown.

For my "beams" I'm intending to use 38Khz IR with the beamer being a simple voltage regulator, 555 timer, and IR LED while the beam target is a fixed-gain IR receiver (TSOP4038). Unfortunately after I placed my order I realized the receiver is on backorder so I haven't been able to test it out yet smiley-confuse. I tried using a CdS photoresistor initially but found them to react much too slowly, plus you have to worry about ambient light messing up the readings.
Logged

Greenville, IL
Offline Offline
Edison Member
*
Karma: 15
Posts: 1328
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


  Thanks both of you for the tips and suggestions! I will continue on the project and update the post when I get more to show.

Thanks,
Mark
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Any updates here?
Logged

Pages: [1]   Go Up
Jump to: