arduino‎ > ‎

sensor

Laser Target Tutorial

In this tutorial you'll create the target circuit for your laser shooting game! When you shoot the target an LED will change color. Below you'll find a diagram and the code.


WARNING Lasers can cause permanent damage to your eyes. DO NOT look directly into the laser when on or point it at another person's face.


Set up

  1. Open your Arduino software on your laptop

  2. Use a USB wire to connect your Arduino board to your computer

  3. Select the Arduino under Tools > Port


Schematic

Photoresistor in A4 and 3.3V
Resistor in A4 and GND
LED in A0 and A1


Code

 
int ldrPin = A4;
long ldrValue1, ldrValue2;

void setup(void) {
   pinMode (ldrPin,INPUT);
   pinMode (A0, OUTPUT);
   pinMode (A1, OUTPUT);
}
 
void loop(void) {
  ldrValue1 = analogRead(ldrPin);  
  delay(10);
  ldrValue2 = analogRead(ldrPin);  

  if (ldrValue1-ldrValue2 > 20||ldrValue2-ldrValue1 > 20){
  digitalWrite(A0,HIGH);
  digitalWrite(A1,LOW);
  delay(1000);
  }
  else{
    digitalWrite(A0,LOW);
    digitalWrite(A1,HIGH);
  }
}