Hey Everyone,
This is the problem (sorry for bad English):
Dc-motor goes to point (400 mm) that ultrasonic sensor measures.
Dc-motor has to slow down at 300mm and 500mm before reaching the 400 mm.
If Dc-motor goes too far, for example (450mm). Dc-motor changes it's direction and goes smoothly to 400 mm (not at full speed what it does).
the main problem is that it works in one way, slows down smoothly from 500 mm to 400mm but not from 300mm to 400mm.
So what did I do wrong?
You can see what it looks like in 123d.circuits link:
Thank you for your help!
Stan
int ping = 12; //connection ultrasonic distance sensor
int knop = 11; //button to activate sensor
long x[] = {0, 400, 100, 600, 150, 800}; //mm //distances to stop motor eventually
long puls, afstand, afstandcal;
//h-brug
int enablepin1 = 4; //Pin h-bridge
int motor1links = 3; //dc-motor left
int motor1rechts = 2; //dc-motor right
//set van waar die moet afremmen //set where the motor starts to slow down
int set = 100;
//error tussen gemeten en bepaalde waarde
long error = 0;
//voltage to change speed of the motor
long motorsnelheid;
void setup() {
Serial.begin(9600);
pinMode(knop, INPUT);
digitalWrite(knop, LOW); //button to activate ultrasonic sensor
//h-brug
pinMode(enablepin1, OUTPUT);
pinMode(motor1links, OUTPUT);
pinMode(motor1rechts, OUTPUT);
}
void loop() {
Serial.println("baas");
if (digitalRead(knop) == HIGH) {
Serial.println("kaas");
for (int i = 1; i < 6; i++) {
while (error != x[i]) {
//motor1 aan
digitalWrite(enablepin1, HIGH);
for (int j = 0; j < 10; j++) {
pinMode(ping, OUTPUT);
digitalWrite(ping, HIGH);
delayMicroseconds(5);
digitalWrite(ping, LOW);
//measures signal getting back
pinMode(ping, INPUT);
puls = pulseIn(ping, HIGH);
afstand = microsecondenaarmilimeters(puls);
Serial.print("10 waarden ");
Serial.println(afstand);
afstandcal += afstand;
}
afstand = afstandcal / 10;
delay(500);
Serial.print(" gemiddelde "); //average speed over 10 measurements
Serial.println(afstand);
afstandcal = 0;
error = x[i] - afstand;
//omrekenen naar motorsnelheid
motorsnelheid = map(error, -set, set, 255, -255);
if (afstand < error - set) {
analogWrite(motor1rechts, 0);
analogWrite(motor1links, 255);
}
if (afstand > error + set) {
analogWrite(motor1rechts, 255);
analogWrite(motor1links, 0);
}
if (error < 0 && error < set) {
analogWrite(motor1rechts, 0);
analogWrite(motor1links, abs(motorsnelheid));
}
if (error > 0 && error > -set) {
analogWrite(motor1rechts, abs(motorsnelheid));
analogWrite(motor1links, 0);
}
}
}
}
}
long microsecondenaarmilimeters(long microsecondes) {
return microsecondes * (0.343 / 2);}
