Motor shield and maxsonar sensor help

Hi

I have a ladyada motor shield, a maxsonar sensor and an old remote control car. I am trying to make the car stay a set distance from away from me, so that as I walk/run towards it it moves away in a straight line. However I can’t seem to get it to work.

The code so far:

#include <AFMotor.h>



const int anPin = 7;     //Maxsonar on Analogue pin 7\

//variables needed to store values
long anVolt, inches, cm;
int sum=0;          //Create sum variable so it can be averaged
int avgrange=1;     //Quantity of values to average (sample size)

int minDist = 30;        //Distance at which motor goes at full speed.
int optimumDist = 100;    //How far  away you want the car/thing
int mapResult = 0;   //Variable to store the mapping result
int motorSpeed = 0;  //Variable for motor speed

AF_DCMotor motor(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm


///////////////////////////////////////////////////////////////////

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  

  motor.setSpeed(motorSpeed);     // Set motor to motorSpeed Value
}


void loop() {
  
  
///////////////////////// Ping/MaxSonar stuff ///////////////////////////////////
  pinMode(anPin, INPUT);


  for(int i = 0; i < avgrange ; i++)
  {

    //Used to read in the analog voltage output that is being sent by the MaxSonar device.
    //Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
    anVolt = analogRead(anPin);
    sum += anVolt;
    delay(50);

  }  

  inches = sum/avgrange;
  cm = inches * 2.54;
  

  Serial.print(cm);
  Serial.print("cm   --  Maxsonar Reading");
  Serial.println();
  //reset sample total
  sum = 0;
  
   
 //////////////////////////// Mappin////////////////////////////////
  
mapResult = map(cm, minDist, optimumDist, 0, 255);

if (cm < minDist)
{
  motorSpeed = 255;
}

else if (cm < optimumDist)
{
  motorSpeed = 255 - mapResult;
}
else if (cm > optimumDist)
{
motorSpeed = 0;
}

Serial.print(mapResult);
Serial.print("   mapResult");
    Serial.println();
    Serial.print(motorSpeed, DEC);
    Serial.print("  Motorspeed output");
      Serial.println();
      
      //////// Turn the Motor ///////////////////
      
  motor.run(FORWARD);      // turn it on going forward

}

Serial Monitor shows that the motor speed variable is changing accordingly, however the motor doesn’t actually move at all. The shield and motor work fine when I use small example test programs. I’m sure it’s something very simple but even with a good search of the forums I can’t work it out. Any help really appreciated!

if (cm < minDist)
{
   motorSpeed = 255;
}
else if (cm < optimumDist)
{
   motorSpeed = 255 - mapResult;
}
else if (cm > optimumDist)
{
   motorSpeed = 0;
}

Shouldn’t the tests be based on mapResult, rather than cm?

Also, shouldn’t there be a call to motor.setSpeed in loop, rather than in setup()?

I know that the pinMode statement in loop should be in setup, instead?

hello im working on something like that. did you ever get it to work.??