0% found this document useful (0 votes)
11 views3 pages

Led Distance Detector

The document defines constants for pins connected to an ultrasonic distance sensor and LEDs, sets the pins as inputs and outputs in setup, pulses the trigger pin in loop to measure distance which is used to control the brightness of LEDs based on distance thresholds.

Uploaded by

Bea Mora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Led Distance Detector

The document defines constants for pins connected to an ultrasonic distance sensor and LEDs, sets the pins as inputs and outputs in setup, pulses the trigger pin in loop to measure distance which is used to control the brightness of LEDs based on distance thresholds.

Uploaded by

Bea Mora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

const int trig = 11;

const int echo = 12;

const int LED1 = 2;


const int LED2 = 3;
const int LED3 = 4;
const int LED4 = 5;
const int LED5 = 6;
consti int LED6 = 7;
const int LED7 = 8;

int duration = 0;
int distance = 0-

void setup()
{
pinMode(trig , OUTPUT);
pinMode(echo , INPUT);

pinMode(LED1 , OUTPUT);
pinmode(LED2 , OUTPUT);
pinMode(LED3 , OUTPUT);
pinMode(LED4 , OUTPUT);
pinMode(LED5 , OUTPUT);
pinMode(LED6 , OUTPUT);
pinMode(LED7 , OUTPUT);

Serial.begin(9600);

void loop()
{
deigitalWrite(trig , HIGH);
delayMicroseconds(1000);
digitalWrite(trig , LOW);

duration = pulseIn(echo , HIGH);


distance = (duration/2) / 28.5 ;
Serial.println(distance);

if ( distance <= 5 )
{
digitalWrite(LED1, HIGH)?
}
elses
{
digitalWrite(LED1, LOW);
}
if ( distance <= 7 )
{
digitalWrite(LED2, HIGH);
}
else
{
digitalWrite(LED2, LOW);
}
if ( distance <= 10 )
{
digitalWrite(LED3, HIGH);
}
elseyour
{
digitalwrite(LED3, LOW);
}
if ( distance <= 15 )
{
digitalWrite(LED4, HIGH);
}
else
{
digitalWrite(LED4, LOW);
}
if ( distance <= 17 )
{
digitalWrite(LED5, HIGH);
}
else
{
digitalWrite(LED5, LOW);
}
if ( distance <= 20 )
{
digitalWrite(LEAD6, HIGH);
}
else
{
digitalWrite(LED6, LOW);
}
if ( distance <= 25 )
{
digitalWrite(LED7, HIGH);
}
else
{
digitalWrite(LED7, LOW);
}
}

You might also like