Arduino Uno Project Problem

Hello Guys
I wrote in this forum because i m new to arduino programming and i need help.
So my project uses the parts in the BYOR kit,so to specify i am using a motor, witch connects with 2 cables ,and the ultrasonic sensor.i am going to post also the image of my current cable configuration and the code that i scrapped from a project of the BYOR.My plan consist on moving the motor (after a 5 second) when the ultrasonic sensor scans an object witch is in between a certain distance (about 10cm).
I tried to modify the code and i tested it on my prototype but the motor just spins and dosnt care of what the sensor says.During my testing i saw that if i programmed that the distance needed to make the motor spins was miner than 10cm it would start but the motor spins constantly.I lowered the value till 0 so he stopped but only at 0...Please help me..

Test_Mk1.1.ino (1.02 KB)

HyperVortex:
I tried to modify the code

If this is code that came as part of a kit did you first make sure that the unmodified code works?

...R

#define M1r    3
#define M1b    5

#define M2r    10
#define M2b    11

#define SONAR_TRIGGER_PIN   6
#define SONAR_ECHO_PIN      7
void setup() {
  Serial.begin(9600);

  pinMode(SONAR_TRIGGER_PIN, OUTPUT);
  pinMode(SONAR_ECHO_PIN, INPUT);

  pinMode(M1r, OUTPUT);
  pinMode(M1b, OUTPUT);

  pinMode(M2r, OUTPUT);
  pinMode(M2b, OUTPUT);
}

void avanti()
{
  digitalWrite(M1r, HIGH);
  digitalWrite(M1b, LOW);
  digitalWrite(M2r, HIGH);
  digitalWrite(M2b, LOW);
}
void fermo()
{
  digitalWrite(M1r, LOW);
  digitalWrite(M1b, LOW);
  digitalWrite(M2r, LOW);
  digitalWrite(M2b, LOW);
}
unsigned int measure_distance()
{
  digitalWrite(SONAR_TRIGGER_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(SONAR_TRIGGER_PIN, LOW);
  unsigned long pulse_length = pulseIn(SONAR_ECHO_PIN, HIGH);
  delay(50);
  return ( (unsigned int) (pulse_length / 58) );
}
void loop() {

  unsigned int ostacolo = measure_distance();
  Serial.println(ostacolo, DEC);
  if (ostacolo < 10 )
  {
    fermo();
  }
  else (ostacolo > 10 );
  {
    avanti();

  }
}

You have bad syntax on the else, some choices:

  if (ostacolo < 10 )
  {
    fermo();
  }
  else  if (ostacolo > 10 ) // add if, remove ;
  {
    avanti();

  }

or

  if (ostacolo < 10 )
  {
    fermo();
  }
  else
  {
    // also calls avanti when ostacolo == 10
    avanti();

  }

Thanks, actually i knew that the else part was wrong, i uploaded the wrong version, but today I am going to test and let you know if it works or not with your corrections! Thanks

I solved my problem! thanks a lot.There was a wire which wasnt giving power to the sensor!
Thanks for helping me with the code!