Paso 4: Programación Arduino
-Arduino UNO
-Algunos hilos
Usa este código:
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;
int diferencia = 0;
int RoL (int A, int B)
{
if(A > B)
retorno 1;
if(B > A)
retorno 2;
Si (A = B).
return 0;
}
anular siga (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)
{
diff de int = ValSens_A - ValSens_B;
volver diff;
}
if(ValSens_A < ValSens_B)
{
diff de int = ValSens_B - ValSens_A;
volver diff;
}
}
void 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, salida);
pinMode (PinMotorA_2, salida);
pinMode (PinMotorB_1, salida);
pinMode (PinMotorB_2, salida);
pinMode (PinSens_A, entrada);
pinMode (PinSens_B, entrada);
}
void loop()
{
if(Difference == 0)
{
diferencia = 1;
}
ValSens_A = analogRead(PinSens_A);
ValSens_B = analogRead (PinSens_B) + 150; utilizar un número variable para equilibrar los sensores
RIS = RoL (ValSens_A, ValSens_B);
Seguir (Ris, 100);
Move(0);
Debug();
};