Étape 4: Programmation Arduino
-Arduino UNO
-Quelques fils
Utilisez ce code :
int PinSens_A = A5 ;
int PinSens_B = A4 ;
int ValSens_A = 0 ;
int ValSens_B = 0 ;
int Ris ;
int PinMotorA_1 = 10 ;
int PinMotorA_2 = 11 ;
int PinMotorB_1 = 5 ;
int PinMotorB_2 = 6 ;
int SpeedMotor_A ;
int SpeedMotor_B ;
différence d’int = 0 ;
int lei roumains (int A, int B)
{
if(A > B)
Return 1 ;
if(B > A)
retour 2 ;
Si (A = B).
return 0 ;
}
void suivre (int val, int speedMax)
{
if(Val == 2)
{
SpeedMotor_A = speedMax ;
SpeedMotor_B = 0 ;
}
if(Val == 1)
{
SpeedMotor_A = 0 ;
SpeedMotor_B = speedMax ;
}
}
int Move (int dir)
{
if(dir == 0)
{
analogWrite (PinMotorA_1, SpeedMotor_A) ;
analogWrite (PinMotorB_1, SpeedMotor_B) ;
}
if(dir == 1)
{
analogWrite (PinMotorA_2, SpeedMotor_A) ;
analogWrite (PinMotorB_2, SpeedMotor_B) ;
}
}
int calcDiff()
{
ValSens_A = analogRead(PinSens_A) ;
ValSens_B = analogRead(PinSens_B) ;
if(ValSens_A > ValSens_B)
{
int diff = ValSens_A - ValSens_B ;
retourner la diff ;
}
if(ValSens_A < ValSens_B)
{
int diff = ValSens_B - ValSens_A ;
retourner la diff ;
}
}
Sub Debug()
{
Serial.Print(RIS) ;
Serial.Print("") ;
Serial.Print(ValSens_A) ;
Serial.Print("") ;
Serial.Print(ValSens_B) ;
Serial.println("") ;
}
void setup()
{
Serial.Begin(9600) ;
pinMode (PinMotorA_1, sortie) ;
pinMode (PinMotorA_2, sortie) ;
pinMode (PinMotorB_1, sortie) ;
pinMode (PinMotorB_2, sortie) ;
pinMode (entrée, PinSens_A) ;
pinMode (entrée, PinSens_B) ;
}
void loop()
{
if(difference == 0)
{
différence = 1 ;
}
ValSens_A = analogRead(PinSens_A) ;
ValSens_B = analogRead (PinSens_B) + 150 ; Utilisez un nombre variable d’équilibrer les capteurs
RIS = RoL (ValSens_A, ValSens_B) ;
Suivre à distance (Ris, 100) ;
Move(0) ;
Debug() ;
};