Avoiding obstacles code

Hi all.

I have started making a small robot the RMOAR https://www.robotshop.com/letsmakerobots/node/20422

but im having some problems with the code i tried to made in order to get this thing working.

I dont know if that code will work and how can i improve it .

Here is the code:

#include <AFMotor.h>
#include <Servo.h>
#define BUMPSENSOR 3
#define BUMPSENSOR2 2
#define TOPSPEED 200
Servo scanservo;
int pos = 0;
int servoStep = 15;
int bumped = digitalRead(BUMPSENSOR);
int bumped2 = digitalRead(BUMPSENSOR2);
AF_DCMotor rightMotor(9, MOTOR12_8KHZ);
AF_DCMotor leftMotor(10, MOTOR12_8KHZ);

void setup()
{
  Serial.begin(9600);
  pinMode(7, OUTPUT);
  scanservo.attach(7);
  delay(2000);
  rightMotor.setSpeed(TOPSPEED);
  leftMotor.setSpeed(TOPSPEED);
  pinMode(BUMPSENSOR, INPUT);
  pinMode(BUMPSENSOR2, INPUT);
  moveForward();

  delay(500);

}
void loop()
{
  delay(500);
  moveForward();
  delay(500);
  for(pos = 0; pos < 180; pos+= servoStep)
  {
    scanservo.write(pos);
        delay(15);
        checkforbump();
         }
  for(pos = 180; pos > 1; pos-= servoStep)
  {
    scanservo.write(pos);
        delay(15);
        checkforbump();
  }

}

void checkforbump() {
    if(bumped == HIGH || bumped2 == HIGH && pos < 90){
            turnRight();
        }
        if(bumped == HIGH || bumped2 == HIGH && pos > 90){
        turnLeft();
   }
    if(bumped == HIGH || bumped2 == HIGH && pos == 90){
        stop();       
        moveBackward();
    }
}
void moveForward(){
  Serial.println("Move Forward");
  rightMotor.run(FORWARD);
  leftMotor.run(FORWARD);
}
void turnLeft(){
  Serial.println("Turn Left");
  rightMotor.run(BACKWARD);
  leftMotor.run(FORWARD);
}
void turnRight(){
  Serial.println("Turn Right");
  rightMotor.run(FORWARD);
  leftMotor.run(BACKWARD);
}
void stop(){
  Serial.println("Stop");
  rightMotor.run(RELEASE);
  leftMotor.run(RELEASE);
  delay(500);
}
void moveBackward(){
  Serial.println("Move Backward");
  rightMotor.run(RELEASE);
  leftMotor.run(RELEASE);
  rightMotor.run(BACKWARD);
  leftMotor.run(BACKWARD);
}

    

 
}

Does the code work? You

Does the code work? You havent said what is wrong with it so Im not sure what to look for.

because

i havent tried it yet thats why i dont know the problem but the compiler says that something is wrong…

 

If you tell us the error

If you tell us the error from the compiler it’ll be a lot easier to track down the problem.

Delete the last curly

Delete the last curly bracket.


I tried it ,and it didn’t work,he moved the scan servo as he should do but the robot didn’t actually moved at all.

 

You have the digitalRead for

You have the digitalRead for your bump sensors outside the code loop. Every time you want to know if you`ve hit something you will have to do another digitalRead. Try this for starters…

void checkforbump() {

    bumped = digitalRead(BUMPSENSOR);

    bumped2 = digitalRead(BUMPSENSOR2);
    if(bumped == HIGH || bumped2 == HIGH && pos < 90){
            turnRight();
        }
        if(bumped == HIGH || bumped2 == HIGH && pos > 90){
        turnLeft();
   }
    if(bumped == HIGH || bumped2 == HIGH && pos == 90){
        stop();       
        moveBackward();
    }
}


Wait I just looked at your robot page… do you actually have bump sensors or do you mean the sharp IR sensors?

What sensor did you use for

What sensor did you use for the bumper, digital or analog? In your RMOAR robot you use 2 Sharp GP2Y0A02YK0F. AFAIK these are analog sensors. You should connect them to analog ports and use the analogRead() function instead of digitalRead(). Make a series of test measurements to get the relation between analog value and distance to the object. Read the datasheet.