0% found this document useful (0 votes)
14 views2 pages

Activity 6 LED BLINKING

This document contains an Arduino sketch for a distance measuring system using ultrasonic sensors. It initializes pins for LEDs and the ultrasonic sensor, measures distance, and lights up different LEDs based on the measured distance in inches. The code also includes functions to convert microseconds to inches and centimeters.

Uploaded by

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

Activity 6 LED BLINKING

This document contains an Arduino sketch for a distance measuring system using ultrasonic sensors. It initializes pins for LEDs and the ultrasonic sensor, measures distance, and lights up different LEDs based on the measured distance in inches. The code also includes functions to convert microseconds to inches and centimeters.

Uploaded by

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

const int trigPin = 2;

const int echoPin = 3;

int ledA=13;
int ledB=12;
int ledC=11;
int ledD=10;

int a=4;
int b=3;
int c=2;
int d=1;

int max_distance = 200;

void setup() {
// put your setup code here, to run once:

Serial.begin(9600);
pinMode (ledA,OUTPUT);
pinMode (ledB,OUTPUT);
pinMode (ledC,OUTPUT);
pinMode (ledD,OUTPUT);

digitalWrite(ledA, LOW);
digitalWrite(ledB, LOW);
digitalWrite(ledC, LOW);
digitalWrite(ledD, LOW);

void loop() {
// put your main code here, to run repeatedly:

long duration, inches, cm;


pinMode(trigPin,OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

//cm = ( duration / 29 ) / 2;
//inches = cm * 0.393701;
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

if (inches <max_distance){
Serial.print(inches);
Serial.print(" in");
Serial.print(cm);
Serial.print(" cm");
Serial.println();
delay(100);
}
//digitalWrite (ledA,HIGH);
if (inches >=a) {digitalWrite (ledA,HIGH);digitalWrite (ledB,LOW); digitalWrite
(ledC,LOW); digitalWrite (ledD,LOW); }
else if (inches >=b){digitalWrite (ledA,LOW);digitalWrite (ledB,HIGH); digitalWrite
(ledC,LOW); digitalWrite (ledD,LOW); }
else if (inches >=c){digitalWrite (ledA,LOW);digitalWrite (ledB,LOW); digitalWrite
(ledC,HIGH); digitalWrite (ledD,LOW); }
else if (inches >=d){digitalWrite (ledA,LOW);digitalWrite (ledB,LOW); digitalWrite
(ledC,LOW); digitalWrite (ledD,HIGH); }
//else {digitalWrite (ledA,HIGH);digitalWrite (ledB,HIGH); digitalWrite
(ledC,HIGH); digitalWrite (ledD,HIGH); }

long microsecondsToInches(long microseconds) {


// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return (microseconds / 74) / 2;
}

long microsecondsToCentimeters(long microseconds) {


// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return (microseconds / 29) / 2;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy