const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
distance = distance + 1;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println("cm");
}
so i took the ruler and aligned along with ultrasonic and placed a simple obstacle and it measured accurately
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;
the following is setup function this section is to set the pinmode of the pins whether it is input or output
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
this section is loop function where i implemented the formula i mentioned above and process the output
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
distance = distance + 1;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println("cm");
}
so for this input device i have integrated it with leds to make it more applicable
#include
#ifdef __AVR__
#include // Required for 16 MHz Adafruit Trinket
#endif
const int trigPin = 32;
const int echoPin = 35;
#define PIN 33
#define NUMPIXELS 4 // Popular NeoPixel ring size
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#define DELAYVAL 500
long duration;
int distance;
int i0 = 0;
int i1 = 1;
int i2 = 2;
int i3 = 3;
void setup() {
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
Serial.begin(9600);
pixels.begin();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Serial.println(distance);
// delay(600);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
distance = distance + 1;
pixels.clear();
if (distance <= 2){
pixels.setPixelColor(i0, pixels.Color(0, 200, 0));
pixels.show();
}
if (distance > 2 && distance <=4){
pixels.setPixelColor(i0, pixels.Color(0,0, 200));
pixels.show();
}
if (distance > 4 && distance <=6){
pixels.setPixelColor(i0, pixels.Color(200,0, 0));
pixels.show();
}
if (distance > 6 && distance <=8){
pixels.setPixelColor(i1, pixels.Color(0, 200, 0));
pixels.show();
}
if (distance > 8 && distance <=10){
pixels.setPixelColor(i1, pixels.Color(0,0, 200));
pixels.show();
}
if (distance > 10 && distance <=12){
pixels.setPixelColor(i1, pixels.Color(200,0, 0));
pixels.show();
}
if (distance > 12 && distance <=14){
pixels.setPixelColor(i2, pixels.Color(0, 200, 0));
pixels.show();
}
if (distance > 14 && distance <=16){
pixels.setPixelColor(i2, pixels.Color(0,0, 200));
pixels.show();
}
if (distance > 16 && distance <=18){
pixels.setPixelColor(i2, pixels.Color(200,0, 0));
pixels.show();
}
if (distance > 18 && distance <=20){
pixels.setPixelColor(i3, pixels.Color(0, 200, 0));
pixels.show();
}
if (distance > 20 && distance <=22){
pixels.setPixelColor(i3, pixels.Color(0,0, 200));
pixels.show();
}
if (distance > 22 && distance <=25){
pixels.setPixelColor(i3, pixels.Color(200,0, 0));
pixels.show();
}
}
these codes i made works as i mentioned above where leds shifts and change colors acording to the distance measured by ultrasonic sensor