|
|
@ -6,9 +6,9 @@ |
|
|
|
*/ |
|
|
|
|
|
|
|
// defines pins numbers
|
|
|
|
const int trigPin = 10; |
|
|
|
const int trigPin = 12; |
|
|
|
const int echoPin = 9; |
|
|
|
const int response = 12; |
|
|
|
const int response = 13; |
|
|
|
const int in1 = 2; |
|
|
|
const int in2 = 4; |
|
|
|
const int in3 = 7; |
|
|
@ -18,7 +18,7 @@ const int leftSpeed = 3; |
|
|
|
|
|
|
|
// defines variables
|
|
|
|
long duration; |
|
|
|
int distance; |
|
|
|
int distance, distanceCm; |
|
|
|
|
|
|
|
void clearUltrasonic(){ |
|
|
|
// Clears the trigPin
|
|
|
@ -44,6 +44,20 @@ Serial.begin(9600); // Starts the serial communication |
|
|
|
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
|
|
|
|
void step(/*boolean dir,*/int steps){ |
|
|
|
// digitalWrite(dirPin1,dir);
|
|
|
@ -99,20 +113,12 @@ void motor_stop(){ |
|
|
|
} |
|
|
|
|
|
|
|
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
|
|
|
|
distance= duration*0.034/2; |
|
|
|
distance= getDistance(echoPin, trigPin); |
|
|
|
|
|
|
|
// Prints the distance on the Serial Monitor
|
|
|
|
//Serial.print("Distance: ");
|
|
|
|
//Serial.println(distance);
|
|
|
|
Serial.println(distance); |
|
|
|
absolute(); |
|
|
|
if (distance < 10){ |
|
|
|
motor_stop(); |
|
|
@ -122,6 +128,5 @@ else{ |
|
|
|
forward(); |
|
|
|
digitalWrite(response, LOW); |
|
|
|
} |
|
|
|
clearUltrasonic(); |
|
|
|
delay(1500); |
|
|
|
delay(1000); |
|
|
|
} |
|
|
|