Automatic search and rescue vehicle based on arduino uno

Two cars are made, the program is basically the same, through Bluetooth control car forward, backward, left turn, right turn, you can check the temperature and humidity value, smoke concentration value, the distance of obstacles in front, and detect whether there is anyone nearby, will buzzer alarm. In addition, there are automatic obstacle avoidance programs, which can be used in cave search and rescue, mine rescue, etc., and directly paste the code below.

//Cart
#include <Arduino.h>
#include "DHT11.h"

boolean flag = true;

#define Sensor A5  //smoke
#define LED 13   //Small lamp
#define Sensor_DO 11  //smoke

#define NOTE_D0 -1
#define NOTE_D1 262
#define NOTE_D2 293
#define NOTE_D3 329
#define NOTE_D4 349
#define NOTE_D5 392
#define NOTE_D6 440
#define NOTE_D7 494
 
#define NOTE_DL1 147
#define NOTE_DL2 165
#define NOTE_DL3 175
#define NOTE_DL4 196
#define NOTE_DL5 221
#define NOTE_DL6 248
#define NOTE_DL7 278
 
 
 
#define NOTE_DH1 523
#define NOTE_DH2 586
#define NOTE_DH3 658
#define NOTE_DH4 697
#define NOTE_DH5 783
#define NOTE_DH6 879
#define NOTE_DH7 987
 
//In the above part, the definition is to match each note with the frequency value. In fact, you don't need to type so many notes, but you can write all kinds of songs in D-key at will. Here I use NOTE_D + numbers to represent notes, NOTE_DH + numbers to represent notes with dots above, and NOTE_DL + numbers to represent notes with dots below. In this way, it's easier to identify.
 
#define WHOLE 1
 
#define HALF 0.5
 
#define QUARTER 0.25
 
#define EIGHTH 0.25
#define SIXTEENTH 0.625
 
//This part corresponds to the beat in English, which makes the back look better

int tune[] = 
 
{
 
  NOTE_DH1,NOTE_D6,NOTE_D5,NOTE_D6,NOTE_D0,
 
  NOTE_DH1,NOTE_D6,NOTE_D5,NOTE_DH1,NOTE_D6,NOTE_D0,NOTE_D6,
 
  NOTE_D6,NOTE_D6,NOTE_D5,NOTE_D6,NOTE_D0,NOTE_D6,
 
  NOTE_DH1,NOTE_D6,NOTE_D5,NOTE_DH1,NOTE_D6,NOTE_D0,
 
  
 
  NOTE_D1,NOTE_D1,NOTE_D3,
 
  NOTE_D1,NOTE_D1,NOTE_D3,NOTE_D0,
 
  NOTE_D6,NOTE_D6,NOTE_D6,NOTE_D5,NOTE_D6,
 
  NOTE_D5,NOTE_D1,NOTE_D3,NOTE_D0,
 
  NOTE_DH1,NOTE_D6,NOTE_D6,NOTE_D5,NOTE_D6,
 
  NOTE_D5,NOTE_D1,NOTE_D2,NOTE_D0,
 
  NOTE_D7,NOTE_D7,NOTE_D5,NOTE_D3,
 
  NOTE_D5,
 
  NOTE_DH1,NOTE_D0,NOTE_D6,NOTE_D6,NOTE_D5,NOTE_D5,NOTE_D6,NOTE_D6,
 
  NOTE_D0,NOTE_D5,NOTE_D1,NOTE_D3,NOTE_D0,
 
  NOTE_DH1,NOTE_D0,NOTE_D6,NOTE_D6,NOTE_D5,NOTE_D5,NOTE_D6,NOTE_D6,
 
  NOTE_D0,NOTE_D5,NOTE_D1,NOTE_D2,NOTE_D0,
 
  NOTE_D3,NOTE_D3,NOTE_D1,NOTE_DL6,
 
  NOTE_D1,
 
  NOTE_D3,NOTE_D5,NOTE_D6,NOTE_D6,
 
  NOTE_D3,NOTE_D5,NOTE_D6,NOTE_D6,
 
  NOTE_DH1,NOTE_D0,NOTE_D7,NOTE_D5,
 
  NOTE_D6,
 
};//This part is the note part of the whole piece, which is defined by a sequence as tune, integer
 
 
 
float duration[]=
 
{
 
  1,1,0.5,0.5,1,
 
  0.5,0.5,0.5,0.5,1,0.5,0.5,
 
  0.5,1,0.5,1,0.5,0.5,
 
  0.5,0.5,0.5,0.5,1,1,
 
  
 
  1,1,1+1,
 
  0.5,1,1+0.5,1,
 
  1,1,0.5,0.5,1,
 
  0.5,1,1+0.5,1,
 
  0.5,0.5,0.5,0.5,1+1,
 
  0.5,1,1+0.5,1,
 
  1+1,0.5,0.5,1,
 
  1+1+1+1,
 
  0.5,0.5,0.5+0.25,0.25,0.5+0.25,0.25,0.5+0.25,0.25,
 
  0.5,1,0.5,1,1,
 
  0.5,0.5,0.5+0.25,0.25,0.5+0.25,0.25,0.5+0.25,0.25,
 
  0.5,1,0.5,1,1,
 
  1+1,0.5,0.5,1,
 
  1+1+1+1,
 
  0.5,1,0.5,1+1,
 
  0.5,1,0.5,1+1,
 
  1+1,0.5,0.5,1,
 
  1+1+1+1
 
};//This part is the capture part of the whole song. It also defines the sequence duration and floating point (the number of arrays is the same as the number of previous notes, one-to-one correspondence)
 
int length;//A variable is defined here, which is used to indicate how many notes there are in total
 
int tonePin=4;//pin of buzzer


DHT11 myDHT11(3);//Temperature and humidity

const int pirPin = 12;  //Human body infrared
 
int Echo=A3;  // Echo echo foot (P2.0)
int Trig=A2;  //  Trig trigger foot (P2.1)
const int negR=5;
const int posR=6;
const int negL=7;
const int posL=8;
const int pwmR=9;
const int pwmL=10;
const int Rspeed=200;
const int Lspeed=200;
const int rotSpeed=500;
const int servopin = 2;  //Steering gear interface

int opmode;//Storage trolley operation mode (manual and automatic): 7 manual and 8 automatic
int cmd;//Store the operation command of the trolley in the manual mode: 0 stop, 1 forward, 2 reverse, 4 left turn, 3 right turn, 5 ring, 6 Display
int blcmd;//Temporary Bluetooth command

int pirValue;    
int sec = 0;      //Detection times
char val;
int rightDistance = 0,leftDistance = 0,middleDistance = 0 ;
unsigned int SensorValue = 0;

      
void forward(byte RmotorSpeed, byte LmotorSpeed)
{
    analogWrite(pwmR,RmotorSpeed);
    analogWrite(pwmL,LmotorSpeed);
    digitalWrite(posR,LOW);
    digitalWrite(negR,HIGH);         
    digitalWrite(posL,LOW);
    digitalWrite(negL,HIGH);   
}  
void back(byte RmotorSpeed, byte LmotorSpeed)
{
    analogWrite(pwmR,RmotorSpeed);
    analogWrite(pwmL,LmotorSpeed);
    digitalWrite(posR,HIGH);
    digitalWrite(negR,LOW);         
    digitalWrite(posL,HIGH);
    digitalWrite(negL,LOW);   
} 
void pause(byte RmotorSpeed, byte LmotorSpeed)
{
    analogWrite(pwmR,RmotorSpeed);
    analogWrite(pwmL,LmotorSpeed);
    digitalWrite(posR,LOW);
    digitalWrite(negR,LOW);         
    digitalWrite(posL,LOW);
    digitalWrite(negL,LOW);   
} 
void right(byte RmotorSpeed, byte LmotorSpeed)
{
    analogWrite(pwmR,RmotorSpeed);
    analogWrite(pwmL,LmotorSpeed);
    digitalWrite(posR,LOW);
    digitalWrite(negR,LOW);         
    digitalWrite(posL,LOW);
    digitalWrite(negL,HIGH);        
} 
void left(byte RmotorSpeed, byte LmotorSpeed)
{
    analogWrite(pwmR,RmotorSpeed);
    analogWrite(pwmL,LmotorSpeed);
    digitalWrite(posR,LOW);
    digitalWrite(negR,HIGH);         
    digitalWrite(posL,LOW);
    digitalWrite(negL,LOW);        
} 
int Distance_test()   // Measure the distance ahead 
{
  digitalWrite(Trig, LOW);   // Give trigger pin low level 2 μ s
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);  // Give the trigger pin a high level of 10 μ s, at least 10 μ s here
  delayMicroseconds(20);
  digitalWrite(Trig, LOW);    // Low power to trigger foot continuously
  float Fdistance = pulseIn(Echo, HIGH);
  Fdistance= Fdistance/58;     
  return (int)Fdistance;
} 

void servopulse(int angle)//Define a pulse function
{
    int pulsewidth=(angle*11)+500;  //Convert the angle to a pulse width of 500-2480
    digitalWrite(servopin,HIGH);    //Turn the actuator interface level to high
    delayMicroseconds(pulsewidth);  //Microseconds of delay pulse width
    digitalWrite(servopin,LOW);     //Turn the interface level of the steering gear to low
    delayMicroseconds(20000-pulsewidth);
}

void bizhang()     
{
         for(int i=0;i<=50;i++)       //Generate the number of PWM, equivalent delay to ensure that the response angle can be turned
           {
              servopulse(90);    //Analog generated PWM
            }   
          delay(500); 
          middleDistance = Distance_test();  
            delay(500); 
          if(middleDistance<50)
          {
                pause(0,0);
     
                for(int i=0;i<=50;i++)       //Generate the number of PWM, equivalent delay to ensure that the response angle can be turned
                {
                  servopulse(45);    //Analog generated PWM
                }
                delay(500);
                rightDistance=Distance_test();  
                 for(int i=0;i<=50;i++)       //Generate the number of PWM, equivalent delay to ensure that the response angle can be turned
                {
                  servopulse(135);   
                }
                delay(500);
                leftDistance=Distance_test();         
                for(int i=0;i<=50;i++)       //Generate the number of PWM, equivalent delay to ensure that the response angle can be turned
                {
                  servopulse(90);    
                } 
                delay(500);
                if(rightDistance<50 && leftDistance<50)
                {
                    back(Rspeed,Lspeed);
                    delay(1500);
                    right(rotSpeed,rotSpeed);
                    delay(300);          
                }      
                else if(rightDistance>leftDistance)
                {
                  back(Rspeed,Lspeed);
                    delay(1500);
                    right(rotSpeed,rotSpeed);
                    delay(300);
                     middleDistance = Distance_test();  
                    delay(500);
                }
                else if(leftDistance>rightDistance)
                {
                  back(Rspeed,Lspeed);
                    delay(1500);
                    left(rotSpeed,rotSpeed);
                    delay(300);
                     middleDistance = Distance_test();  
                    delay(500);
                }    
            } 
        
}
void setup()
{
 
  opmode=7;//Set to manual mode
  pinMode(servopin,OUTPUT);//Set the servomotor interface as the output interface
  
  pinMode(Echo, INPUT);    // Define ultrasonic input foot
  pinMode(Trig, OUTPUT);   // Define ultrasonic output pin 
 
  pinMode(pirPin, INPUT);
  pinMode(Sensor_DO,INPUT);
  pinMode(Sensor,INPUT);
  pinMode(LED,OUTPUT);
  pinMode(tonePin,OUTPUT);
  pinMode(posR,OUTPUT);
  pinMode(negR,OUTPUT);
  pinMode(posL,OUTPUT);
  pinMode(negL,OUTPUT); 
  digitalWrite(LED, LOW);  
  length = sizeof(tune)/sizeof(tune[0]);//Here we use a sizeof function to find out how many notes there are in the tone sequence  
  Serial.begin(9600);
  Serial.println("Welcome to use!");  //Sent content
  Serial.println("Now it's manual mode");//Output "now in manual mode" to mobile phone
}
void loop() 
{
    int i;
    pirValue = digitalRead(pirPin);
    if(pirValue==1)
    {
      digitalWrite(LED, HIGH);
       for(i=0;i<80;i++)
             {
                digitalWrite(tonePin,HIGH);
                delay(1);
                digitalWrite(tonePin,LOW);
                delay(1);
              } 
      }
        
  while(Serial.available())
  {
    blcmd=Serial.read();
   blcmd=blcmd-'0';    
    if(blcmd==0)
    {
      Serial.println("Stop command received");
      cmd=blcmd;
    }
    if(blcmd==1)
    {
      Serial.println("Forward command received");
      cmd=blcmd;
    } 
    if(blcmd==2)
    {
      Serial.println("Reverse command received");
      cmd=blcmd;
    }
    if(blcmd==3)
    {
      Serial.println("Left turn command received");
       cmd=blcmd;
    }
     if(blcmd==4)
    {
      Serial.println("Right turn command received");
       cmd=blcmd;
    }
     if(blcmd==5)
     {
          Serial.println("Ring command received");
      cmd=blcmd;
      }
    if(blcmd==6)
    {
      Serial.println("Display command received");
      cmd=blcmd;
    } 
     if(blcmd==7)
    {
      Serial.println("Switch to manual mode");
      opmode=blcmd;
    }
     if(blcmd==8)
    {
      Serial.println("Switch to automatic mode");
      opmode=blcmd;
      cmd=10;
    }
  }     
  if((cmd==0)&&(opmode==7))
  {
    pause(0,0);
    Serial.println("Trolley stopped");
  }
   if((cmd==0)&&(opmode==7))
  {
    pause(0,0);
    Serial.println("Trolley stopped");
  }
  if((cmd==1)&&(opmode==7))
  {
   forward(Rspeed,Lspeed);
    Serial.println("The car is moving forward");
  }
  if((cmd==2)&&(opmode==7))
  {
   back(Rspeed,Lspeed);
    Serial.println("The car is backing up");
  }
  if((cmd==4)&&(opmode==7))
  {
   left(Rspeed,Lspeed);
    Serial.println("The car is turning left");
  }
  if((cmd==3)&&(opmode==7))
  {
   right(Rspeed,Lspeed);
    Serial.println("The car is turning right");
  }
   if((cmd==5)&&(opmode==7))
  {
    pause(0,0);
    Serial.println("The car is ringing");
    for(int x=0;x<length;x++)
  {
    tone(tonePin,tune[x]);//This function plays the array in the tune sequence in turn, that is, each note
 
    delay(400*duration[x]);//The duration of each note, i.e. beat duration, 400, is the greater the adjustment time, the slower the music speed, the smaller the music speed, the faster the music speed, so you can master it yourself
 
    noTone(tonePin);//Stop the current note and enter the next note
 
  }
  }
  if((cmd==6)&&(opmode==7))
  {
    pause(0,0);
    Serial.println("The car is being tested");
    myDHT11.DHT11_Read();               //Read the temperature and humidity value
          SensorValue = analogRead(Sensor);    //Read the analog value of Sensor pin, which is 0-1023
    
          sec += 1;   

          //Serial port
          Serial.print("Second: ");
          Serial.println(sec);

          Serial.print("HUMI = ");
          Serial.print(myDHT11.HUMI_Buffer_Int);
          Serial.println(" %RH");

          Serial.print("TMEP = ");
          Serial.print(myDHT11.TEM_Buffer_Int);
          Serial.println(" C");

          Serial.print("AD Value = ");
          Serial.println(SensorValue);      //Output analog value to serial port

          Serial.print("PIR value: ");
          Serial.print(pirValue);
          Serial.print('\n');
          delay(1000);        //Delay 1s
  }
  bizhang();
} 

/*Trolley
#include <Arduino.h>
#include "DHT11.h"

#define Sensor A5  //smoke
#define LED 13   //Small lamp
#define beep 4   //Buzzer
#define Sensor_DO 11  //smoke

DHT11 myDHT11(3);//Temperature and humidity

const int pirPin = 12;  //Human body infrared
 
int Echo = A3;  // Echo Echo foot (P2.0)
int Trig =A2;  //  Trig Trigger pin (P2.1)
const int negR=5;
const int posR=6;
const int negL=7;
const int posL=8;
const int pwmR=9;
const int pwmL=10;
const int Rspeed=120;
const int Lspeed=130;
const int rotSpeed=150;
const int servopin = 2;  //Steering gear interface

int pirValue;    
int sec = 0;      //Detection times
char val;
int rightDistance = 0,leftDistance = 0,middleDistance = 0 ;
unsigned int SensorValue = 0;

      
void forward(byte RmotorSpeed, byte LmotorSpeed)
{
    analogWrite(pwmR,RmotorSpeed);
    analogWrite(pwmL,LmotorSpeed);
    digitalWrite(posR,LOW);
    digitalWrite(negR,HIGH);         
    digitalWrite(posL,LOW);
    digitalWrite(negL,HIGH);   
}  
void back(byte RmotorSpeed, byte LmotorSpeed)
{
    analogWrite(pwmR,RmotorSpeed);
    analogWrite(pwmL,LmotorSpeed);
    digitalWrite(posR,HIGH);
    digitalWrite(negR,LOW);         
    digitalWrite(posL,HIGH);
    digitalWrite(negL,LOW);   
} 
void pause(byte RmotorSpeed, byte LmotorSpeed)
{
    analogWrite(pwmR,RmotorSpeed);
    analogWrite(pwmL,LmotorSpeed);
    digitalWrite(posR,LOW);
    digitalWrite(negR,LOW);         
    digitalWrite(posL,LOW);
    digitalWrite(negL,LOW);   
} 
void right(byte RmotorSpeed, byte LmotorSpeed)
{
    analogWrite(pwmR,RmotorSpeed);
    analogWrite(pwmL,LmotorSpeed);
    digitalWrite(posR,LOW);
    digitalWrite(negR,LOW);         
    digitalWrite(posL,LOW);
    digitalWrite(negL,HIGH);        
} 
void left(byte RmotorSpeed, byte LmotorSpeed)
{
    analogWrite(pwmR,RmotorSpeed);
    analogWrite(pwmL,LmotorSpeed);
    digitalWrite(posR,LOW);
    digitalWrite(negR,HIGH);         
    digitalWrite(posL,LOW);
    digitalWrite(negL,LOW);        
} 
int Distance_test()   // Measure the distance ahead 
{
  digitalWrite(Trig, LOW);   // Give trigger pin low level 2 μ s
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);  // Give the trigger pin a high level of 10 μ s, at least 10 μ s here
  delayMicroseconds(20);
  digitalWrite(Trig, LOW);    // Low power to trigger foot continuously
  float Fdistance = pulseIn(Echo, HIGH);
  Fdistance= Fdistance/58;     
  return (int)Fdistance;
} 

void servopulse(int angle)//Define a pulse function
{
    int pulsewidth=(angle*11)+500;  //Convert the angle to a pulse width of 500-2480
    digitalWrite(servopin,HIGH);    //Turn the actuator interface level to high
    delayMicroseconds(pulsewidth);  //Microseconds of delay pulse width value
    digitalWrite(servopin,LOW);     //Turn the interface level of the steering gear to low
    delayMicroseconds(20000-pulsewidth);
}

void setup()
{
  pinMode(servopin,OUTPUT);//Set the servomotor interface as the output interface
  
  pinMode(Echo, INPUT);    // Define ultrasonic input foot
  pinMode(Trig, OUTPUT);   // Define ultrasonic output pin 
 
  pinMode(pirPin, INPUT);
  pinMode(Sensor_DO,INPUT);
  pinMode(Sensor,INPUT);
  pinMode(LED,OUTPUT);
  pinMode(beep,OUTPUT);
  pinMode(posR,OUTPUT);
  pinMode(negR,OUTPUT);
  pinMode(posL,OUTPUT);
  pinMode(negL,OUTPUT); 
  digitalWrite(LED, LOW);   
  Serial.begin(9600);
  Serial.println("Welcome to use!");  //Sent content
}
void loop() 
{
    int flag;
    pirValue = digitalRead(pirPin);
    digitalWrite(LED, pirValue);
    digitalWrite(beep, pirValue);
    if(Distance_test()<50)
    {
                pause(0,0);
                delay(1000);
                flag=0;
    }       
  if(Serial.available())
  {
    val=Serial.read();
    val=val-'0';    
    if(val==0) 
    { 
       flag=0;
       pause(0,0); 
    }
    else if(val==1)
    {
      flag=0;
       forward(Rspeed,Lspeed);
    } 
    else if(val==2)
    {
      flag=0;
       back(Rspeed,Lspeed);
    }
    else if(val==3)
    {
      flag=0;
       right(Rspeed,Lspeed);
    }
    else if(val==4)
    {
      flag=0;
       left(Rspeed,Lspeed);
    }
     else if(val==5)
     {
              flag=0;
              digitalWrite(LED,HIGH);    //Set output high level
             for(int i = 0 ; i < 1000 ; i++)    //Cycle 100 times
              {
                digitalWrite(beep,HIGH);    //Set output high level
                 delayMicroseconds(1000); //Delay PotBuffer value us
                digitalWrite(beep,LOW);     //Set output low level
                  delayMicroseconds(1000);       //Delay 100us
               }  
           
      }
     else if(val==6)
     {
          flag=0;
          myDHT11.DHT11_Read();               //Read the temperature and humidity value
          SensorValue = analogRead(Sensor);    //Read the analog value of Sensor pin, which is 0-1023
    
          sec += 1;   

          //Serial port
          Serial.print("Second: ");
          Serial.println(sec);

          Serial.print("HUMI = ");
          Serial.print(myDHT11.HUMI_Buffer_Int);
          Serial.println(" %RH");

          Serial.print("TMEP = ");
          Serial.print(myDHT11.TEM_Buffer_Int);
          Serial.println(" C");

          Serial.print("AD Value = ");
          Serial.println(SensorValue);      //Output analog value to serial port

          Serial.print("PIR value: ");
          Serial.print(pirValue);
          Serial.print('\n');
          delay(1000);        //Delay 1s
       } 
       else if(val==7)
       {
            flag=1;
        }
       if(flag==1)
       {
       
         for(int i=0;i<=50;i++)       //Generate the number of PWM, equivalent delay to ensure that the response angle can be turned
           {
              servopulse(90);    //Analog generated PWM
            }   
          delay(500); 
          middleDistance = Distance_test();
         // #ifdef send
          Serial.print("middleDistance=");
          Serial.println(middleDistance);
        //  #endif

          if(middleDistance<50)
          {
                pause(0,0);   
                for(int i=0;i<=50;i++)       //Generate the number of PWM, equivalent delay to ensure that the response angle can be turned
                {
                  servopulse(45);    //Analog generated PWM
                }
                delay(500);
                rightDistance=Distance_test();  
                 for(int i=0;i<=50;i++)       //Generate the number of PWM, equivalent delay to ensure that the response angle can be turned
                {
                  servopulse(135);   
                }
                delay(500);
                leftDistance=Distance_test();         
                for(int i=0;i<=50;i++)       //Generate the number of PWM, equivalent delay to ensure that the response angle can be turned
                {
                  servopulse(90);    
                } 
                delay(500);
                if(rightDistance<25 && leftDistance<25)
                {
                    back(Rspeed,Lspeed);
                    delay(100);
                    right(rotSpeed,rotSpeed);
                    delay(50);
                    forward(Rspeed,Lspeed);
                }      
                else if(rightDistance>leftDistance)
                {
                    right(rotSpeed,rotSpeed);
                    delay(50);
                }
                else if(leftDistance>rightDistance)
                {
                    left(rotSpeed,rotSpeed);
                    delay(50);  
                }    
            } 
            else
                forward(Rspeed,Lspeed); 
        
       }
  }     
} */
Published 36 original articles, won praise 17, visited 20000+
Private letter follow

Tags: Mobile

Posted on Sat, 14 Mar 2020 07:15:12 -0700 by suttercain