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);
}
Do'stlaringiz bilan baham: |