Pages: [1]   Go Down
Author Topic: Laser Trigger timer  (Read 644 times)
0 Members and 1 Guest are viewing this topic.
Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino N00b
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm have two questions, firstly I would like the edit this code
Code:
*
Jonathan Robson Feb 09.
Laser tripwire activates a buzzer. adapted from
http://www.instructables.com/id/Arduino_Laser_Tripwire/
*/

int buzzPin = 11; // buzzer connected to digital pin 11

void setup() {
pinMode(buzzPin, OUTPUT); // sets the digital pin as output
}

void loop(){
if(analogRead(0) < 850){ // this number depends on calibration of the photocell
digitalWrite(buzzPin, HIGH); // turns buzzer on
delay(1000); // waits for 1 second
digitalWrite(buzzPin, LOW); // turns buzzer off

} else{
digitalWrite(buzzPin, LOW);
}
}
  from a laser trip wire project found here: http://www.instructables.com/id/Another-Arduino-Laser-Tripwire/  so that instead of buzzing when the laser is broken but to have a timer, so that when you shine the laser on it for lets say 3 seconds it starts a timer that gives you five minutes to shine the laser again or the buzzer goes off.

My second question is probably an obivous answer but I'm still new to this so please bare with me, if I wanted to use an arduino mini for this project would I use the exact same pins and code?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is what you asked for but probably not what you really want.  The user has no feedback to tell them when they have successfully triggered the timer.  They also get no feedback when they successfully disarm the timer.  I suggest two short beeps  when the timer starts and five short beeps when they disarm the timer.
Code:
int buzzPin = 11; // buzzer connected to digital pin 11

void setup() {
  pinMode(buzzPin, OUTPUT); // sets the digital pin as output
}

boolean hit() {
  return analogRead(0) < 850;
}

void loop() {
  static int state = 0;
  static unsigned long timerStart = 0;

  switch (state) {
  case 0: // Waiting for initial hit
    if (hit()) {
      state = 1;
      timerStart = millis();
    }
    break;

  case 1: // Got initial hit.  Must last three seconds to count
    if (millis() - timerStart > 3000) {
      state = 2;  // Long enough
      // I'd put a couple of short beeps in here to let them know the three seconds have passed.
      }
    else
      if (!hit())
        state = 0;  // Not long enough.  Start over.
    break;

  case 2: // Got initial 3-second hit.  Wait for it to go away
    if (!hit()) {
      state = 3;
      timerStart = millis();  // Five minute timer starts now
    }
    break;

  case 3: //  In the timer
    if (hit())
      state = 4;  // Winner!
    else
      if (millis() - timerStart > 5*60*1000UL) {
        // Ran out of time
        digitalWrite(buzzPin, HIGH); // turns buzzer on
        delay(1000); // waits for 1 second
        digitalWrite(buzzPin, LOW); // turns buzzer off
        state = 4;
      }
    break;

  case 4:  // Won
     // I'd put five short beeps in here to let them know they have won
    state = 0;  // Start over
    break;
  }
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino N00b
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

wow that's perfect!! THank you so much! I have have one more question instead of adding a beep to let you know it's engaged how would you have it trigger an led instead?
Logged

Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino N00b
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

would I just be  something like

Code:
digitalWrite(ledPin, HIGH);
delay(3000);
 digitalWrite(ledPin, LOW)           
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

would I just be  something like

Code:
digitalWrite(ledPin, HIGH);
delay(3000);
 digitalWrite(ledPin, LOW)           

Yes, that would light the LED for three seconds.

Another choice would be to light the LED at state=2 and turn it off at state=4.  The LED would then come on when the timer was triggered and off again at the end of the game.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: