Étape 3: Programar Arduino
#include < Servo.h >
Sonar de servo ; Créez l’objet servo pour contrôler un servo
Servo timon ; objeto contrôle timon
Servo-moteur ;
int configPin = 13 ;
void setup()
{
sonar.Attach(9) ;
Timon.Attach(8) ;
Motor.Attach(11) ;
Serial.Begin(9600) ;
pinMode(configPin,OUTPUT) ;
inicializo moteur
int VelMin = 60 ;
int VelMax = 100 ;
pour (int i = 60; i < 100; i ++)
{
Motor.Write(i) ;
Delay(100) ;
}
incializo timon
int TimMin = 0 ;
int TimMax = 90 ;
pour (int i = TimMin; i < TimMax; i ++)
{
Timon.Write(i) ;
Delay(10) ;
}
inicializo sonar
int SonarMin = 0 ;
int SonarMax = 90 ;
pour (int i = SonarMin; i < SonarMax; i ++)
{
sonar.Write(i) ;
Delay(10) ;
}
}
////////////////
Funciones / /
//////////////
int Detecto_Objeto (int pos)
{
fr de servo de Pongo 0,90,180
sonar.Write(POS) ;
Delay(250) ;
mido la distancia
digitalWrite(configPin,HIGH) ; soulever la broche reset haute
Delay(250) ; début de la bague d’étalonnage
float sensorvalue = analogRead(5) ; valoriser un capteur analogique broche 5
float cmvalue = (1,25 * sensorvalue + 3,75) ;
Serial.Print("distancia=") ; Imprimez du texte de pouce
Serial.println(cmvalue) ; imprimer la valeur en pouce
Delay(15) ; retard en option 1 seconde
digitalWrite(configPin,LOW) ; arrêter le capteur et bague d’étalonnage
/*Serial.Print ("pos/Distancia =") ;
Serial.Print(POS) ;
Serial.Print("/") ;
Serial.Print(cmvalue) ;
Serial.Print("\n"); * /
Return cmvalue ;
}
Funciones de movimiento
Sub Aceleracion(int vel)
{
int i = 0 ;
Serial.Print ("Aceleracion") ;
Serial.Print(vel) ;
Serial.Print("\n") ;
pour (i = 60; i < vel; i ++)
{
Motor.Write(i) ;
Delay(100) ;
Serial.Print ("pos/vel") ;
Serial.Print(i) ;
Serial.Print("/") ;
Serial.Print(vel) ;
Serial.Print("\n") ;
}
Delay(1000) ;
}
Sub VehiculoCTL(int pos)
{
int Adelante = 90 ;
int Derecha = 0 ;
int Izquierda = 180 ;
int Vel ;
Timon.Write(POS) ;
Aceleracion(70) ;
Delay(150) ;
Serial.Print ("pos/vel") ;
Serial.Print(POS) ;
Serial.Print("/") ;
Serial.Print(vel) ;
Serial.Print("\n") ;
Delay(100) ;
}
/////////////////////////
Programa Principal / /
///////////////////////
void loop()
{
Objeto Detecto
int pos = 0 ;
int DistChoque = 30 ;
int Adelante = 90 ;
int Derecha = 0 ;
int Izquierda = 180 ;
Si (Detecto_Objeto(Adelante) > DistChoque)
VehiculoCTL(Adelante) ;
d’autre
Si (Detecto_Objeto(Derecha) > DistChoque)
VehiculoCTL(Izquierda) ;
d’autre
Si (Detecto_Objeto(Izquierda) > DistChoque)
VehiculoCTL(Derecha) ;
}