Étape 3: programmation
le codage que j’ai utilisé est l’un des
Jesper Birk
voiture autonome
Créée par Jesper Birk - 2014
int motorPin1 = 6 ; moteur fil connecté à la broche numérique 5
int motorPin2 = 5 ; moteur fil connecté à la broche numérique 6
int motorPin3 = 7 ; volant moteur fil connecté à la broche numérique 7
int motorPin4 = 8 ; volant moteur fil connecté à la broche numérique 8
int steeringMotorSpeed = 150 ; Puissance de la configuration par défaut pour envoyer au moteur de direction
int avoidDistance = 25 ;
int endAvoidDistance = 30 ;
int goToReverseDistance = 45 ;
Rebours int = 2000 ;
int fullTrottleMinDistance = 150 ;
int fullTrottleSpeed = 220 ;
cruiseSpeed int = 160 ;
int avoidSpeed = 120 ;
int reverseSpeed = 130 ;
int sensorDelay = 100 ; //
long randNumber ;
int avoidDirection = 1 ;
int avoidCirclesBeforeDirectionChange = 10 ;
le compte à rebours int = 1 ;
int distanceCountDown = 1 ;
#include « Ultrasonic.h »
Ultrasonic(12,13) ultrasonique ;
distance de l’int = (ultrasons. Ranging(cm)) ;
La méthode setup() s’exécute une seule fois, au démarrage de l’esquisse
void setup() {}
initialiser une communication série 9600 bits par seconde :
Serial.Begin(9600) ;
randomSeed(analogRead(0)) ;
initialiser les broches numériques en tant que sortie :
pinMode (motorPin1, sortie) ;
pinMode (motorPin2, sortie) ;
pinMode (motorPin3, sortie) ;
pinMode (motorPin4, sortie) ;
}
//////////// LOOP ////////////
void loop()
{
Serial.println (« boucle de départ ») ;
Serial.println (ultrasons. Ranging(cm)) ;
choseMode() ;
distance = (ultrasons. Ranging(cm)) ;
MODE REVERSE / / /
Si (distance < = goToReverseDistance) {}
avoidDirection = avoidDirection * -1 ;
while(distance < avoidDistance) {}
Serial.println ("inversion de démarrage") ;
if(avoidDirection == 1) {/ / une façon de tour
analogWrite (motorPin3, steeringMotorSpeed) ;
digitalWrite (motorPin4, basse) ;
} else {/ /... ou l’inverse
analogWrite (motorPin4, steeringMotorSpeed) ;
digitalWrite (motorPin3, basse) ;
}
analogWrite (motorPin2, reverseSpeed) ; rotation moteur - en arrière
digitalWrite (motorPin1, basse) ;
Delay(reverseTime) ; pour l’heure définie à rebours
Delay(sensorDelay) ;
distance = (ultrasons. Ranging(cm)) ;
// }
digitalWrite (motorPin2, basse) ; arrêter les moteurs
digitalWrite (motorPin3, basse) ;
digitalWrite (motorPin4, basse) ;
avoidDirection = avoidDirection * -1 ; commutateur de volant de direction
distance = (ultrasons. Ranging(cm)) ;
}
///////////////////////////////////////////////
ÉVITER DE MODE / / /
distance = (ultrasons. Ranging(cm)) ;
if(distance < avoidDistance) {}
Serial.println ("éviter de démarrer") ;
distance = (ultrasons. Ranging(cm)) ;
compte à rebours = avoidCirclesBeforeDirectionChange ;
distanceCountDown = distance ;
tandis que (distance < = endAvoidDistance & & distance > goToReverseDistance) {}
if(avoidDirection == 1) {}
analogWrite (motorPin3, steeringMotorSpeed) ; une façon de tourner
digitalWrite (motorPin4, basse) ;
} else {}
analogWrite (motorPin4, steeringMotorSpeed) ; ou tourner en sens inverse
digitalWrite (motorPin3, basse) ;
}
analogWrite (motorPin1, avoidSpeed) ; moteur de tourne
digitalWrite (motorPin2, basse) ; la valeur de la basse de motorPin2 de Pin
Delay(sensorDelay) ;
distance = (ultrasons. Ranging(cm)) ;
if(distance < distanceCountDown) {compte à rebours--;} / / si l’obstacle se rapproche - comte vers le bas pour changer de direction
Si (compte à rebours < 1) {}
avoidDirection = avoidDirection * -1 ; commutateur de volant de direction
compte à rebours = avoidCirclesBeforeDirectionChange ;
distanceCountDown = distance ;
}
Serial.println(distance) ;
Serial.println(avoidDirection) ;
} / / fin tandis que (à éviter)
digitalWrite (motorPin3, basse) ;
digitalWrite (motorPin4, basse) ;
}
///////////////////////////////
CROISIÈRE MODE / / /
distance = (ultrasons. Ranging(cm)) ;
tandis que (distance > avoidDistance & & distance < fullTrottleMinDistance) {}
analogWrite (motorPin1, cruiseSpeed) ; moteur de tourne
digitalWrite (motorPin2, basse) ; la valeur de la basse de motorPin2 de Pin
Delay(sensorDelay) ;
distance = (ultrasons. Ranging(cm)) ;
Serial.println("Cruise") ;
Serial.println(distance) ;
} / / fin tandis que
PLEINE VITESSE MODE / / /
distance = (ultrasons. Ranging(cm)) ;
while(distance > fullTrottleMinDistance) {}
analogWrite (motorPin1, fullTrottleSpeed) ; moteur de tourne
digitalWrite (motorPin2, basse) ; la valeur de la basse de motorPin2 de Pin
Delay(sensorDelay) ;
distance = (ultrasons. Ranging(cm)) ;
Serial.println("Full") ;
Serial.println(distance) ;
} / / fin tandis que
Serial.println ("boucle de redémarrage") ;
Serial.println(distance) ;
}