0% found this document useful (0 votes)
30 views8 pages

ROBOTIC

The document contains multiple Arduino code snippets for controlling LEDs, interfacing with an IR proximity sensor, and measuring distance using an ultrasonic sensor. It includes setup and loop functions for each application, detailing how to initialize pins and respond to sensor inputs. Additionally, it provides a distance measurement code that activates different LEDs based on proximity and outputs the distance to the serial monitor.

Uploaded by

pankaj rangaree
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)
30 views8 pages

ROBOTIC

The document contains multiple Arduino code snippets for controlling LEDs, interfacing with an IR proximity sensor, and measuring distance using an ultrasonic sensor. It includes setup and loop functions for each application, detailing how to initialize pins and respond to sensor inputs. Additionally, it provides a distance measurement code that activates different LEDs based on proximity and outputs the distance to the serial monitor.

Uploaded by

pankaj rangaree
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/ 8

 MULTIPLE BLINKING LED

1) int led = 13;


2) int led2 = 12;
3) int led3 = 11;
4)
5) // the setup routine runs once when you press reset:
6) void setup() {
7) // initialize the digital pin as an output.
8) pinMode(led, OUTPUT);
9) pinMode(led2, OUTPUT);
10) pinMode(led3, OUTPUT);
11) }
12)
13) // the loop routine runs over and over again forever:
14) void loop() {
15) digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
16) delay(100); // wait for a second
17) digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
18) delay(100);
19) {digitalWrite(led2, HIGH);
20) delay(100);
21) digitalWrite(led2, LOW);
22) delay(100);}
23) {digitalWrite(led3, HIGH);
24) delay(100);
25) digitalWrite(led3, LOW);
26) delay(100);}// wait for a second
27) }

 IR PROXIMITY SENSOR INTER FACE CODE


 /*
 IR Proximity Sensor interface code
 Turns on an LED on when obstacle is detected, else off.
 blog.circuits4you.com 2016
 */


 const int ProxSensor=2;

 void setup() {
 // initialize the digital pin as an output.
 // Pin 13 has an LED connected on most Arduino boards:
 pinMode(13, OUTPUT);
 //Pin 2 is connected to the output of proximity sensor
 pinMode(ProxSensor,INPUT);
 }

 void loop() {
 if(digitalRead(ProxSensor)==HIGH) //Check the sensor output
 {
 digitalWrite(13, HIGH); // set the LED on
 }
 else
 {
 digitalWrite(13, LOW); // set the LED off
 }
 delay(100); // wait for a second
 }

 ULTRASONIC DISTANCE SENSOR


 void setup() {
 //Serial Port begin
 Serial.begin (9600);
 //Define inputs and outputs
 pinMode(trigPin, OUTPUT);int trigPin = 4; //Trig - green Jumper
 int echoPin = 2; //Echo - yellow Jumper
 long duration, cm, inches;

 pinMode(echoPin, INPUT);
 }
 void loop()
 {
 // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
 // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
 digitalWrite(trigPin, LOW);
 delayMicroseconds(5);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);
 // Read the signal from the sensor: a HIGH pulse whose
 // duration is the time (in microseconds) from the sending
 // of the ping to the reception of its echo off of an object.
 pinMode(echoPin, INPUT);
 duration = pulseIn(echoPin, HIGH);
 // convert the time into a distance
 cm = (duration/2) / 29.1;
 inches = (duration/2) / 74;
 Serial.print("Distance: ");
 Serial.print(inches);
 Serial.print("in, ");
 Serial.print(cm);
 Serial.print("cm");
 Serial.println();

 delay(250);
 }

 Aurdino diagram

 int trigPin = 4; //Trig - green Jumper


 int echoPin = 2; //Echo - yellow Jumper
 long duration, cm, inches;
 void setup() {
 //Serial Port begin
 Serial.begin (9600);
 //Define inputs and outputs
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 }
 void loop()
 {
 // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
 // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
 digitalWrite(trigPin, LOW);
 delayMicroseconds(5);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);
 // Read the signal from the sensor: a HIGH pulse whose
 // duration is the time (in microseconds) from the sending
 // of the ping to the reception of its echo off of an object.
 pinMode(echoPin, INPUT);
 duration = pulseIn(echoPin, HIGH);
 // convert the time into a distance
 cm = (duration/2) / 29.1;
 inches = (duration/2) / 74;
 Serial.print("Distance: ");
 Serial.print(inches);
 Serial.print("in, ");
 Serial.print(cm);
 Serial.print("cm");
 Serial.println();

 delay(250);
 }

/*
IR Proximity Sensor interface code
Turns on an LED on when obstacle is detected, else off.
blog.circuits4you.com 2016
*/
const int ProxSensor=2;

void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
//Pin 2 is connected to the output of proximity sensor
pinMode(ProxSensor,INPUT);
}

void loop() {
if(digitalRead(ProxSensor)==HIGH) //Check the sensor output
{
digitalWrite(13, HIGH); // set the LED on
}
else
{
digitalWrite(13, LOW); // set the LED off
}
delay(100); // wait for a second
}
 CODE OF DISTANCE MEASURE WITH AURDINO AND LED

#define trigPin 7
#define echoPin 6
#define led 13
#define led2 12
#define led3 11
#define led4 10
#define led5 9
#define led6 8
#define buzzer 3

int sound = 250;

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(buzzer, OUTPUT);

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;

if (distance <= 30) {


digitalWrite(led, HIGH);
sound = 250;
}
else {
digitalWrite(led,LOW);
}
if (distance < 25) {
digitalWrite(led2, HIGH);
sound = 260;
}
else {
digitalWrite(led2, LOW);
}
if (distance < 20) {
digitalWrite(led3, HIGH);
sound = 270;
}
else {
digitalWrite(led3, LOW);
}
if (distance < 15) {
digitalWrite(led4, HIGH);
sound = 280;
}
else {
digitalWrite(led4,LOW);
}
if (distance < 10) {
digitalWrite(led5, HIGH);
sound = 290;
}
else {
digitalWrite(led5,LOW);
}
if (distance < 5) {
digitalWrite(led6, HIGH);
sound = 300;
}
else {
digitalWrite(led6,LOW);
}

if (distance > 30 || distance <= 0){


Serial.println("Out of range");
noTone(buzzer);
}
else {
Serial.print(distance);
Serial.println(" cm");
tone(buzzer, sound);

}
delay(500);
}

You might also like