I am building a project using bluetooth control as well as an ir sensor. The logic behind my program is when I press a key I want my robot to execute a series of program and while the program is executing if an obstacle is detected I need the motors to stop and then continue from the same point after the obstacle is removed. I am uploading my code for ref.
#include <Servo.h>
#include <AFMotor.h>
#include <Servo.h>
int obstaclePin = 12;
int obstacle = LOW;
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
Servo myservo1;
Servo myservo2;
int pos = 0;
char bt='S';
void setup()
{
Serial.begin(9600);
pinMode(obstaclePin,INPUT);
myservo1.attach(10);
myservo2.attach(9);
motor1.setSpeed(100);
motor2.setSpeed(100);
motor3.setSpeed(100);
motor4.setSpeed(100);
Stop();
}
void loop() {
bt=Serial.read();
if(bt=='F')
{
forward();
delay(2000);
backward();
delay(2000);
left();
delay(2000);
right();
delay(2000);
Stop();
}
if(bt=='B')
{
forward();
delay(8000);
left();
delay(1500);
ServoControl();
delay(500);
backward();
delay(1000);
right();
delay(3000);
forward();
delay(8000);
Stop();
}
if(bt=='L')
{
forward();
delay(10000);
left();
delay(1500);
ServoControl();
delay(500);
backward();
delay(1000);
right();
delay(3000);
forward();
delay(10000);
Stop();
}
if(bt=='R')
{
forward();
delay(12000);
left();
delay(1500);
ServoControl();
delay(500);
backward();
delay(1000);
right();
delay(3000);
forward();
delay(12000);
Stop();
}
if(bt=='S')
{
Stop();
}
if(bt=='O')
{
servoOpen();
}
if(bt=='C')
{
servoCLose();
}
}
void forward()
{
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
void backward()
{
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
void left()
{
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void right()
{
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
void Stop()
{
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void servoOpen()
{
for (pos = 0; pos <= 100; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo1.write(pos);
myservo2.write(pos);
}
}
void servoCLose()
{
for (pos = 100; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo1.write(pos);
myservo2.write(pos);
}
}
void ServoControl()
{
for (pos = 0; pos <= 100; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo1.write(pos);
myservo2.write(pos);
delay(40); // waits 15ms for the servo to reach the position
}
for (pos = 100; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo1.write(pos);
myservo2.write(pos);
delay(40); // waits 15ms for the servo to reach the position
}
}
void obstacleD()
{
obstacle = digitalRead(obstaclePin);
if(obstacle == HIGH)
{
Stop();
}
}
I don't know where to add the obstacle detection so it would work like an interrupt. the main problem I was experiencing was that whenever I tried running the code on my Arduino it would completely change the logic and noting would work even if I put the command in the serial monitor until an obstacle was detected. I'm a newbie at programming and using Arduino.
delay()
is your enemy.delay
we can use other timing mechanisms. The problem withdelay
is that if OD is an actual interrupt after the ISR runs you'll drop right back intodelay
, which when you're running into a wall, not cool. That's why the comment regarding FSMs and looking in to other ways to "do something for awhile" w/o usingdelay
.