Ultrasonic Sensorni ishlatish // Include NewPing Library



Download 371,16 Kb.
Sana17.07.2022
Hajmi371,16 Kb.
#810661
Bog'liq
Ultrasonic Sensorni ishlatish


Ultrasonic Sensorni ishlatish
// Include NewPing Library
#include "NewPing.h"
// Hook up HC-SR04 with Trig to Arduino Pin 9, Echo to Arduino pin 10
#define TRIGGER_PIN 9
#define ECHO_PIN 10
// Maximum distance we want to ping for (in centimeters).
#define MAX_DISTANCE 400
// NewPing setup of pins and maximum distance.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
float duration, distance;
void setup()
{
Serial.begin(9600);
}
void loop()
{
// Send ping, get distance in cm
distance = sonar.ping_cm();
// Send results to Serial Monitor
Serial.print("Distance = ");
if (distance >= 400 || distance <= 2)
{
Serial.println("Out of range");
}
else
{
Serial.print(distance);
Serial.println(" cm");
}
delay(50);
}

Arduino kodi - NewPing kutubxonasidan foydalanish
Ultrasonik sensorni ishga tushirish va qabul qilingan signal pulsining kengligini qo'lda o'lchash o'rniga, biz maxsus kutubxonadan foydalanamiz. Ularning bir nechtasi mavjud, eng ko'p qirrali " NewPing " deb ataladi.


// includes the LiquidCrystal Library
#include

// includes the LcdBarGraph Library


#include

// Maximum distance we want to ping for (in centimeters).


#define max_distance 200

// Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

LcdBarGraph lbg(&lcd, 16, 0, 1); // Creates an LCD Bargraph object.

const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;

void setup()


{
lcd.begin(16,2); // Initializes the interface to the LCD screen
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop()


{
// Write a pulse to the HC-SR04 Trigger Pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the response from the HC-SR04 Echo Pin
duration = pulseIn(echoPin, HIGH);
// Determine distance from duration
// Use 343 metres per second as speed of sound
distance= duration*0.034/2;
// Prints "Distance: " on the first line of the LCD
lcd.setCursor(0,0);
lcd.print("Distance: ");
lcd.print(distance);
lcd.print(" cm");

// Draws bargraph on the second line of the LCD


lcd.setCursor(0,1);
lbg.drawValue(distance, max_distance);
delay(500);
}

Harakatlanuvchi avtomobilni yig`ish va dasturlash


// Include NewPing Library


#include "NewPing.h"

// Hook up HC-SR04 with Trig to Arduino Pin 9, Echo to Arduino pin 10


#define TRIGGER_PIN A0
#define ECHO_PIN A1

// Maximum distance we want to ping for (in centimeters).


//#define MAX_DISTANCE 200

// NewPing setup of pins and maximum distance.


NewPing sonar(TRIGGER_PIN, ECHO_PIN);
float duration, distance;

const int LeftMotorForward = 6;


const int LeftMotorBackward = 7;
const int RightMotorForward = 5;
const int RightMotorBackward = 4;

void setup()


{
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
}

void loop()


{
// Send ping, get distance in cm
distance = sonar.ping_cm();
if (distance <= 10)
{
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, LOW);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, HIGH);
}
else
{
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorForward, HIGH);
}
delay(300);
}

Download 371,16 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish