#include <AFMotor.h>
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
const int TRIG_PIN = 6; // Arduino pin connected to Ultrasonic Sensor's TRIG pin
const int ECHO_PIN = 7; // Arduino pin connected to Ultrasonic Sensor's ECHO pin
const int DISTANCE_THRESHOLD = 20; // centimeters
float duration_us, distance_cm;
void setup() {
Serial.begin (9600); // initialize serial port
pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode
pinMode(ECHO_PIN, INPUT); // set arduino pin to input mode
// put your setup code here, to run once:
motor1.setSpeed(255); //1R MOTORIIN HURD
motor2.setSpeed(168); //2R MOTORIIN HURD
}
void loop() {
uragshaa();
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration_us = pulseIn(ECHO_PIN, HIGH);
distance_cm = 0.017 * duration_us;
if(distance_cm < DISTANCE_THRESHOLD)
{
zogs();
}
else
{
uragshaa();
}
Serial.print("distance: ");
Serial.print(distance_cm);
Serial.println(" cm");
delay(500);
}
void uragshaa() {
motor1.run(FORWARD);
motor2.run(FORWARD);
}
void hoishoo() {
motor1.run(BACKWARD);
motor2.run(BACKWARD);
}
void zogs() {
motor1.run(RELEASE);
motor2.run(RELEASE);
}
void baruun() {
motor1.run(FORWARD);
motor2.run(RELEASE);
}
void zuun() {
motor1.run(RELEASE);
motor2.run(FORWARD);
}
No comments:
Post a Comment