Light Gate using Arduino
Let's create a light gate using Arduino.
Components and supplies
1
Photo resistor
1
Flashlight / Torch
5
Jumper wires (generic)
1
Breadboard (generic)
1
Arduino UNO
1
LED (generic)
1
Resistor 330 ohm
Tools and machines
1
Scissor, Electrician
1
Black Paper Sheet
Apps and platforms
1
Arduino IDE
Project description
Code
Arduino Code
arduino
1int timer = 0; //For the counting of the breaked time of light. 2bool 3 checking = false; //Checks if the the object is being monitored. 4float speed 5 = 0; //The final speed of the object. 6float lengthOfBreaker = 0.04; //The length 7 of the breaker used in the object. 8 9void setup() { 10 pinMode(8, OUTPUT); 11 //An LED for showing the 'checking' variable. 12 Serial.begin(9600); //Begin the 13 serial communication. 14} 15 16void loop() { 17 if (analogRead(A0) < 900) { 18 //Checks if the light intensity is below 900. That means the light is breaked. 19 20 checking = true; 21 digitalWrite(8, checking); 22 timer = 0; 23 while 24 (analogRead(A0) <= 900) { //Increment the timer until the object is gone. 25 timer++; 26 27 delay(1); //A fixed time of 1 millisecond is given for counting. 28 } 29 30 if (timer > 0) { //Check if the timer value is greater that 0 milliseconds. 31 32 checking = false; //Set the completed variable to true since the checking 33 is completed. 34 digitalWrite(8, checking); 35 float timeInSec = (float(timer) 36 / float(1000)); //Convert the time in milliseconds to seconds. Output is a float 37 value. 38 speed = lengthOfBreaker / timeInSec; //Calculates the speed of the 39 object by using this formula: Speed = Distance / Time 40 Serial.print("Object 41 Instantaneous Speed: "); // 42 Serial.print(speed); // 43 Print the object speed 44 Serial.print(" m/s"); // 45 in the Serial monitor. 46 Serial.print("\ 47"); // 48 49 timeInSec = 0; // Reset the seconds value. 50 } 51 speed = 0; // 52 53 checking = false; // Reset other values too. 54 timer = 0; // 55 56 } 57}
Arduino Code
arduino
1int timer = 0; //For the counting of the breaked time of light. 2bool checking = false; //Checks if the the object is being monitored. 3float speed = 0; //The final speed of the object. 4float lengthOfBreaker = 0.04; //The length of the breaker used in the object. 5 6void setup() { 7 pinMode(8, OUTPUT); //An LED for showing the 'checking' variable. 8 Serial.begin(9600); //Begin the serial communication. 9} 10 11void loop() { 12 if (analogRead(A0) < 900) { //Checks if the light intensity is below 900. That means the light is breaked. 13 checking = true; 14 digitalWrite(8, checking); 15 timer = 0; 16 while (analogRead(A0) <= 900) { //Increment the timer until the object is gone. 17 timer++; 18 delay(1); //A fixed time of 1 millisecond is given for counting. 19 } 20 if (timer > 0) { //Check if the timer value is greater that 0 milliseconds. 21 checking = false; //Set the completed variable to true since the checking is completed. 22 digitalWrite(8, checking); 23 float timeInSec = (float(timer) / float(1000)); //Convert the time in milliseconds to seconds. Output is a float value. 24 speed = lengthOfBreaker / timeInSec; //Calculates the speed of the object by using this formula: Speed = Distance / Time 25 Serial.print("Object Instantaneous Speed: "); // 26 Serial.print(speed); // Print the object speed 27 Serial.print(" m/s"); // in the Serial monitor. 28 Serial.print("\ 29"); // 30 timeInSec = 0; // Reset the seconds value. 31 } 32 speed = 0; // 33 checking = false; // Reset other values too. 34 timer = 0; // 35 } 36}
Downloadable files
Circuit Diagram
Circuit Diagram

Comments
Only logged in users can leave comments