|
@ -6,9 +6,9 @@ |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
|
// defines pins numbers
|
|
|
// defines pins numbers
|
|
|
const int trigPin = 10; |
|
|
const int trigPin = 12; |
|
|
const int echoPin = 9; |
|
|
const int echoPin = 9; |
|
|
const int response = 12; |
|
|
const int response = 13; |
|
|
const int in1 = 2; |
|
|
const int in1 = 2; |
|
|
const int in2 = 4; |
|
|
const int in2 = 4; |
|
|
const int in3 = 7; |
|
|
const int in3 = 7; |
|
@ -18,7 +18,7 @@ const int leftSpeed = 3; |
|
|
|
|
|
|
|
|
// defines variables
|
|
|
// defines variables
|
|
|
long duration; |
|
|
long duration; |
|
|
int distance; |
|
|
int distance, distanceCm; |
|
|
|
|
|
|
|
|
void clearUltrasonic(){ |
|
|
void clearUltrasonic(){ |
|
|
// Clears the trigPin
|
|
|
// Clears the trigPin
|
|
@ -44,6 +44,20 @@ Serial.begin(9600); // Starts the serial communication |
|
|
clearUltrasonic(); |
|
|
clearUltrasonic(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int getDistance(int echoPin, int trigPin){ |
|
|
|
|
|
clearUltrasonic(); |
|
|
|
|
|
// Sets the trigPin on HIGH state for 10 micro seconds
|
|
|
|
|
|
digitalWrite(trigPin, HIGH); |
|
|
|
|
|
delayMicroseconds(10); |
|
|
|
|
|
digitalWrite(trigPin, LOW); |
|
|
|
|
|
|
|
|
|
|
|
// Reads the echbackwardoPin, returns the sound wave travel time in microseconds
|
|
|
|
|
|
duration = pulseIn(echoPin, HIGH); |
|
|
|
|
|
distanceCm = duration * 0.0340 / 2; |
|
|
|
|
|
|
|
|
|
|
|
return distanceCm; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
//Stepper Function
|
|
|
//Stepper Function
|
|
|
void step(/*boolean dir,*/int steps){ |
|
|
void step(/*boolean dir,*/int steps){ |
|
|
// digitalWrite(dirPin1,dir);
|
|
|
// digitalWrite(dirPin1,dir);
|
|
@ -99,20 +113,12 @@ void motor_stop(){ |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void loop() { |
|
|
void loop() { |
|
|
// Sets the trigPin on HIGH state for 10 micro seconds
|
|
|
|
|
|
analogWrite(trigPin, HIGH); |
|
|
|
|
|
delayMicroseconds(10); |
|
|
|
|
|
analogWrite(trigPin, LOW); |
|
|
|
|
|
|
|
|
|
|
|
// Reads the echbackwardoPin, returns the sound wave travel time in microseconds
|
|
|
|
|
|
duration = pulseIn(echoPin, HIGH); |
|
|
|
|
|
|
|
|
|
|
|
// Calculating the distance
|
|
|
// Calculating the distance
|
|
|
distance= duration*0.034/2; |
|
|
distance= getDistance(echoPin, trigPin); |
|
|
|
|
|
|
|
|
// Prints the distance on the Serial Monitor
|
|
|
// Prints the distance on the Serial Monitor
|
|
|
//Serial.print("Distance: ");
|
|
|
//Serial.print("Distance: ");
|
|
|
//Serial.println(distance);
|
|
|
Serial.println(distance); |
|
|
absolute(); |
|
|
absolute(); |
|
|
if (distance < 10){ |
|
|
if (distance < 10){ |
|
|
motor_stop(); |
|
|
motor_stop(); |
|
@ -122,6 +128,5 @@ else{ |
|
|
forward(); |
|
|
forward(); |
|
|
digitalWrite(response, LOW); |
|
|
digitalWrite(response, LOW); |
|
|
} |
|
|
} |
|
|
clearUltrasonic(); |
|
|
delay(1000); |
|
|
delay(1500); |
|
|
|
|
|
} |
|
|
} |
|
|