Control DC-motor with Sonar Rangefinder

Hi all,

I'm in a bit of a trouble with my DC motor and a sonar range finder. This is an extremely simple problem but since I am a max/msp user I am not familiar with arduino coding that much.
I need to activate my DC motor for 1000 msec and return it if the range finder detects anything in the range of 2 meters [roughly, doesn't need to be accurate.]
I am using the ladyada mshield : Arduino motor/stepper/servo control - How to use
and Maxbotics LV-EZ3 : http://www.maxbotix.com/MaxSonar-EZ1__FAQ.html

and this is my code so far : {it has errors}

#include <AFMotor.h>
AF_DCMotor motor(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motor test!");
  
  motor.setSpeed(200);     // set the speed to 200/255
}

void loop() { 
   int distsensor, i;
   long time;
   /*
   for (i=0; i<50; i++) {
     pulseServo(servo,0);
   }
   for (i=0; i<50; i++) {
     pulseServo(servo,400);
   }
   return;
   */
   distsensor = 0;
   for (i=0; i<8; i++) {
     distsensor += analogRead(0);
     delay(50);
   }
   distsensor /= 8;

    Serial.println(distsensor);
   
   if (distsensor <= 500) {
     
       Serial.print("tick"); 
       motor.run(FORWARD);      // turn it on going forward
       delay(1000);
       
       Serial.print("tack");
       motor.run(RELEASE);      // stopped
       delay(500);
     
       Serial.print("tock");
       motor.run(BACKWARD);     // the other way
       delay(1000);
       
       Serial.print("tack");
       motor.run(RELEASE);      // stopped
       delay(1000);
     }

any help or suggestion would be greatly appreciated, I have 24 hours to get this thing working!
cheers,
mani

{it has errors}

Well, the program is missing a final "}", but that may just be your cut'n'paste.
What else is wrong?

It looks like the function "pulseServo" and the argument "servo" aren't defined anywhere.

What do you want the motor to do based on the input? Right now it looks like it just goes forwards and backwards for a second then releases control.

When you say {it has errors} is that on compile or it doesn't function they way you think it should?

Cheers guys, I kinds figured it out. the error was from my shield. the final code goes like this :

#include <AFMotor.h>
AF_DCMotor motor(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motor test!");
  
  motor.setSpeed(255);     // set the speed to 200/255
}

void loop() { 
   int distsensor, i;
   long time;
   distsensor = 0;
   for (i=0; i<8; i++) {
     distsensor += analogRead(0);
     delay(100);
   }
   
   distsensor /= 8;

    Serial.println(distsensor);

   if (distsensor >= 33) {
     
       Serial.print("tick"); 
       motor.run(FORWARD);      // turn it on going forward
       delay(6000);
       
       Serial.print("tack");
       motor.run(RELEASE);      // stopped
       delay(500);
     
       Serial.print("tock");
       motor.run(BACKWARD);     // the other way
       delay(6000);
       
       Serial.print("tack");
       motor.run(RELEASE);      // stopped
       delay(2000);
     }
}

Cool, so what's it eventually supposed to do? Besides lurch forward and backward when something walks in front of it ;D

Thats it ! lol
it's for an art piece, so it's not really a technological challenge.
I find max/msp a more user friendly interface for people like me.