Inspiration
Car sensors. Whenever an object or car comes very close to our car, it starts beeping and lighting. That was my motivation to create this sensor.
Video/Image
Work
Basically, as an object comes closer to the Ultrasonic sensor, the bulb lights. For the switch, I added a bulb connected switch- simply press the button.
Challenges
I had many issues with this assignment. It was mainly faulty jumper wires and Arduino’s functioning on Mac. At first, Changing jumper wires solved the issue of Ultrasonic sensor’s distance capture. I spent majority time on solving this 'avrdude: stk500_recv():'
error. Re-installed the drivers, added new libraries, etc and magically it just started working!
Code
#include <NewPing.h> const int trig = 12; const int echo = 13; const int led = 2; int duration = 0; int distance = 0; void setup() { pinMode(trig , OUTPUT); pinMode(echo , INPUT); pinMode(led , OUTPUT); Serial.begin(9600); } void loop() { digitalWrite(trig , HIGH); delayMicroseconds(200); digitalWrite(trig , LOW); duration = pulseIn(echo , HIGH); distance = (duration/2) ; Serial.println(distance); if ( distance <= 10 ) { digitalWrite(led, HIGH); } else { digitalWrite(led, LOW); } }