Making a laser-triggered timer with Arduino. Check it.

edited May 2015 in Share Your Work

Hi! We organize local mountain bike competitions, so I decided to make a laser-triggered finish timer.

Riders starts every minute (we just made a video of starting lights, looped in a minute interval and launched it on ipad on start)) and there is about 30 seconds to finish, so next rider starts when previous have already finished.

So my goal was to make a 60sec looped timer that display current time, when laser beam was crossed.

I made a simple scheme with arduino, photoresistor and laser pointing on it. When photoresistor's value drops - show the time. I'm new with all this stuff but... why not?

I tried and it seems to work fine. But maybe you can see some errors or give me some advice to improvement!

Here is the code:

    import processing.serial.*;
    import cc.arduino.*;
    PFont font, font2;

    Arduino arduino;
    int a = 7; // sets switch threshold
    int bg = 20; //bg color
    String txt = "Waiting..."; 
    String go = "Go! Go! Go!";
    String up = "COUNTRY SLALOM";
    String dn = "PROTVINO 2015";
    int savedTime;
    int totalTime = 59999; //reset time
    long startMillis;
    PrintWriter output;

    void setup() {
      size(displayWidth, displayHeight);
      background(bg);
      arduino = new Arduino(this, "COM5", 57600);
      output = createWriter("positions.txt"); 
      frameRate(120);                           //?
      startMillis = millis();
      savedTime = millis();
      textAlign(CENTER);
      font = createFont("Impact",72,true);
      font2 = loadFont("MyriadPro-Bold-72.vlw");
      textFont(font2,100);
      fill(204);
      text(up, displayWidth/2, displayHeight/6);
      text(dn, displayWidth/2, displayHeight-200);
      textFont(font,160);
      fill(255,102,0);
      text(txt, displayWidth/2, displayHeight/2);
    }

    void draw() {                                                         
      int passedTime = millis() - savedTime;    // Calculates how much time has passed
      String time = ("Time:\t  " + round(passedTime/10)/1e2);

     if (arduino.analogRead(0)<a)    // shading trigger
     {                                                                                            
        if (millis()-startMillis >=1e3)   //anti-lag
        {
        fill(20);
        rect(0, displayHeight/4, displayWidth, displayHeight/3);
        //background(bg);
        fill(255,102,0);
        text(time, displayWidth/2, displayHeight/2);
        output.println(time);
        startMillis=millis();
        }
       } 

      if (passedTime > totalTime) {
        savedTime = millis();  // Save the current time to restart the timer   (?)
        fill(20);
        rect(0, displayHeight/4, displayWidth, displayHeight/3);
        fill(255,102,0);
        text(go, displayWidth/2, displayHeight/2);
      }

    }
    void keyPressed() {
      if (key == ENTER) {  //sync timer with iPad video of starting lights
        savedTime = millis();
        fill(20);
        rect(0, displayHeight/4, displayWidth, displayHeight/3);
        fill(255,102,0);
      text(txt, displayWidth/2, displayHeight/2);
        println("reset");
      }
      if (key == 'a'){   //autoset a (trigger treshold)
        a=arduino.analogRead(0)-50;
        println(a);
      }
      if (key == 's'){     //save positions.txt
        output.flush();
      //output.close(); 
       }

    }

Any advices?

Tagged:
Sign In or Register to comment.