Home > other >  Based on the arduino no.5 infrared tracking code by car
Based on the arduino no.5 infrared tracking code by car

Time:11-28

Novice arduino no.5 infrared tracking, pure don't send the code to the BBS


# define STOP 0
# define FORWARD 1
# define BACKWARD 2
# define STURNLEFT 3
# define STURNRIGHT 4
# define TURNLEFT 5
# define TURNRIGHT 6
# define TURNRIGHT90 7
# define TURNLEFT90 8
9 # define TURNLEFT360
# define TURNRIGHT360 10

# include & lt; Wire. H>
# include & lt; VL53L0X. H>
VL53L0X sensor;

Int leftA_PIN=A0;
Int leftB_PIN=A1;
Int righA_PIN=A2;
Int righB_PIN=A3;
Int righ_PWM=5;
Int left_PWM=6;

Int trac1=11;//from the right side of the front direction started sorting
Int trac2=10;
Int trac3=9;
Int trac4=8;
Int trac5=7;

Void setup ()
{
//motor pin initializing
Serial.begin(9600);
PinMode (leftA_PIN, the OUTPUT);
PinMode (leftB_PIN, the OUTPUT);
PinMode (righA_PIN, the OUTPUT);
PinMode (righB_PIN, the OUTPUT);
PinMode (righ_PWM, the OUTPUT);
PinMode (left_PWM, the OUTPUT);
PinMode (2, the OUTPUT);

Serial.begin(9600);
Wire. The begin ();
Sensor. The init ();
Sensor. The setTimeout (500);
Sensor. StartContinuous ();



//tracing module initialization D0 pin
PinMode (trac1, INPUT);
PinMode (trac2, INPUT);
PinMode (trac3, INPUT);
PinMode (trac4, INPUT);
PinMode (trac5, INPUT);
}

Void loop () {
Int buttonState=0;//mode switch to LOW - 0 tracking, bluetooth HIGH - 1
PinMode (4, INPUT);//mode switch
ButtonState=digitalRead (4);
If (buttonState==HIGH) {

While (Serial. The available ())
{
Char val=Serial. The read ();

If (val=='w') {
Serial. Println (" forward ");
Serial. Write (" forward ");
Serial. Println ();
MotorRun (FORWARD);
}
Else if (val=='s') {
Serial. Println (" back ");
Serial. Write (" back ");
Serial. Println ();
MotorRun (BACKWARD);

}

Else if (val=='d') {
Serial. Println (" right ");
Serial. Write (" right ");
Serial. Println ();
MotorRun (TURNRIGHT);
delay(200);
MotorRun (STOP);
}

Else if (val=='a') {
Serial. Println (" left ");
Serial. Write (" left ");
Serial. Println ();
MotorRun (TURNLEFT);
delay(200);
MotorRun (STOP);
}

Else if (val=='t') {
Serial. Println (" stop ");
Serial. Write (" stop ");
Serial. Println ();
MotorRun (STOP);
}
Else if (val=='l') {
Serial. Println (" in situ left 360 ");
Serial. Write (" in situ left 360 ");
Serial. Println ();
MotorRun (TURNLEFT360);
MotorRun (STOP);
}

Else if (val=='r') {
Serial. Println (" in situ right 360 ");
Serial. Write (" in situ right 360 ");
Serial. Println ();
MotorRun (TURNRIGHT360);
MotorRun (STOP);
}




}

}
The else
If (sensor. ReadRangeSingleMillimeters () & lt; 200)
{
DigitalWrite (2, HIGH);
delay(100);
DigitalWrite (2, LOW);

MotorRun (BACKWARD);
delay(500);
}

The else
Tracing ();
Delay (5);
}


Void motorRun (int CMD)
{
Serial. Print (sensor. ReadRangeSingleMillimeters ());
If (sensor. TimeoutOccurred ()) {Serial. Print (" TIMEOUT "); }

Serial. Println ();



The switch (CMD) {
Case FORWARD:
Serial. Println (" FORWARD ");//output state
DigitalWrite (leftA_PIN, LOW);
DigitalWrite (leftB_PIN, HIGH);
DigitalWrite (righA_PIN, LOW);
DigitalWrite (righB_PIN, HIGH);
AnalogWrite (left_PWM, 33);
AnalogWrite (righ_PWM, 30);//left forward 40 right wheel forward 40
break;
Case BACKWARD:
Serial. Println (" BACKWARD ");//output state
DigitalWrite (leftA_PIN, HIGH);
DigitalWrite (leftB_PIN, LOW);
DigitalWrite (righA_PIN, HIGH);
DigitalWrite (righB_PIN, LOW);
AnalogWrite (left_PWM, 25);
AnalogWrite (righ_PWM, 25);//revolver back 40 right wheel back 40
break;
Case TURNLEFT:
Serial. Println (" TURN LEFT ");//output state
DigitalWrite (leftA_PIN, LOW);
DigitalWrite (leftB_PIN, HIGH);
DigitalWrite (righA_PIN, LOW);
DigitalWrite (righB_PIN, HIGH);
AnalogWrite (left_PWM, 25);
AnalogWrite (righ_PWM, 55);//left forward 20 right wheel forward 45
Delay (50);
break;
Case STURNLEFT:
Serial. Println (" STURN LEFT ");//output state
DigitalWrite (leftA_PIN, LOW);
DigitalWrite (leftB_PIN, HIGH);
DigitalWrite (righA_PIN, LOW);
DigitalWrite (righB_PIN, HIGH);
AnalogWrite (left_PWM, 10);
AnalogWrite (righ_PWM, 35);//left forward 30 right wheel forward 45
break;

Case TURNRIGHT:
Serial. Println (" TURN RIGHT ");//output state
DigitalWrite (leftA_PIN, LOW);
DigitalWrite (leftB_PIN, HIGH);
DigitalWrite (righA_PIN, LOW);
DigitalWrite (righB_PIN, HIGH);
AnalogWrite (left_PWM, 55);
AnalogWrite (righ_PWM, 25);//45 right round revolver forward forward 20
break;

Case STURNRIGHT:
Serial. Println (" STURN RIGHT ");//output state
DigitalWrite (leftA_PIN, LOW);
DigitalWrite (leftB_PIN, HIGH);
DigitalWrite (righA_PIN, LOW);
DigitalWrite (righB_PIN, HIGH);
AnalogWrite (left_PWM, 35);
AnalogWrite (righ_PWM, 10);//left forward 45 right wheel forward 30
break;

Case TURNRIGHT90:
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related