Input Devices
Hi pals?..this week I`m required to measure something by adding a sensor into the microcontroller board I created in Electronics Design..I had many ideas in mind but I had to pull out something quick...I decided to use Ultrasonic waves so
as to measure distance and blink an LED when its less than 10cm..On this one I used Arduino for efficiency but next time I`ll enact more Languages for consistency in skill..
This is how I went about it:
Okay the code I used is pretty simple as shown below:
#define trigPin 5
#define echoPin 7
#define led 8
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 10) { // This is where the LED On/Off happens
digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
}
else {
digitalWrite(led,LOW);
}
delay(500);
}