Hello ,
I have built a obstacle avoiding robot …and this is the code
#include <Ultrasonic.h>
#include <Servo.h>
#define MOTOR_1_FORWARD 17
#define MOTOR_1_BACK 16
#define MOTOR_2_FORWARD 19
#define MOTOR_2_BACK 18
#define TRIG 5
#define ECHO 4
#define HEAD_SERVO 6
#define OBSTRACLE_DISTANCE 20.0
#define TURN_DELAY 500
#define LOG false
Ultrasonic ultrasonic(TRIG, ECHO);
Servo headServo;
int servoAngle = 90;
int angleStep = 30;
float distance = 0;
void setup()
{
pinMode(MOTOR_1_FORWARD, OUTPUT);
pinMode(MOTOR_1_BACK, OUTPUT);
pinMode(MOTOR_2_FORWARD, OUTPUT);
pinMode(MOTOR_2_BACK, OUTPUT);
stopMove();
headServo.attach(HEAD_SERVO);
if(LOG) Serial.begin( 9600 );
}
void loop()
{
updateHeadAngle();
checkDistance();
moove();
delay(10);
}
void checkDistance()
{
distance = ultrasonic.Ranging(CM);
if(LOG) Serial.println(distance);
}
void moove()
{
if( distance > OBSTRACLE_DISTANCE )
{
if(LOG) Serial.println("FORWARD");
goForward();
delay(200); //TURN_DELAY
}
else
{
stopMove();
checkObstracle();
}
}
void checkObstracle()
{
int obsLeft = 0;
int obsRight = 0;
// Count the obstacles from left and right side
for(servoAngle = 0; servoAngle <= 180; servoAngle += 30)
{
headServo.write(servoAngle);
delay(200);
checkDistance();
if(distance < OBSTRACLE_DISTANCE && servoAngle < 90) obsLeft++;
else if(distance < OBSTRACLE_DISTANCE) obsRight++;
}
if(LOG) Serial.print("TURN");
if(obsLeft && obsRight)
{
goBack();
delay(TURN_DELAY * 6);
if(obsLeft < obsRight) goLeft();
else goRight();
delay(TURN_DELAY*6);
}
else if(obsRight)
{
goLeft();
delay(TURN_DELAY*6);
}
else if(obsLeft)
{
goLeft(); // goRight
delay(TURN_DELAY*6);
}
else
{
goForward();
delay(TURN_DELAY);
}
}
void updateHeadAngle()
{
headServo.write(servoAngle);
servoAngle += angleStep;
if(servoAngle >= 150)
{
servoAngle = 150;
angleStep *= -1;
}
if(servoAngle <= 30)
{
servoAngle = 30;
angleStep *= -1;
}
}
void goForward()
{
digitalWrite(MOTOR_1_FORWARD, HIGH);
digitalWrite(MOTOR_1_BACK, LOW);
digitalWrite(MOTOR_2_FORWARD, HIGH);
digitalWrite(MOTOR_2_BACK, LOW);
}
void goBack()
{
digitalWrite(MOTOR_1_FORWARD, LOW);
digitalWrite(MOTOR_1_BACK, HIGH);
digitalWrite(MOTOR_2_FORWARD, LOW);
digitalWrite(MOTOR_2_BACK, HIGH);
}
void goLeft()
{
digitalWrite(MOTOR_1_FORWARD, LOW);
digitalWrite(MOTOR_1_BACK, HIGH);
digitalWrite(MOTOR_2_FORWARD, HIGH);
digitalWrite(MOTOR_2_BACK, LOW);
}
void goRight()
{
digitalWrite(MOTOR_1_FORWARD, HIGH);
digitalWrite(MOTOR_1_BACK, LOW);
digitalWrite(MOTOR_2_FORWARD, LOW);
digitalWrite(MOTOR_2_BACK, HIGH);
}
void stopMove()
{
digitalWrite(MOTOR_1_FORWARD, LOW);
digitalWrite(MOTOR_1_BACK, LOW);
digitalWrite(MOTOR_2_FORWARD, LOW);
digitalWrite(MOTOR_2_BACK, LOW);
}
The above code is the fully automated code. I want to make it automated/manual mode. If signals from additional sensor (I’ll define some extra sensor in pin 10/11 ) is high, It should come to manual mode. Then I’ll control it by bolt and below is manual code.
#include <BoltIoT-Arduino-Helper.h>
# define left_motor1 17
# define left_motor2 16
# define right_motor1 19
# define right_motor2 18
# define sensor 3
String command = "";
int delay_time = 250;
String check() {
int sensor_value = digitalRead(sensor);
Serial.println(sensor_value);
Serial.println(command);
if ((sensor_value == 0) && (command == "FORWARD")) {
digitalWrite(left_motor2,LOW);
digitalWrite(left_motor1,LOW);
digitalWrite(right_motor1,LOW);
digitalWrite(right_motor2,LOW);
command = "";
}
return "STOP";
}
String forward(String *data) {
command = "FORWARD";
digitalWrite(left_motor1,HIGH);
digitalWrite(left_motor2,LOW);
digitalWrite(right_motor1,LOW);
digitalWrite(right_motor2,HIGH);
return "Success:Forward";
}
String backward(String *data) {
command = "BACKWARD";
digitalWrite(left_motor1,LOW);
digitalWrite(left_motor2,HIGH);
digitalWrite(right_motor1,HIGH);
digitalWrite(right_motor2,LOW);
return "Success:Backward";
}
String right(String *data) {
command = "RIGHT";
digitalWrite(left_motor1,LOW);
digitalWrite(left_motor2,HIGH);
digitalWrite(right_motor1,LOW);
digitalWrite(right_motor2,HIGH);
delay(delay_time);
digitalWrite(left_motor2,LOW);
digitalWrite(left_motor1,LOW);
digitalWrite(right_motor1,LOW);
digitalWrite(right_motor2,LOW);
return "Success:Left";
}
String left(String *data) {
command = "LEFT";
digitalWrite(left_motor1,HIGH);
digitalWrite(left_motor2,LOW);
digitalWrite(right_motor1,HIGH);
digitalWrite(right_motor2,LOW);
delay(delay_time);
digitalWrite(left_motor2,LOW);
digitalWrite(left_motor1,LOW);
digitalWrite(right_motor1,LOW);
digitalWrite(right_motor2,LOW);
return "Success:Right";
}
String stopcar(String *data) {
digitalWrite(left_motor2,LOW);
digitalWrite(left_motor1,LOW);
digitalWrite(right_motor1,LOW);
digitalWrite(right_motor2,LOW);
return "Success:Stop";
}
void setup() {
// put your setup code here, to run once:
pinMode(left_motor1,OUTPUT);
pinMode(left_motor2,OUTPUT);
pinMode(right_motor1,OUTPUT);
pinMode(right_motor2,OUTPUT);
pinMode(sensor,INPUT);
boltiot.begin(Serial);
Serial.begin(9600);
boltiot.setCommandString("FORWARD",forward);
boltiot.setCommandString("BACKWARD",backward);
boltiot.setCommandString("LEFT",left);
boltiot.setCommandString("RIGHT",right);
boltiot.setCommandString("STOP",stopcar);
}
void loop() {
// put your main code here, to run repeatedly:
boltiot.handleCommand();
check();
}
tell me how to combine these to codes so that logic is as follows
If I define some sensor and take its input and if the sensor is high then automated mode or if the sensor is low then manual mode (via BoltIoT)
I tried a lot but I am getting repeatedly error…please help