Thinking about the use of LEDs in a more practical and engineering way, I came up with the idea of creating an object detection sensor, which is widely used in tesla cars, home security systems, etc.
Implementation
As an object detector, I used an ultrasonic sensor (digital input). I also used a photosensor as a sort of solar power, which enables the work of the whole system (analog input).
There are three modes to detect an object depending on its distance from the ultrasonic sensor. You can identify if the object is close or far away by looking at which LED light is turned on and which sound is produced. If an object is close, the red LED will be turned on. If an object is at a medium distance, the yellow LED light turns on, which serves as a warning. And if an object is at a normal distance, the green LED is turned on all the time.
Code
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int sensorPin = A0; // select the input pin for the potentiometer
int powerPin = 7;
int ledPin1 = 11; // red LED
int ledPin2 = 10; // yellow LED
int ledPin3 = 9; // green LED
int buzzPin = 4;
const int trigPin = 12;
const int echoPin = 13;
long duration;
int distance;
bool gameStarted = false;
int sensorValue = 0; // variable to store the value coming from the sensor
voidplaySound(int hz){
tone(buzzPin, hz);
delay(hz);
noTone(buzzPin);
}
voidsetup(){
// declare the ledPins as an OUTPUT:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(buzzPin, OUTPUT);
pinMode(sensorPin, INPUT);
pinMode(powerPin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
voidloop(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration *0.034 / 2;
digitalWrite(powerPin, HIGH);
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
//to turn on the object detection by photo sensor (solar power)
if(sensorValue <= 30){
gameStarted = false;
delay(2000);
}
//if there is no light, the system does not work
elseif(sensorValue >30){
gameStarted = true;
}
if(gameStarted){//to measure a distance
if(distance <= 5){//red led turns on at the lowest distance
// turn the ledPin on
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
playSound(200);
delay(100);
// turn the ledPin off:
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
delay(100);
}
elseif(distance >5&& distance <= 10){//yellow led turns on at the medium distance
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, LOW);
playSound(1000);
delay(100);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
delay(100);
}
elseif(distance >10){//to turn on the green led
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
delay(100);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
delay(100);
}
}
}
int sensorPin = A0; // select the input pin for the potentiometer
int powerPin = 7;
int ledPin1 = 11; // red LED
int ledPin2 = 10; // yellow LED
int ledPin3 = 9; // green LED
int buzzPin = 4;
const int trigPin = 12;
const int echoPin = 13;
long duration;
int distance;
bool gameStarted = false;
int sensorValue = 0; // variable to store the value coming from the sensor
void playSound(int hz) {
tone(buzzPin, hz);
delay(hz);
noTone(buzzPin);
}
void setup() {
// declare the ledPins as an OUTPUT:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(buzzPin, OUTPUT);
pinMode(sensorPin, INPUT);
pinMode(powerPin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
digitalWrite(powerPin, HIGH);
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
//to turn on the object detection by photo sensor (solar power)
if (sensorValue <= 30) {
gameStarted = false;
delay(2000);
}
//if there is no light, the system does not work
else if (sensorValue > 30) {
gameStarted = true;
}
if (gameStarted) { //to measure a distance
if (distance <= 5) { //red led turns on at the lowest distance
// turn the ledPin on
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
playSound(200);
delay(100);
// turn the ledPin off:
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
delay(100);
}
else if (distance > 5 && distance <= 10) { //yellow led turns on at the medium distance
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, LOW);
playSound(1000);
delay(100);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
delay(100);
}
else if (distance > 10) { //to turn on the green led
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
delay(100);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
delay(100);
}
}
}
int sensorPin = A0; // select the input pin for the potentiometer
int powerPin = 7;
int ledPin1 = 11; // red LED
int ledPin2 = 10; // yellow LED
int ledPin3 = 9; // green LED
int buzzPin = 4;
const int trigPin = 12;
const int echoPin = 13;
long duration;
int distance;
bool gameStarted = false;
int sensorValue = 0; // variable to store the value coming from the sensor
void playSound(int hz) {
tone(buzzPin, hz);
delay(hz);
noTone(buzzPin);
}
void setup() {
// declare the ledPins as an OUTPUT:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(buzzPin, OUTPUT);
pinMode(sensorPin, INPUT);
pinMode(powerPin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
digitalWrite(powerPin, HIGH);
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
//to turn on the object detection by photo sensor (solar power)
if (sensorValue <= 30) {
gameStarted = false;
delay(2000);
}
//if there is no light, the system does not work
else if (sensorValue > 30) {
gameStarted = true;
}
if (gameStarted) { //to measure a distance
if (distance <= 5) { //red led turns on at the lowest distance
// turn the ledPin on
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
playSound(200);
delay(100);
// turn the ledPin off:
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
delay(100);
}
else if (distance > 5 && distance <= 10) { //yellow led turns on at the medium distance
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, LOW);
playSound(1000);
delay(100);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
delay(100);
}
else if (distance > 10) { //to turn on the green led
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
delay(100);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
delay(100);
}
}
}
One thought on “Week 9 – object detector (kind of)”
Hi Aliya, please do not use delay to make things blink if there is interaction.
Hi Aliya, please do not use delay to make things blink if there is interaction.