Qs about coding using SRF05 and Adafruit V2 Motor shield for a obstacle robot

I am a starter, and i am working on a obstacle avoidance robot using a SRF05 distance sensor and a Adafruit V2 motor shield.
I have looked at someones’ codes using a SRF05 but without the motor shield
http://luckylarry.co.uk/arduino-projects/obstacle-avoidance-robot-build-your-own-larrybot/
and i changed the motor running part by using the libarary from Adafruit for the motor shield.
After i uploaded the codes, the right motor is always running while the left motor is just always at rest.
I think there is a problem with the connecting part between digital write of the sensor and the commands to the motors.
But i still dont know where is the error.
can anyone help just looked at the codes?
the final purpose is to let the robot turn left while there is a obstacle.

[code]#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

Adafruit_DCMotor *myleftMotor=AFMS. getMotor(3);
Adafruit_DCMotor *myrightMotor=AFMS. getMotor(4);

const int numofReadings=10;
int readings[numofReadings];
int arrayIndex=0;
int total=0;
int averageDistance=0;
const int outputPin= 6;
const int inputPin= 7;
unsigned long pulseTime=0;
unsigned long distance=0;

void setup(){
  pinMode(outputPin,OUTPUT);
  pinMode(inputPin,INPUT);
  for (int thisReading=0; thisReading < numofReadings; thisReading++){
    readings[thisReading]=0;
  }
}

void loop(){
   digitalWrite(inputPin,HIGH);
   delayMicroseconds(10);
   digitalWrite(inputPin,LOW);
   pulseTime=pulseIn(outputPin,HIGH);
   distance=pulseTime/58;
   total=total-readings[arrayIndex];
   readings[arrayIndex]=distance;
   total=total+readings[arrayIndex];
   arrayIndex=arrayIndex+1;
   
   if (arrayIndex >= numofReadings){
     arrayIndex=0;
   }
   
   averageDistance=total/numofReadings;
   delay(10);
   
   AFMS.begin();
   myrightMotor->setSpeed(100);
   myleftMotor->setSpeed(100);
   
  if (averageDistance >10){
    myrightMotor->run(BACKWARD);
    myleftMotor->run(BACKWARD);
  }
  
  if (averageDistance <=10){
    myrightMotor->run(BACKWARD);
    myleftMotor->run(RELEASE);
  }
  
}

[/code]

click the MODIFY button in the upper right of the post window. Highlight all you code. click the "#" CODE TAGS button on the toolbar above just to the left of the QUOTE button. click SAVE (at the bottom). When you post code on the forum, please make a habit of using the code tags "#" button.

Add LEDs (with 220 ohm resistors in series) for ALL you motor shield signals (DIR,PWM,EN etc) You should have 4 DIR and two PWM signals. You need to be able to look at the leds and tell us the logic state of any or all of them at a glance. Once you've added those , let us know and we'll proceed debugging it ....

Also, add COMMENTS explaining EXACTLY what your I/O PINS represent in terms of DIR or PWM (inputpin/output pin is no help to us because motor shields don't have output pins that connect to arduinos. The ouput pins on a motor shield are the pins the motor connects to . The DIRECTION pins are often labeled IN1, IN2, IN3 etc. and the ENable pins are almost always labeled ENABLE, but there are no motor shield pins that connect to an arduino labeled OUTPUTs. Change you I/O pin labels to something that describes their purpose more specifically than "input or output".such as M1DIR1,M1DIR2, M2DIR1,M2DIR2, EN1,EN2.

raschemmel:

click the MODIFY button in the upper right of the post window. Highlight all you code. click the "#" CODE TAGS button on the toolbar above just to the left of the QUOTE button. click SAVE (at the bottom). When you post code on the forum, please make a habit of using the code tags "#" button.

Add LEDs (with 220 ohm resistors in series) for ALL you motor shield signals (DIR,PWM,EN etc) You should have 4 DIR and two PWM signals. You need to be able to look at the leds and tell us the logic state of any or all of them at a glance. Once you've added those , let us know and we'll proceed debugging it ....

Also, add COMMENTS explaining EXACTLY what your I/O PINS represent in terms of DIR or PWM (inputpin/output pin is no help to us because motor shields don't have output pins that connect to arduinos. The ouput pins on a motor shield are the pins the motor connects to . The DIRECTION pins are often labeled IN1, IN2, IN3 etc. and the ENable pins are almost always labeled ENABLE, but there are no motor shield pins that connect to an arduino labeled OUTPUTs. Change you I/O pin labels to something that describes their purpose more specifically than "input or output".such as M1DIR1,M1DIR2, M2DIR1,M2DIR2, EN1,EN2.

The Input/output Pins i declared in my codes are the trig pin and echo pin from my SRF05 sensor. The pins for the motors are connected to where it said M3&M4 on the shield. I have tested the motors and sensor seperately, and both of them worked-the motors were running well by using the test code from Adafruit, and the sensor did output measurements.

raschemmel:

click the MODIFY button in the upper right of the post window. Highlight all you code. click the "#" CODE TAGS button on the toolbar above just to the left of the QUOTE button. click SAVE (at the bottom). When you post code on the forum, please make a habit of using the code tags "#" button.

Add LEDs (with 220 ohm resistors in series) for ALL you motor shield signals (DIR,PWM,EN etc) You should have 4 DIR and two PWM signals. You need to be able to look at the leds and tell us the logic state of any or all of them at a glance. Once you've added those , let us know and we'll proceed debugging it ....

Also, add COMMENTS explaining EXACTLY what your I/O PINS represent in terms of DIR or PWM (inputpin/output pin is no help to us because motor shields don't have output pins that connect to arduinos. The ouput pins on a motor shield are the pins the motor connects to . The DIRECTION pins are often labeled IN1, IN2, IN3 etc. and the ENable pins are almost always labeled ENABLE, but there are no motor shield pins that connect to an arduino labeled OUTPUTs. Change you I/O pin labels to something that describes their purpose more specifically than "input or output".such as M1DIR1,M1DIR2, M2DIR1,M2DIR2, EN1,EN2.

Thank U!! I mostly understand what u mean. The trig pin of the sensor is connected to the PWM 3 while the echo pin is on the PWM 2

To put it plainly, your I/O are named whatever you named them. if you have a Trigpin and an Echopin , doesn't it stand to reason they should be named Trigpin & Echopin ? Is there any rationale that makes sense to name them anything else ?

raschemmel:
To put it plainly, your I/O are named whatever you named them. if you have a Trigpin and an Echopin , doesn’t it stand to reason they should be named Trigpin & Echopin ? Is there any rationale that makes sense to name them anything else ?

yes,u r right. i am changing them right now.
But how are the codes? Do u have any suggestions to make it works?I think i need to revise the setup and loop.

When you change the names of your motor I/O pins to M3 & M4 and whatever else you to what it should be named and add comments to explain what your code is doing then I'll take a look at your code. When you post code on the forum asking us to look at it , you are basically asking us for a favor. That's fine , but we at least expect you to have the courtesy to put comments in your code to make it easier for us. I understand that you are not aware that we expect that which is why I am mentioning it. When someone posts code asking for help and there are no comments , one's inclination is to think 'well if he can't be bothered to put comments in his code I can't be bothered to look at it." . It's just human nature. I would suggest you isolate the motor code from the ultrasonic code by creating separate test sketches that each perform one function. If it doesn't work all in the same sketch then you are not ready to run them together. You said your motors work with the example files so what are you doing different ? Is the ultrasonic working ?

the right motor is always running while the left motor is just always at rest.

Did you add the LEDs I mentioned ?

I think there is a problem with the connecting part between digital write of the sensor and the commands to the motors. Rename your I/O and add the leds and comments .