Bryce Lukens
Published

Parking... with Lasers!

An Arduino Uno project which uses lasers to read and indicate if your car is parked correctly within a zone.

IntermediateFull instructions provided2,806
Parking... with Lasers!

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
KY-008 Laser Dot Diode Copper Head Sensor Module
×1
Icstation 5V Light Sensor Module Receiver for Arduino, High Level Output
×1
LED (generic)
LED (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Circuit Diagram for Parking with Lasers

Diagram for Arduino and laser layout

Circuit Diagram for Parking with Lasers JPG

Code

Laser_final2.1

Arduino
lasers and sensors to light an LED when both beams are interrupted
//first step is to define all variables and their associated pins and include any libraries
#include <LowPower.h> //in order to save power I have included the low power library which will turn almost everything on the arduino off for 8 seconds
#define DETECTH 2 //DETECTH is the output of the sensor on the higher part of the driveway and it is linked to pin 2
#define DETECTHP 12 //DETECTHP is the power for the sensor at the higher part of the driveway and it is linked to pin 12
#define ACTIONH 8 //ACTIONH is the power of the higher laser and is linked to pin 8
#define DETECTL 9 //DETECTL is the output of the lower sensor and is linked to pin 4
#define DETECTLP 13 //DETECTLP is the power of the lower sensor and is linked to pin 13
#define ACTIONL 10 //ACTIONL is the power to the lower laser and is linked to pin 10
int detectedH2 = digitalRead(DETECTH); //the variable detectedH2 represents the value of the output of the higher sensor
#define LED 7 //the variable LED represents the pin the positive side of the led is linked to.
 
void setup() {
Serial.begin(9600); //Serial.begin is required in order for a connection between a computer and the arduino to be possible, in this case the connection is on 9600 baud
Serial.print("Test"); //to make sure the connection is working I asked the arduino to print test to the computer
Serial.println(); //println breaks up the serial messages so that it they are on seperate lines
pinMode(DETECTH,INPUT); //DETECTH needs to be set to input as it sends a signal to the arduino
pinMode(ACTIONH, OUTPUT); //ACTIONH needs to receive energy from the arduino so the pin must be set to output
pinMode(DETECTL, INPUT); //DETECTL is set to input as it is a pint on which signals are sent to the arduino
pinMode(ACTIONL, OUTPUT); //ACTIONL needs to receive energy from the arduino so the pin must be set to output
pinMode(DETECTHP, OUTPUT);//DETECTHP needs to receive energy from the arduino so the pin must be set to output
pinMode(DETECTLP, OUTPUT);//DETECTLP needs to receive energy from the arduino so the pin must be set to output
}

void loop() {
 LowPower.idle(SLEEP_8S, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF,SPI_OFF, USART0_OFF, TWI_OFF); //low power mode is the first command given to the arduino and will turn off any components not neccessary to the arduino's function for 8 seconds
Serial.print("STEP 1"); // The arduino is asked to say step 1 here in order to make sure that it has exited low power mode
 Serial.println();//println breaks up the serial messages so that it they are on seperate lines
 for(int h; h<30; h++){ // I put a small for loop here to make sure that there is enough time between leaving low power mode and starting regular operations
 Serial.print("do nothing"); // in order to make sure the arduino enters the for loop 
 Serial.println();//println breaks up the serial messages so that it they are on seperate lines
 }
 int detectedL2 = digitalRead(DETECTL); //after exiting the forloop we define the integer detectedL2 which is just a word that replaces the DigitalRead of the lower sensor, when we call on this variable in the code we will get a value back that is high or low
digitalWrite (ACTIONL, HIGH); //here pin 10 is told to give power to the lower laser's power pin
digitalWrite (DETECTLP, HIGH); //here pin 13 is told to give power to the Lower sensor's power pin
if (detectedL2 == HIGH){ //here we enter the if statement that will only occur if the lower sensor sends a high signal along pin 4, which will only happen if the sensor sees the laser 
  Serial.print ("Bottom sensor is seeing laser"); //here we tell the arduino to send a message to the computer via Serial, that indicates us that it is in the if statement
    Serial.println();
digitalWrite(DETECTHP, LOW); //here we tell pin 12 to cut the power to the power pin on the higher sensor
digitalWrite(ACTIONH, LOW);  //here we tell pin 8 to cut the power to the power pin on the higher laser
digitalWrite(LED, LOW); // here we tell pin 7 to cut the power to the positive node of the LED           
}//ultimately this loop is here as a power saver
else{ // now we enter an else loop, this loop will only occur if the opposite event of the if loop occurs, in this case if the bottom sensor does not see the laser then enter this loop instead of the if loop
 digitalWrite(ACTIONH, HIGH); //here we tell pin 8 to give the higher laser power 
digitalWrite(DETECTHP, HIGH);//here we tell pin 12 to give power to the higher sensor
Serial.print("LASER cannot be seen by bottom sensor"); //here we tell the arduino to send a message to the computer that indicates that we have entered the else loop
  Serial.println();
for(int i = 0, j = 1; i < 20000; i++) //here we enter a for loop which will last for as long as the condition inside the parenthesis is true, in this case until i>j the loop will go continually, we also set i to zero and j to 1, also at the end of each loop we add 1 to i
    {
  int detectedH2 = digitalRead(DETECTH); //this integer takes the place of a digitalRead for the higher sensor throughout the for loop
  int detectedL2 = digitalRead(DETECTL); // here we redefine one integer and define 1 new integer because a for loop ignores any integer outside of it
    Serial.print ("for loop");// here we tell the arduino to send a message to the computer, this indicates we have entered the for loop or are still in it
    Serial.println();
    if(detectedH2 == HIGH)//here we have an if loop in a for loop that will occur as long as the higher sensor sees the higher laser
    {
    digitalWrite(ACTIONL, LOW); //during this for loop we want to conserve power and thus turn off all other unneccesary peripherals
    digitalWrite(DETECTLP, LOW);
    j++; //here we tell the arduino to 1 to j to force the condition of i<j to stay true
    Serial.print("j++"); //here we tell the arduino to send a message to the computer indicating we have added to j
    }
    else //here we have an else loop that is the opposite condition of the if loop, the condition is that the higher sensor does not see the laser, if this occurs then enter the else loop
    {
    digitalWrite(ACTIONH, LOW); //here we turn more peripherals off to conserve power
    digitalWrite(DETECTHP, LOW);
  digitalWrite(LED,HIGH);//here we turn the LED on in order to indicate to the driver that they are parked in the correct position
  Serial.print("NO LASER"); //we also print a message to the computer to indicate that we are in the else loop
  Serial.println();
  delay(20000);// we include a delay here to keep the LED on for 8 seconds
  digitalWrite(LED,LOW); //we then turn the LED off to conserve energy prior to low power mode occuring again
 break;
   }
    }
}
Serial.print("End");
Serial.println();
}

Credits

Bryce Lukens
1 project • 0 followers

Comments