单片机教程网

电脑版
提示:原网页已由神马搜索转码, 内容由www.51hei.com提供.
查看:3304|回复:0

arduino简单用PWM控制小车制作(加超声波和OLED显示)源程序

[复制链接]
ID:573864发表于 2019-8-2 11:08|显示全部楼层
只是一个新手,有一些错误及不完善的地方麻烦提一下。亲测是可以简单的实现的。把引脚这些定义到自己创的.h文件有助于整理。

制作出来的实物图如下:
51hei图片_20190802110706.jpg
51hei图片_20190802110700.jpg


Arduino源程序如下:
  1. include"arduino.h"

  2. #define inputPin A5  //超声波触发引脚
  3. #define outputPin A4  //超声波接收引脚

  4. int fm = A0;
  5. /*int K1;
  6. int K2;*/
  7. int limit_switch0 = A1;


  8. const byte right_motor_encoder_a = 2;
  9. const byte right_motor_encoder_b = 7;
  10. const byte left_motor_encoder_a = 3;
  11. const byte left_motor_encoder_b = 12;
  12. int Right_motor_go =9; //L298P直流电机驱动板的使能端口连接到数字接口8
  13. int Right_motor_back =10; //L298P直流电机驱动板的转向端口连接到数字接口9
  14. int Left_motor_go =5; //L298P直流电机驱动板的使能端口连接到数字接口5
  15. int Left_motor_back =6; //L298P直流电机驱动板的转向端口连接到数字接口4

  16. double right_position;//右轮脉冲数
  17. double right_position_pre;
  18. double left_position;//左轮脉冲数
  19. double left_position_pre;
  20. boolean right_Direction;//右轮旋转方向
  21. boolean left_Direction;//左轮旋转方向
  22. double speed_right;
  23. double speed_left;
  24. boolean result;

  25. unsigned long before = 0;
  26. unsigned long current = 0;
  27. unsigned long interval = 100;
复制代码
  1. #include"zzz.h"
  2. #include "U8g2lib.h"
  3. #ifdef U8X8_HAVE_HW_SPI
  4. #include< SPI.h>
  5. #endif
  6. #ifdef U8X8_HAVE_HW_I2C
  7. #include< Wire.h>
  8. #endif
  9. U8G2_SSD1306_128X64_NONAME_1_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ A3, /* dc=*/ 4, /* reset=*/ 8);

  10. void setup()
  11. {  
  12.    Serial.begin(9600);//Initialize the serial port
  13.    u8g2.begin();
  14.    pinMode(fm,OUTPUT);
  15.    pinMode(inputPin,INPUT_PULLUP);
  16.    pinMode(outputPin,OUTPUT);
  17.    //初始化电机驱动IO为输出方式
  18.    pinMode(Left_motor_go,OUTPUT);
  19.    pinMode(Left_motor_back,OUTPUT);
  20.    pinMode(Right_motor_go,OUTPUT);
  21.    pinMode(Right_motor_back,OUTPUT);
  22.    pinMode(right_motor_encoder_a,INPUT_PULLUP);
  23.    pinMode(left_motor_encoder_a,INPUT_PULLUP);
  24.    pinMode(right_motor_encoder_b,INPUT);
  25.    pinMode(left_motor_encoder_b,INPUT);
  26.    pinMode(limit_switch,INPUT);
  27.   
  28.    EncoderInit();
  29. }
  30. void run()     // 前进
  31. {
  32.    digitalWrite(Right_motor_go,HIGH);  // 右电机前进
  33.    digitalWrite(Right_motor_back,LOW);    
  34.    analogWrite(Right_motor_go,52);
  35.    analogWrite(Right_motor_back,0);
  36.    digitalWrite(Left_motor_go,HIGH);  // 左电机前进
  37.    digitalWrite(Left_motor_back,LOW);
  38.    analogWrite(Left_motor_go,64);
  39.    analogWrite(Left_motor_back,0);
  40.   
  41. }
  42. void r_advance()
  43. {
  44. //  digitalWrite(Right_motor_go,HIGH);  // 右电机前进
  45. //   digitalWrite(Right_motor_back,LOW);    
  46.    analogWrite(Right_motor_go,80);
  47.    analogWrite(Right_motor_back,0);
  48.   // digitalWrite(Left_motor_go,LOW);  // 左电机前进
  49.   // digitalWrite(Left_motor_back,LOW);
  50.    analogWrite(Left_motor_go,0);
  51.    analogWrite(Left_motor_back,30);
  52. }
  53. void back()
  54. {
  55.    digitalWrite(Right_motor_go,LOW);  // 右电机前进
  56.    digitalWrite(Right_motor_back,HIGH);    
  57.    analogWrite(Right_motor_go,0);
  58.    analogWrite(Right_motor_back,68);
  59.    digitalWrite(Left_motor_go,LOW);  // 左电机前进
  60.    digitalWrite(Left_motor_back,HIGH);
  61.    analogWrite(Left_motor_go,0);
  62.    analogWrite(Left_motor_back,80);
  63. }

  64. void loop()
  65. {  
  66.    current = millis();
  67.    digitalWrite(outputPin, LOW);
  68.    delayMicroseconds(2);
  69.    digitalWrite(outputPin, HIGH); // Pulse for 10μs to trigger ultrasonic detection
  70.    delayMicroseconds(10);
  71.    digitalWrite(outputPin, LOW);
  72.    const unsigned long duration = pulseIn(inputPin, HIGH);// Read receiver pulse time
  73.    int distance = duration/58 ; // Transform pulse time to distance
  74.    if ( duration == 0 ){
  75.    Serial.println( "Warning: no pulse from sensor");
  76.    }
  77.    else{
  78.    Serial.print ( "distance is:" );
  79.    Serial.println ( distance );
  80.    }
  81.    delay(100);

  82.    if(distance<30){
  83.    back();
  84.    delay(500);
  85.    r_advance();
  86.    delay(300);
  87.    run();
  88.    }
  89.    else{
  90.    run();
  91.    }
  92.    if(distance<10){
  93.    back();
  94.    delay(1000);
  95.    r_advance();
  96.    delay(300);
  97.    run();
  98.    }
  99.    /*int i = analogRead(limit_switch);
  100.    if(i==0){
  101.        back();
  102.        delay(600);
  103.    }*/
  104.   
  105.   if((current=millis())-before > interval)
  106.      {
  107.        before = current;
  108.        Serial.print("left speed is:");
  109.        Serial.println(speed_left);
  110.        Serial.print("right speed is:");
  111.        Serial.println(speed_right);
  112.        speed_left=abs(left_position - left_position_pre)/300;
  113.        speed_right=abs(right_position - right_position_pre)/300;
  114.        left_position_pre = left_position;
  115.        right_position_pre = right_position;
  116.      }
  117.   u8g2.firstPage();
  118.   do {
  119.      u8g2.setFont(u8g2_font_courR12_tr);
  120.      u8g2.setCursor(0,15);
  121.      u8g2.print("distance:");
  122.      u8g2.setCursor(0,30);
  123.      u8g2.print(distance);
  124.      u8g2.print("cm");
  125.      u8g2.setCursor(0,45);
  126.      u8g2.print("l_s:");
  127.      u8g2.print((float)speed_left,2);    
  128.      u8g2.setCursor(0,60);
  129.      u8g2.print("r_s:");
  130.      u8g2.print((float)speed_right,2);  
  131.    } while ( u8g2.nextPage() );
  132.    delay(1000);
  133.    if(speed_left=0){
  134.    digitalWrite(fm,HIGH);
  135.    }
  136.    else{
  137.    digitalWrite(fm,LOW);
  138.    }
  139. }
  140.    /*if(speed_left>speed_right){
  141.    r_pwm++;
  142.    }else{
  143.    r_pwm++;
  144.    }
  145.    else if(speed_left=speed_right)
  146.    r_pwm=r_pwm;
  147.    }*/
  148. void EncoderInit()
  149. {
  150.   right_Direction = true;//default -> Forward  
  151.   pinMode(right_motor_encoder_b,OUTPUT);  
  152.   attachInterrupt(digitalPinToInterrupt(2), right_Speed, RISING);
  153.   left_Direction = true;//default -> Forward  
  154.   pinMode(left_motor_encoder_b,OUTPUT);  
  155.   attachInterrupt(digitalPinToInterrupt(3), left_Speed, RISING);
  156. }

  157. void  left_Speed()
  158. {
  159.    //Serial.println("left interrupt is ok");
  160.    int a = digitalRead(left_motor_encoder_a);
  161.    int b = digitalRead(left_motor_encoder_b);
  162.    if((a==HIGH&&b==LOW)||(a==LOW&&b==HIGH)){
  163.      left_position++;
  164.    }
  165.    else if((a==HIGH&&b==HIGH)||(a==LOW&&b==LOW)){
  166.        left_position--;
  167.      }
  168. }

  169. void  right_Speed()
  170. {
  171.      //Serial.println("right interrupt is ok");
  172.      int c = digitalRead(right_motor_encoder_a);
  173.      int d = digitalRead(right_motor_encoder_b);
  174.      if((c==HIGH&&d==LOW)||(c==LOW&&d==HIGH)){
  175.      right_position++;
  176.      }
  177.      else if((c==HIGH&&d==HIGH)||(c==LOW&&d==LOW)){
  178.        right_position--;
  179.        }
  180. }
复制代码
自己做的小车 有点丑
PWM.rar(2.18 KB, 下载次数: 14)

评分

黑币 +50
收起理由
+ 50
共享资料的黑币奖励!

查看全部评分

手机版|小黑屋|51黑电子论坛|51黑电子论坛6群QQ管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网