Motor and sensor

Swaggydogy:
Just please, i would like to get some help about the code...

I am assuming the code is what is in your Original Post. This is the code from loop()

void loop() {
 for (int i = 0; i = 50; i++) // we turn the motor
   {
   analogWrite(3, i);
   delay(10);
   }
 buttonState = digitalRead(buttonPin);// reading of the sensor
 if (buttonState == HIGH) // if something is detected
 {
   digitalWrite(ledPin, HIGH); // LED on
   for (int i = 50; i = 0; i++) // We stop the rotation of the motor
   {
   analogWrite(3, i);
   delay(10);
 }
 }
}

I suspect the main problem with that is your use of FOR because all the code in a FOR loop will complete before anything else happens. So, for example the speed of the motor increases in 50 steps over a period of 500 millisecs before there is any attempt to read the sensor. And then if the sensor is detected there will be another 50 steps ...

And as well as that you have the syntax for your FOR loops wrong. The first one should be

for (int i = 0; i <= 50; i++) // we turn the motor

and the second one should be

for (int i = 50; i >= 0; i--) // We stop the rotation of the motor

However I have no idea how far the motor will move in 500 millisecs
And what do you think will happen if the sensor is not detected the first time?

A more sensible approach is to set the motor rotating with a single call to analogRead() analogWrite() and then repeatedly check the sensor. Something like

void loop() {
   if (motorMoving == false) {
      analogWrite(motorPin, pwmValue);
      motorMoving = true;
   }
   if (motorMoving == true) {
      sensorVal = digitalRead(sensorPin);
      if (sensorVal == HIGH) {
         analogWrite(motorPin, 0);
         motorMoving = false;
      }
   }
}

Have a look at these links
Several Things at a Time
Planning and Implementing a Program

...R