مشروع الحلقة 5: تصنيع جهاز قياس المسافات

نص البرنامج (الكود):

 

/*
* Arduino-Distance Sensor-Darah
* Darahmakers.com
* Ultrasonic Sensor HC-SR04 and Arduino Tutorial
* Arduino-20×04-LiquidCrystal_I2C
*/

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address

const int trigPin = 9;
const int echoPin = 10;

long duration;
float distanceCm;
float distanceMeter;

void setup() {
lcd.begin(20,4);

lcd.setCursor(4,0);
lcd.print(“*************”);

lcd.setCursor(2,1);
lcd.print(” Wellcome to “);

lcd.setCursor(0,2);
lcd.print(“Distance Measurement”);

lcd.setCursor(2,3);
lcd.print(“*************”);

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
delay(2000);
}

void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distanceCm= duration*0.034/2;
distanceMeter= distanceCm/100;

if ( distanceCm < 1000){
lcd.setCursor(0,1);
lcd.print(“Distance: “);
lcd.print(distanceCm);
lcd.print(” cm”);

Serial.print(“Distance: “);
Serial.print(distanceCm);
Serial.println(” cm”);

lcd.setCursor(0,2);
lcd.print(“Distance: “);
lcd.print(distanceMeter);
lcd.print(” meter”);

Serial.print(“Distance: “);
Serial.print(distanceMeter);
Serial.println(” meter”);}
else {

lcd.setCursor(0,1);
lcd.print(” ERORR !! “);
lcd.setCursor(0,2);
lcd.print(” ERORR !! “);

Serial.println(“ERORR !!”);
}

delay(1000);

}