• arduino mega 避障报距小车


    流程图

    流程图

    硬件

    mega2560

    mega
    红外
    超声波
    电机驱动
    扩展板

    // Pin 13 has an LED connected on most Arduino boards.
    // give it a name:
    #include<string.h>
    //变量IO口定义
    int a;
    int a1=-1;
    int a2=-1;
    int a3=-1;
    char a11;
    char a22;
    char a33;
    
    int out13 = 13;
    int in12 = 12;
    int out1 = 11;
    int out2 = 10;
    int out3 = 9;
    int out4 = 8;
    
    int echo = 7;
    int trig = 6;
    int in1 = 5;
    int in2 = 4;
    int busy = 3;//喇叭状态
    
    // the setup routine runs once when you press reset:
    //启动 定义引脚输入输出
    void setup()    {                
        // initialize the digital pin as an output.
        pinMode(out1, OUTPUT); 
        pinMode(out2, OUTPUT); 
        pinMode(out3, OUTPUT); 
        pinMode(out4, OUTPUT); 
      
        pinMode(in1,   INPUT);
        pinMode(in2,   INPUT);
      
        pinMode(in12,   INPUT);
        pinMode(out13,   OUTPUT);
    
        pinMode(trig,   OUTPUT);
        pinMode(echo,   INPUT);
      
        pinMode(busy,INPUT);                          //初始化串口
        do Serial3.begin(9600);while(!Serial3); 
     
        Serial.begin(9600);                     //置 0
    }
    
    
    /*
    超声波模块
    */
    int chaoshengbo(){
        unsigned long time = 0;
        int time2 = 0;
        digitalWrite(trig, LOW);
        delay(1);
        digitalWrite(trig, HIGH);
        delayMicroseconds(20);
        digitalWrite(trig, LOW);
        time = pulseIn(echo,HIGH);
        time2 = time/58;
        // Serial.println(time/58);
        delay(300);
        return time2;
    }
    
    /*
    文本转语音模块
    */
    void laba()    {
        int err=0;
        int i=0;
        
        if(a>=100)a11=0x30+a1;
        a22=0x30+a2;
        a33=0x30+a3;
        char data[] = {0xc7,0xb0,0xb7,0xbd,a11,a22,a33,0xc0,0xe5,0xc3,0xd7};
        char tou[] = {0xFD,0x00,0x1f,0x01,0x00};
      
        
        tou[2] = 3+sizeof(data);
        for(i=0;i<sizeof(tou);i++)    {
            Serial3.write(tou[i]);
            err^=tou[i];
        }
        for(i= 0;i<sizeof(data);i++)    {
          Serial3.write(data[i]);
          err^=data[i];
        }
        Serial3.write(err);
        Serial.println(Serial3.read());   
    }
    
    /*
    左转语音
    */
    void laba1()    {
       int err=0;
        int i=0;
        char data[] = {0xD7,0xF3,0xD7,0xAA};
        char tou[] = {0xFD,0x00,0x1f,0x01,0x00};
      
        tou[2] = 3+sizeof(data);
        for(i=0;i<sizeof(tou);i++)    {
            Serial3.write(tou[i]);
            err^=tou[i];
        }
        for(i= 0;i<sizeof(data);i++)    {
          Serial3.write(data[i]);
          err^=data[i];
        }
        Serial3.write(err);
        Serial.println(Serial3.read());   
    }
    
    /*
    右转语音
    */
    void laba2()    {
        int err=0;
        int i=0;
        char data[] = {0xD3,0xD2,0xD7,0xAA};
        char tou[] = {0xFD,0x00,0x1f,0x01,0x00};
      
        tou[2] = 3+sizeof(data);
        for(i=0;i<sizeof(tou);i++)    {
            Serial3.write(tou[i]);
            err^=tou[i];
        }
        for(i= 0;i<sizeof(data);i++)    {
          Serial3.write(data[i]);
          err^=data[i];
        }
        Serial3.write(err);
        Serial.println(Serial3.read());   
    }
    
    /*
    循环执行功能
    */
    // the loop routine runs over and over again forever:
    void loop() {
        //Serial.println(time); 
        static int b,c = 0;
        a = chaoshengbo();
        a1=a/100;
        if(a<100)a2=a/10;
        else a2=a%100/10;
        a3=a%10;
        // Serial.println(a);
        if(a<=15.00) {c++;}
        else {c=0;}
        // Serial.println(c);
       
       if(2==c)   {
            c=0;
            digitalWrite(out1, HIGH);   // turn the LED on (HIGH is the voltage level)
            digitalWrite(out2, LOW);    // turn the LED off by making the voltage LOW
            digitalWrite(out3, LOW);    // turn the LED on (HIGH is the voltage level)
            digitalWrite(out4, HIGH);   // turn the LED off by making the voltage LOW
            delay(1000);
       }
      
        if( digitalRead(busy)==0 ) laba();
      
        digitalWrite(out1, HIGH);     // turn the LED on (HIGH is the voltage level)
        digitalWrite(out2, LOW);      // turn the LED off by making the voltage LOW
        digitalWrite(out3, HIGH);     // turn the LED on (HIGH is the voltage level)
        digitalWrite(out4, LOW);      // turn the LED off by making the voltage LOW
      
      digitalWrite(out1, HIGH);//you cuo wu
      
     while(digitalRead(in1)==0)  {
          laba2();   
          while(digitalRead(in1)==0)      {
            digitalWrite(out1, HIGH);    // turn the LED on (HIGH is the voltage level)
            digitalWrite(out2, LOW);     // turn the LED off by making the voltage LOW
            digitalWrite(out3, LOW);     // turn the LED on (HIGH is the voltage level)
            digitalWrite(out4, HIGH);    // turn the LED off by making the voltage LOW
          }
          digitalWrite(out2, LOW);      // turn the LED on (HIGH is the voltage level)
          digitalWrite(out1, LOW);      // turn the LED off by making the voltage LOW
          digitalWrite(out4, LOW);      // turn the LED on (HIGH is the voltage level)
          digitalWrite(out3, LOW);      // turn the LED off by making the voltage LOW      
      }
      
      
      while(digitalRead(in2)==0)  {
          laba1();
          while(digitalRead(in2)==0)      {
            digitalWrite(out2, HIGH);    // turn the LED on (HIGH is the voltage level)
            digitalWrite(out1, LOW);     // turn the LED off by making the voltage LOW
            digitalWrite(out4, LOW);     // turn the LED on (HIGH is the voltage level)
            digitalWrite(out3, HIGH);    // turn the LED off by making the voltage LOW
          }
          digitalWrite(out2, LOW);      // turn the LED on (HIGH is the voltage level)
          digitalWrite(out1, LOW);      // turn the LED off by making the voltage LOW
          digitalWrite(out4, LOW);      // turn the LED on (HIGH is the voltage level)
          digitalWrite(out3, LOW);      // turn the LED off by making the voltage LOW        
      }
    }
    
    http://www.cnblogs.com/OceanF/
  • 相关阅读:
    [SDOI2013]直径(树的直径)
    [ZJOI2012]旅游(树的直径)
    [SDOI2011]消防(树的直径)
    【模板】2-SAT 问题(2-SAT)
    [HNOI2006]公路修建问题
    速度限制(分层图)
    [JLOI2011]飞行路线(分层图)
    【洛谷 P3194】 [HNOI2008]水平可见直线 (单调栈)
    【洛谷 P3187】 [HNOI2007]最小矩形覆盖 (二维凸包,旋转卡壳)
    【洛谷 P1452】 Beauty Contest (二维凸包,旋转卡壳)
  • 原文地址:https://www.cnblogs.com/OceanF/p/9149456.html
Copyright © 2020-2023  润新知