Your motor only has two speeds, and one of those is invalid (>255).
Also note that if no echo is detected (nothing in range) the distance will be calculated as ZERO. You may want to check for that so the motor doesn't switch to slow speed when there is nothing to detect.
Here is your code with some comments:
// These are constants, so tell the compiler that:
const int trigPin = 9;
const int echoPin = 10;
const int led = 7;
#include <AFMotor.h>
AF_DCMotor motor1(1);
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// motor1.setSpeed(255);
// put your setup code here, to run once:
}
void loop() {
// pulseIn() returns 'unsigned long'
unsigned long duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(10000); // This is equivalent to delay(10); which is easier to read.
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
Serial.print(distance);
Serial.println("CM");
// delay(1000); // DO NOT wait a full second between reading the distance and using that value.
if ((distance <= 10)) // The extra set of parenthesese don't help.
{
digitalWrite(led, HIGH);
motor1.setSpeed(10);
}
else // if (distance > 10) // NO NEED to put another 'if' here. If it not <= 10 you know it is > 10.
{
digitalWrite(led, LOW);
motor1.setSpeed(300); // Are ytou sure that setSpeed() can take a value over 255?
}
} // I assume that this bracket being missing was just a cut and paste error.