Hi everybody.
I am working on my robot, and i want to add a ultrasonic distance sensor( a HC-SR04 ) i already have one installed to avoid objects.
Now i want to add a HC-SR04 to measure the distance between the robot and the floor to prevent it falling from the stairs or my desk.
but it isn't working out that well so if somebody could help me out i would be very happy.
#include "Ultrasonic.h"
#define TRIG_PIN 45 // object avoiding HC-SR04
#define ECHO_PIN 44 // object avoiding HC-SR04
#define TRIG_PIN2 43 // preventing from falling HC-SR04
#define ECHO_PIN2 42 // preventing from falling HC-SR04
Ultrasonic OurModule2(TRIG_PIN2, ECHO_PIN2); // preventing from falling module
Ultrasonic OurModule(TRIG_PIN, ECHO_PIN); // object sensing module
#define PwmPinMotorA 49 // this is for the motor controller from dagu
#define PwmPinMotorB 50
#define DirectionPinMotorA 48
#define DirectionPinMotorB 51
int pos = 0; // servo's position
#include <Servo.h>
Servo myservo; // servo 1
Servo myservo2; // servo 2
int distance = 0;
int distance2 = 0;
int distanceLeft = 0;
int distanceRight = 0;
int state = 1; // starting state for the switchcase
void setup() {
Serial.begin(9600);
pinMode(PwmPinMotorA, OUTPUT);
pinMode(PwmPinMotorB, OUTPUT);
pinMode(DirectionPinMotorA, OUTPUT);
pinMode(DirectionPinMotorB, OUTPUT);
myservo.attach(10);
myservo2.attach(8);
}
void forward(){ // driving forward
digitalWrite(DirectionPinMotorA, HIGH);
analogWrite(PwmPinMotorA, 150);
digitalWrite(DirectionPinMotorB, LOW);
analogWrite(PwmPinMotorB, 150);
}
void backword(){ // driving backwords
digitalWrite(DirectionPinMotorA, LOW);
analogWrite(PwmPinMotorA, 150);
digitalWrite(DirectionPinMotorB, HIGH);
analogWrite(PwmPinMotorB, 150);
}
void left(){ // turning left
digitalWrite(DirectionPinMotorA, HIGH);
analogWrite(PwmPinMotorA, 150);
digitalWrite(DirectionPinMotorB, HIGH);
analogWrite(PwmPinMotorB, 150);
delay(300);
digitalWrite(DirectionPinMotorA, LOW);
analogWrite(PwmPinMotorA, 0);
digitalWrite(DirectionPinMotorB, LOW);
analogWrite(PwmPinMotorB, 0);
}
void right(){ // turning right
digitalWrite(DirectionPinMotorA, LOW);
analogWrite(PwmPinMotorA, 255);
digitalWrite(DirectionPinMotorB, LOW);
analogWrite(PwmPinMotorB, 255);
delay(300);
digitalWrite(DirectionPinMotorA, LOW);
analogWrite(PwmPinMotorA, 0);
digitalWrite(DirectionPinMotorB, LOW);
analogWrite(PwmPinMotorB, 0);
}
void pause(){ // standing still
digitalWrite(DirectionPinMotorA, LOW);
analogWrite(PwmPinMotorA, 0);
digitalWrite(DirectionPinMotorB, LOW);
analogWrite(PwmPinMotorB, 0);
}
void Move() // moving system for the switchcase
{
distance = OurModule.Ranging(CM);
distance2 = OurModule2.Ranging(CM);
if (distance < 35)
{
state = 2;
pause();
}
else
{
forward();
}
if (distance2 <= 6 ){
state = 3;
pause();
}
else
{
forward();
}
}
void Determine() // determining witch way it has to turn system
{
myservo.write(30);
delay(400);
distanceLeft = OurModule.Ranging(CM);
delay(20);
myservo.write(130);
delay(400);
distanceRight = OurModule.Ranging(CM);
delay(20);
myservo.write(80);
if (distanceLeft <= distanceRight)
{
right();
}
else
{
left();
}
state = 1;
}
void Turn180() {
left();
delay(3000);
pause();
state = 1;
pause();
}
void loop() {
myservo2.write(110); // this is only for the second servo, don't mind it isn't importend
switch (state)
{
case 1: // 1 is moving
Move();
break;
case 2: // 2 is Determine
Determine();
break;
case 3:
Turn180();
break;
default:
pause();
break;
}
}