Hi,
I am working on a robot that evades objects. At the start I wanted to give it a servo to check left and right to see wich direction its safe to turn to. Sadly servo broke and I dont have time to order a new one. Basically, I wanted to make the robot (has the sensor at the front) to turn right (because after the turn its still basicly on the same postion, just turned) to give it the same effect as servo checking right and left.
And if the sensor sees the path is clear, the robot should move forward. If not, it should check right side as well and go forward if its clear. If not, turn back in the initial position and go backwards, then repeat the checking again and go where its safe.
The time for the robot to turn for 90 degrees is about 0,7 seconds (700).
At the moment the code is made so that the robot always goes to the right when sees a object. I don't know how to make multiple "if" one after another.
Thanks for any help, the code is below:
//Libraries
#include "Ultrasonic.h"
//Constants
const int button = 2;
const int led = 3;
const int buzzer = 4;
const int motorA1= 6; //motor A pos
const int motorA2= 9; //motor A neg
const int motorB1=11; //motor B pos
const int motorB2=10; //motor B neg
Ultrasonic ultrasonic(A4 ,A5);
//Variables
int distance; //sensor
int function=0; //1 robot moving , 0 not moving
int buttonState=0; //button
int flag=0; //button hold
void setup()
{
pinMode(button, INPUT_PULLUP);
pinMode(led, OUTPUT);
pinMode(motorA1,OUTPUT);
pinMode(motorA2,OUTPUT);
pinMode(motorB1,OUTPUT);
pinMode(motorB2,OUTPUT);
}
void loop()
{
buttonState = digitalRead(button);
unsigned long currentMillis = millis();
if (buttonState == LOW) {//If button is pressed once...
delay(500);
if ( flag == 0){
function = 1;
flag=1; //change flag variable
}
else if ( flag == 1){ //If button is pressed twice
function = 0;
flag=0; //change flag variable again
}
}
if (function == 0){ //button pressed twice
stop(); //robot se ustavi
digitalWrite(led, HIGH); // led on
}
else if (function == 1){ //If button is pressed then:
//Read distance...
distance = ultrasonic.Ranging(CM); //Tip: Use 'CM' for centimeters or 'INC' for inches
//Check for objects...
if (distance > 15){
forward(); //All clear, move forward!
digitalWrite(led,LOW);
}
else if (distance <=15){
stop(); //Object detected! Stop the robot and check left and right for the better way out!
digitalWrite(led,HIGH); // turn the led on
delay(600);
right();
delay(900);
forward;
}
}
}
void forward(){
digitalWrite(motorA1, HIGH);
digitalWrite(motorA2, LOW);
digitalWrite(motorB1, HIGH);
digitalWrite(motorB2, LOW);
}
void backward(){
digitalWrite(motorA1, LOW);
digitalWrite(motorA2, HIGH);
digitalWrite(motorB1, LOW);
digitalWrite(motorB2, HIGH);
}
void left(){
digitalWrite(motorA1, HIGH);
digitalWrite(motorA2, LOW);
digitalWrite(motorB1, LOW);
digitalWrite(motorB2, HIGH);
}
void right(){
digitalWrite(motorA1, LOW);
digitalWrite(motorA2, HIGH);
digitalWrite(motorB1, HIGH);
digitalWrite(motorB2, LOW);
}
void stop(){
digitalWrite(motorA1, LOW);
digitalWrite(motorA2, LOW);
digitalWrite(motorB1, LOW);
digitalWrite(motorB2, LOW);
}