Arduino avoidance car (part of the code)

1. To achieve the car forward and back

void setup() {
 // put your setup code here, to run once:
   pinMode(10 , OUTPUT);
   pinMode(6 , OUTPUT);
   pinMode(5 , OUTPUT);
   pinMode(9 , OUTPUT);
}
void loop(){
 // put your main code here, to run repeatedly: 

  digitalWrite(5,HIGH);
  digitalWrite(6,LOW);
  digitalWrite(9,HIGH);
  digitalWrite(10,LOW);
  delay(1000);   
  digitalWrite(5,LOW);
  digitalWrite(6,HIGH);
  digitalWrite(9,LOW);
  digitalWrite(10,HIGH);
  delay(1000);   
}

2. To achieve near-infrared basic functions

void setup() {
  // put your setup code here, to run once:
   pinMode(10 , OUTPUT);
   pinMode(6 , OUTPUT);
   pinMode(5 , OUTPUT);
   pinMode(9 , OUTPUT);
   pinMode(14,INPUT);
}
void loop() {
  // put your main code here, to run repeatedly: 
  int a = digitalRead(14);
  if(a == 1)
  {
  digitalWrite(5,LOW);
  digitalWrite(6,HIGH);
  digitalWrite(9,LOW);
  digitalWrite(10,HIGH);
  delay(1000);
  }
  else{
  digitalWrite(5,HIGH);
  digitalWrite(6,LOW);
  digitalWrite(9,HIGH);         
  digitalWrite(10,LOW);   
 }
}

 

3. car obstacle avoidance:

void Setup () {
 // PUT Setup your code here Wallpaper, to RUN Once
     // outer infrared sensor 
   the pinMode ( 14 , the INPUT);    // A0 port 
   the pinMode ( 18 is , the INPUT);    // A4 port
    // (5,6) ; (9, 10 are) 
   the pinMode ( . 5 , the OUTPUT); 
   the pinMode ( . 6 , the OUTPUT); 
   the pinMode ( . 9 , the OUTPUT); 
   the pinMode ( 10 , the OUTPUT); 
} 

void Loop () {
   // PUT your code here Wallpaper main, to RUN Repeatedly: 
   // state acquisition signal; 1 is high, 0 low level 
  int a = digitalRead (14 );       // A0 
  int B = digitalRead ( 18 is );       // A4
   // the LOW is triggered, meaning here the wall face 
  IF (the LOW == A == B && the LOW) 
  { 
      turnback (); 
      Delay ( 100 ); 
      turnright (); 
      Delay ( 100 ); 
      Forward (); 
   } 
   the else  IF (A == B == HIGH && the LOW) 
   { 
      TurnLeft (); 
      Delay ( 100 ); 
      Forward (); 
    } 
    the else  IF (A = the LOW && == B = HIGH) 
    {
       turnright();
       delay(100);
       forward();
     }
     else if(a==HIGH && b==HIGH)
     {
         forward();
     }
}
void turnright()        //right
{
   digitalWrite(5,LOW);
   digitalWrite(6,LOW);
   digitalWrite(9,HIGH);
   digitalWrite(10,LOW);
}
void turnleft()        //left
{
   digitalWrite(5,HIGH);
   digitalWrite(6,LOW);
   digitalWrite(9,LOW);
   digitalWrite(10,LOW);
}

void turnback()        //back
{
    digitalWrite(5,LOW);
    digitalWrite(6,HIGH);
    digitalWrite(9,LOW);
    digitalWrite(10,HIGH);
}

void forward()        //strght
{
    digitalWrite(5,HIGH);
    digitalWrite(6,LOW);
    digitalWrite(9,HIGH);
    digitalWrite(10,LOW);
}

 

Guess you like

Origin www.cnblogs.com/if-it-is-possible/p/11406917.html