Problem whit this code

HELLO THERE, I TRY TO MOVE MYROVER Autonomous, BUT THE PART OF THE CODE TO MOVE THE WHEELS DONT WORK, CAN ANY HELP

HERE IS THE CODE

#include <Servo.h> 

// ============ OPERATIONAL CONSTANTS =================

int safe_distance = 25; // safe distance to obstacle before robot takes action (eg stop, turn) - in inches
int E1 = 6; //M1 Speed Control
int E2 = 5; //M2 Speed Control
int M1 = 8; //M1 Direction Control
int M2 = 7; //M2 Direction Control

int rturn_time = 1000;          // time needed for wheels to make a 90 Right turn
int lturn_time = 1000;          // time needed for wheels to make a 90 Left turn
int reverse_time = 2000;     // time needed for wheels while reversing
int backturn_time = 2000;   // time needed for wheels to spin to turn 180

// Create Servo objects
Servo IR_servo;           // Servo to rotate Sharp IR sensor acting as radar 
// Assign servos/sensor I/O to Arduino  pins
int IR_read_pin = 5;       // analog pin to read IR sensor
int IR_pin = 9;            // pin to control servo to spin IR sensor
int IR_value_debug =0;
// Trackers to control servo positions. 
int IR_pos = 90;

char go_direction = 'F' ;  // holds current robot direction
// char last_direction = 'F';
// char dir = 'F';

void setup() 
{  
  // set all pins to output
  int i;
  for(i=5;i<=8;i++)
    pinMode(i, OUTPUT); 

  Serial.begin(9600);
  pinMode(IR_pin, INPUT);    // set IR Sonar pin for input 
  IR_servo.attach(IR_pin);   // activate IR scanning servo  

  // find which direction to start moving.   
  go_direction = look_around('A');            // scan in (A)ll directions to decide where to go first
  go_direction = move_to(go_direction);  // Go in direction with more space
} 

void loop()
{ 
  // Motor tuning - since this is a track vehicle and motors are never exact, they must be tuned ...
  //255 is maximum speed

  int leftspeed = 255; //adjust to balance motors
  int rightspeed = 255;//adjust to balance motors

  // F = Forward  R = Right  L = Left  B = Backturn 180  T = Reverse S = Stop D= Deadend O = Obstacle

  while (go_direction == 'F'){  // keep going forward while no Obstacle is reached and direction returned is 'F'orward
    // Serial.print("Loop/Global IR value: ");   Serial.println(IR_value_debug);
    go_direction = look_around(go_direction);
  }
  go_still(); // stop

  // Serial.print("NO LONGER MOVING FORWARD BECAUSE OF CONDITION: "); Serial.println(go_direction);

  if (go_direction == 'O'){            // if Forward movement reaches Obstacle
    go_direction = look_around('A');   // look around
    if (go_direction == 'D') {         // if Dead end
      move_to('B');                    // turn 180
      go_direction = look_around('A'); // look around
      if (go_direction == 'D'){        // if another Dead end
        move_to('S');                  // Stop and send help
        // Serial.println("DEAD END! I GIVE UP.");
        while (1==1) {
        }                
      } 
    }
  }
  go_direction = move_to(go_direction);
}

// ====================== RADAR FUNCTION ============================

char look_around(char whereto){
  char dirx = '/'; 
  int last_distance = 0;
  int distance = 0;

  switch (whereto) { // scan where?

  case 'F':  // scan if Forward is clear, return same F if true
    // Serial.println("look_around>> Scanning forward");
    IR_servo.write(90);
    delay(100);
    last_distance = IR_scan();
    delay(10);
    if (last_distance > safe_distance) {
      // Serial.print("look_around>> FORWARD DISTANCE: ");  Serial.println(last_distance);
      dirx = 'F';
      return 'F';    
    } 
    else
    {
      dirx = 'O';   // Obstacle reached
      return 'O';   // Obstacle reached
    }
    break;

  case 'A':  // Scan L,F,R if clear. return direction of direction with more space. 
    dirx = 'D';    // Assume Dead end unless changed in following tests 

    // SCAN LEFT
    IR_servo.write(180); // turn IR servo Left (or Right depending on your setting)
    delay(1000);
    distance = IR_scan(); // get distance to obstacle NOTE: be mindful of out of range values for sensor

    // Serial.print("look_around>> Left scan distance: "); Serial.println(distance);
    if (distance > safe_distance ){
      dirx = 'L';
    }

    // SCAN FORWARD
    IR_servo.write(90); // turn IR servo Forward
    delay(1000);
    last_distance = IR_scan(); // get distance to obstacle NOTE: be mindful of out of range values for sensor
    delay(500);
    // Serial.print("look_around>> Forward scan distance: ");  Serial.println(last_distance);
    if (last_distance > distance && last_distance > safe_distance ){
      distance = last_distance; // forward space is greater than previous space
      dirx = 'F'; //go forward
    }

    // SCAN RIGHT
    IR_servo.write(0); // turn Right (or Left depending on servo orientation)
    delay(1000);
    last_distance = IR_scan(); // get distance to obstacle NOTE: be mindful of out of range values for sensor
    delay(500);
    // Serial.print("look_around>> Right scan distance: "); Serial.println(last_distance);    
    if (last_distance > distance && last_distance > safe_distance ){
      distance = last_distance; // forward space is greater than previous space
      dirx = 'R'; //go Left (or Right depending on your servo)
    }
    return dirx;  // retun resulting direction. 
    break;
  }
  // Serial.print("look_around>> Go direction: "); Serial.print(dirx); Serial.print("   @distance: "); Serial.println 
  (last_distance);
}
// ====================== RADAR FUNCTION ============================

// ==================MOVEMENT FUCTION =====================================
// F = forward  R = right  L = left  B = backturn 180  T = reverse S = stop 
char move_to(char dir){
  switch (dir) { // scan ahead
  case 'F':  
    // Serial.println("move_to>> Turn  forward");
    go_forward();
    return 'F';
    break;
  case 'L': 
    // Serial.println("move_to>> Turn  Left");
    go_left(lturn_time);
    go_forward();
    return 'F';
    break;
  case 'R': 
    // Serial.println("move_to>> Turn  Right");
    go_right(rturn_time);
    go_forward();    
    return 'F';
    break; 
  case 'B': 
    // Serial.println("move_to>> Backturn 180");
    go_back(backturn_time);
    go_still();
    return 'S';
    break;
  case 'T': 
    // Serial.println("move_to>> Reverse in straight line");
    go_reverse(reverse_time);
    go_still();
    return 'S';
    break;
  case 'S': 
    // Serial.println("move_to>> Stop");
    go_still();
    return 'S';
    break;
  }
}

// =========================== IR LOGIC ===========================
int IR_scan()  // scan for obstacles then return distance
{
  float IR_distance =0;
  float TOT_IR_distance =0;
  float volts = 0;
  int IR_readings = 10;

  //  Serial.println("==============START REAL TIME IR READING ===========");
  for (int i =0; i < IR_readings ; i++) { 
    volts += analogRead(IR_read_pin) *0.0048828125;  
    IR_distance += 65*pow(volts, -1.10);    // worked out from graph 65 = theoretical distance / (1/Volts)S - luckylarry.co.uk
    //   Serial.println(IR_distance);
    delay(50);   
    //Serial.print("IR_scan>> each IR distance: "); Serial.println(IR_distance);
    //TOT_IR_distance += IR_distance;
  }
  IR_distance  /= IR_readings;
  IR_value_debug = IR_distance;
  // Serial.print("IR_scan>> AVG TOTAL distance: "); Serial.println(IR_distance);
  return IR_distance;
}

// ================== PHYSICAL MOVEMENT ========================

void go_forward()
{
  analogWrite (E1,HIGH);
  digitalWrite(M1,LOW);
  analogWrite (E2,HIGH);
  digitalWrite(M2,LOW);
}

void go_still(void)
{
  digitalWrite(E1,LOW);
  digitalWrite(E2,LOW);
}

void go_reverse(int move_time)
{
  analogWrite (E1,HIGH);
  digitalWrite(M1,HIGH);
  analogWrite (E2,HIGH);
  digitalWrite(M2,HIGH);
}

void  go_back(int move_time){  // turn 180 in place
  delay(100);
  analogWrite (E1,HIGH);
  digitalWrite(M1,HIGH);
  analogWrite (E2,HIGH);
  digitalWrite(M2,LOW);
}

void  go_left(int move_time)
{
  analogWrite (E1,HIGH);
  digitalWrite(M1,HIGH);
  analogWrite (E2,HIGH);
  digitalWrite(M2,LOW);
}

void  go_right(int move_time)
{
  analogWrite (E1,HIGH);
  digitalWrite(M1,LOW);
  analogWrite (E2,HIGH);
  digitalWrite(M2,HIGH);
}

//================ END ====================

Yes ive ran the blink example, also basic functions i just cannot seem to come up with a funtional code.

Hi, i have the same rover and im trying to get it too work. I tried using the codes above and when i upload them then unplug the usb and turn the rover on. Nothing happens help please i have this due tomorrow.

So, the blink example does work properly? Have you tried sample Sketch #1 and #2 from the user guide?

If those work, we would either recommend modifying the Sketch #2 to make it suit your needs, or to compare the sketch above with Sketch #2 to see what it might be doing different and finding what’s not working on your rover. We do not currently provide consulting and aren’t unfortunately able to debug sketches that are more than a few dozen lines long…

When you upload the code, do see an upload complete confirmation message?

When you turn on the robot, do the LEDs also turn on?

You may want to try starting with the Blink example that codes with the Arduino IDE to verify that the upload and base electronics do work properly.

That’s right, the pins can’t be changed because they’re directly connected in the PCB.

If you add servos, you can get the rover to move differently, but we won’t be able to help much with that.

I think the problem lies with the way you are using analogWrite.

Since HIGH=1, it is generally only used for digital inputs/outputs. When you do analogWrite(E1, HIGH) (in your physical movement functions), you are setting pin 6 (E1) to a value of 5 millivolts which is barely more than 0 volts.

Instead, you should either say analogWrite(E1, 255) or digitalWrite(E1, HIGH).

If you make this change, I think your rover will start working as you expect.

Also, I noticed that in your setup() you set pinMode(IR_pin, INPUT). This isn’t necessary (and may cause problems) when you setup the servo object to use IR_pin. I think you can remove it.

Hope this helps,

Thanks jarcand,

i try the code on rover v2, motors pin can make modification on the code ?

int E1 = 6; //M1 Speed Control
int E2 = 5; //M2 Speed Control
int M1 = 8; //M1 Direction Control
int M2 = 7; //M2 Direction Control

because these pin are connected directly on the pcb board ?

// ============ Original code =================

#include <Servo.h> 



int safe_distance = 25; // safe distance to obstacle before robot takes action (eg stop, turn) - in inches
int rturn_time = 1000;          // time needed for wheels to make a 90 Right turn
int lturn_time = 1000;          // time needed for wheels to make a 90 Left turn
int reverse_time = 2000;     // time needed for wheels while reversing
int backturn_time = 2000;   // time needed for wheels to spin to turn 180

// Create Servo objects
Servo IR_servo;           // Servo to rotate Sharp IR sensor acting as radar 
Servo l_servo;             // Servo to drive Left wheel
Servo r_servo;            // Servo to drive Right wheel

// Assign servos/sensor I/O to Arduino  pins
int IR_read_pin = 5;       // analog pin to read IR sensor
int IR_pin = 9;                // pin to control servo to spin IR sensor
int IR_value_debug =0;
int l_pin = 10;              // pin to direct Left wheel servo
int r_pin = 11;             // pin to direct Right wheel servo

// Trackers to control servo positions. 
int IR_pos = 90;
int l_pos = 0;
int r_pos = 0;

// Values used to stop wheel servos. In this program, I don't use them 
// because I just "detach" servo objects which stops the servos

int r_stop = 78;  // this value is specific to a given servo 
int l_stop = 90;  //   this value is specific to a given servo 

// I use these values to drive servos at maximum speed. Note how one speed is negative 
// while other is positive. 
// That's because when you position servos on both sides of the bot, they will 
// spin opposite each other causing the robot to spin around itself. 
// so to drive a robot with two servos in one direction, one wheel must 
// move clockwise and the other counterclockwise for robot to move ahead or back 

int r_go = +200;  // this value is specific to a given servo 
int l_go = -200;  // this value is specific to a given servo 

// Same as above but reversed. 
int r_reverse = -200;  // this value is specific to a given servo 
int l_reverse = +200;  // this value is specific to a given servo 

char go_direction = 'F' ;  // holds current robot direction
// char last_direction = 'F';
// char dir = 'F';

void setup() 
{   
  Serial.begin(9600);
  pinMode(IR_pin, INPUT);    // set IR Sonar pin for input 
  IR_servo.attach(IR_pin);   // activate IR scanning servo  

  // find which direction to start moving.   
  go_direction = look_around('A');            // scan in (A)ll directions to decide where to go first
  go_direction = move_to(go_direction);  // Go in direction with more space
} 

void loop()
{ 
  // F = Forward  R = Right  L = Left  B = Backturn 180  T = Reverse S = Stop D= Deadend O = Obstacle

  while (go_direction == 'F'){  // keep going forward while no Obstacle is reached and direction returned is 'F'orward
                                              // Serial.print("Loop/Global IR value: ");   Serial.println(IR_value_debug);
    go_direction = look_around(go_direction);
  }
  go_still(); // stop
  
 // Serial.print("NO LONGER MOVING FORWARD BECAUSE OF CONDITION: "); Serial.println(go_direction);

  if (go_direction == 'O'){            // if Forward movement reaches Obstacle
    go_direction = look_around('A');   // look around
    if (go_direction == 'D') {         // if Dead end
      move_to('B');                    // turn 180
      go_direction = look_around('A'); // look around
      if (go_direction == 'D'){        // if another Dead end
        move_to('S');                  // Stop and send help
        // Serial.println("DEAD END! I GIVE UP.");
        while (1==1) {
        }                
      } 
    }
  }
  go_direction = move_to(go_direction);
}

// ====================== RADAR FUNCTION ============================

char look_around(char whereto){
  char dirx = '/'; 
  int last_distance = 0;
  int distance = 0;

  switch (whereto) { // scan where?

  case 'F':  // scan if Forward is clear, return same F if true
    // Serial.println("look_around>> Scanning forward");
    IR_servo.write(90);
    delay(100);
    last_distance = IR_scan();
    delay(10);
    if (last_distance > safe_distance) {
      // Serial.print("look_around>> FORWARD DISTANCE: ");  Serial.println(last_distance);
      dirx = 'F';
      return 'F';    
    } 
    else
    {
      dirx = 'O';   // Obstacle reached
      return 'O';   // Obstacle reached
    }
    break;

  case 'A':  // Scan L,F,R if clear. return direction of direction with more space. 
    dirx = 'D';    // Assume Dead end unless changed in following tests 

    // SCAN LEFT
    IR_servo.write(180); // turn IR servo Left (or Right depending on your setting)
    delay(1000);
    distance = IR_scan(); // get distance to obstacle NOTE: be mindful of out of range values for sensor

    // Serial.print("look_around>> Left scan distance: "); Serial.println(distance);
    if (distance > safe_distance ){
      dirx = 'L';
    }

    // SCAN FORWARD
    IR_servo.write(90); // turn IR servo Forward
    delay(1000);
    last_distance = IR_scan(); // get distance to obstacle NOTE: be mindful of out of range values for sensor
    delay(500);
    // Serial.print("look_around>> Forward scan distance: ");  Serial.println(last_distance);
    if (last_distance > distance && last_distance > safe_distance ){
      distance = last_distance; // forward space is greater than previous space
      dirx = 'F'; //go forward
    }

    // SCAN RIGHT
    IR_servo.write(0); // turn Right (or Left depending on servo orientation)
    delay(1000);
    last_distance = IR_scan(); // get distance to obstacle NOTE: be mindful of out of range values for sensor
    delay(500);
    // Serial.print("look_around>> Right scan distance: "); Serial.println(last_distance);    
    if (last_distance > distance && last_distance > safe_distance ){
      distance = last_distance; // forward space is greater than previous space
      dirx = 'R'; //go Left (or Right depending on your servo)
    }
    return dirx;  // retun resulting direction. 
    break;
  }
  // Serial.print("look_around>> Go direction: "); Serial.print(dirx); Serial.print("   @distance: "); Serial.println 
(last_distance);
}

// ==================MOVEMENT FUCTION =====================================
// F = forward  R = right  L = left  B = backturn 180  T = reverse S = stop 
char move_to(char dir){
  switch (dir) { // scan ahead
  case 'F':  
    // Serial.println("move_to>> Turn  forward");
    go_forward();
    return 'F';
    break;
  case 'L': 
    // Serial.println("move_to>> Turn  Left");
    go_left(lturn_time);
    go_forward();
    return 'F';
    break;
  case 'R': 
    // Serial.println("move_to>> Turn  Right");
    go_right(rturn_time);
    go_forward();    
    return 'F';
    break; 
  case 'B': 
    // Serial.println("move_to>> Backturn 180");
    go_back(backturn_time);
    go_still();
    return 'S';
    break;
  case 'T': 
    // Serial.println("move_to>> Reverse in straight line");
    go_reverse(reverse_time);
    go_still();
    return 'S';
    break;
  case 'S': 
    // Serial.println("move_to>> Stop");
    go_still();
    return 'S';
    break;
  }
}

// =========================== IR LOGIC ===========================
int IR_scan()  // scan for obstacles then return distance
{
  float IR_distance =0;
  float TOT_IR_distance =0;
  float volts = 0;
  int IR_readings = 10;
  //  Serial.println("==============START REAL TIME IR READING ===========");
  for (int i =0; i < IR_readings ; i++) { 
    volts += analogRead(IR_read_pin) *0.0048828125;  
    IR_distance += 65*pow(volts, -1.10);    // worked out from graph 65 = theoretical distance / (1/Volts)S - luckylarry.co.uk
    //   Serial.println(IR_distance);
    delay(50);   
    //Serial.print("IR_scan>> each IR distance: "); Serial.println(IR_distance);
    //TOT_IR_distance += IR_distance;
  }
  IR_distance  /= IR_readings;
  IR_value_debug = IR_distance;
  // Serial.print("IR_scan>> AVG TOTAL distance: "); Serial.println(IR_distance);
  return IR_distance;
}

// ================== PHYSICAL MOVEMENT ========================

void go_forward()
{
  attach_servos();
  l_servo.write(l_go);
  r_servo.write(r_go);
}

void go_still()
{
  delay(100);
  detach_servos();
  delay(100);
}

void go_reverse(int move_time){ // move straight  but opposite direction
  delay(100);
  attach_servos();
  l_servo.write(l_reverse);
  r_servo.write(r_reverse);
  delay(move_time); // keep moving for seconds before stopping
  detach_servos();  // stop
  delay(100);
}

void  go_back(int move_time){  // turn 180 in place
  delay(100);
  attach_servos();
  l_servo.write(l_reverse);
  r_servo.write(r_go);
  delay(move_time); // keep moving for seconds before stopping
  detach_servos();  // stop
  delay(100);
}

void  go_left(int move_time){
  delay(100);
  //attach_servos();
  attach_servos();
  l_servo.write(l_go);
  r_servo.write(r_reverse);
  delay(move_time); // keep moving for seconds before stopping
  detach_servos();  // stop
  delay(100);
}

void  go_right(int move_time){
  delay(100);
  attach_servos();
  l_servo.write(l_reverse);
  r_servo.write(r_go);
  delay(move_time); // keep moving for seconds before stopping
  detach_servos(); // stop
  delay(100);
}

void detach_servos(){
  delay(100);
  l_servo.detach();  // sure way to stop servo
  r_servo.detach(); // sure way to stop servo
  delay(100);
}

void attach_servos(){
  delay(100);
  l_servo.attach(l_pin);
  r_servo.attach(r_pin);
  delay(100);
}

//================ END ====================