Once again, the code is very simple:
#define MOTOR_OFF B00000000;
#define MOTOR_FWD B10010000;
#define MOTOR_REV B01100000;
#define MOTOR_LFT B10100000;
#define MOTOR_RHT B01010000;
#include "Ultrasonic.h"
Ultrasonic ultrasonic(12,13);
int distance = 0;
const int maxDistance = 100;
const int range = 25;
void setup() {
PORTD = MOTOR_OFF;
delay(500);
PORTD = MOTOR_FWD;
Serial.begin(9600);
}
void loop() {
distance = ultrasonic.Ranging(CM);
if(distance < range) {
PORTD = MOTOR_OFF;
do {
distance = ultrasonic.Ranging(CM);
if(distance > maxDistance) {
delay(5);
distance = ultrasonic.Ranging(CM);
continue;
}
PORTD = MOTOR_REV;
} while(distance < range);
PORTD = MOTOR_OFF;
PORTD = MOTOR_LFT;
delay(1000);
PORTD = MOTOR_OFF;
PORTD = MOTOR_FWD;
}
}
No comments:
Post a Comment