只是一個新手,有一些錯誤及不完善的地方麻煩提一下。親測是可以簡單的實現的。把引腳這些定義到自己創的.h文件有助于整理。
制作出來的實物圖如下:
51hei圖片_20190802110706.jpg (493.27 KB, 下載次數: 49)
下載附件
2019-8-2 11:07 上傳
51hei圖片_20190802110700.jpg (442.49 KB, 下載次數: 48)
下載附件
2019-8-2 11:07 上傳
Arduino源程序如下:
- include"arduino.h"
- #define inputPin A5 //超聲波觸發引腳
- #define outputPin A4 //超聲波接收引腳
- int fm = A0;
- /*int K1;
- int K2;*/
- int limit_switch0 = A1;
- const byte right_motor_encoder_a = 2;
- const byte right_motor_encoder_b = 7;
- const byte left_motor_encoder_a = 3;
- const byte left_motor_encoder_b = 12;
- int Right_motor_go =9; //L298P直流電機驅動板的使能端口連接到數字接口8
- int Right_motor_back =10; //L298P直流電機驅動板的轉向端口連接到數字接口9
- int Left_motor_go =5; //L298P直流電機驅動板的使能端口連接到數字接口5
- int Left_motor_back =6; //L298P直流電機驅動板的轉向端口連接到數字接口4
- double right_position;//右輪脈沖數
- double right_position_pre;
- double left_position;//左輪脈沖數
- double left_position_pre;
- boolean right_Direction;//右輪旋轉方向
- boolean left_Direction;//左輪旋轉方向
- double speed_right;
- double speed_left;
- boolean result;
- unsigned long before = 0;
- unsigned long current = 0;
- unsigned long interval = 100;
復制代碼- #include"zzz.h"
- #include "U8g2lib.h"
- #ifdef U8X8_HAVE_HW_SPI
- #include <SPI.h>
- #endif
- #ifdef U8X8_HAVE_HW_I2C
- #include <Wire.h>
- #endif
- U8G2_SSD1306_128X64_NONAME_1_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ A3, /* dc=*/ 4, /* reset=*/ 8);
- void setup()
- {
- Serial.begin(9600);//Initialize the serial port
- u8g2.begin();
- pinMode(fm,OUTPUT);
- pinMode(inputPin,INPUT_PULLUP);
- pinMode(outputPin,OUTPUT);
- //初始化電機驅動IO為輸出方式
- pinMode(Left_motor_go,OUTPUT);
- pinMode(Left_motor_back,OUTPUT);
- pinMode(Right_motor_go,OUTPUT);
- pinMode(Right_motor_back,OUTPUT);
- pinMode(right_motor_encoder_a,INPUT_PULLUP);
- pinMode(left_motor_encoder_a,INPUT_PULLUP);
- pinMode(right_motor_encoder_b,INPUT);
- pinMode(left_motor_encoder_b,INPUT);
- pinMode(limit_switch,INPUT);
-
- EncoderInit();
- }
- void run() // 前進
- {
- digitalWrite(Right_motor_go,HIGH); // 右電機前進
- digitalWrite(Right_motor_back,LOW);
- analogWrite(Right_motor_go,52);
- analogWrite(Right_motor_back,0);
- digitalWrite(Left_motor_go,HIGH); // 左電機前進
- digitalWrite(Left_motor_back,LOW);
- analogWrite(Left_motor_go,64);
- analogWrite(Left_motor_back,0);
-
- }
- void r_advance()
- {
- // digitalWrite(Right_motor_go,HIGH); // 右電機前進
- // digitalWrite(Right_motor_back,LOW);
- analogWrite(Right_motor_go,80);
- analogWrite(Right_motor_back,0);
- // digitalWrite(Left_motor_go,LOW); // 左電機前進
- // digitalWrite(Left_motor_back,LOW);
- analogWrite(Left_motor_go,0);
- analogWrite(Left_motor_back,30);
- }
- void back()
- {
- digitalWrite(Right_motor_go,LOW); // 右電機前進
- digitalWrite(Right_motor_back,HIGH);
- analogWrite(Right_motor_go,0);
- analogWrite(Right_motor_back,68);
- digitalWrite(Left_motor_go,LOW); // 左電機前進
- digitalWrite(Left_motor_back,HIGH);
- analogWrite(Left_motor_go,0);
- analogWrite(Left_motor_back,80);
- }
- void loop()
- {
- current = millis();
- digitalWrite(outputPin, LOW);
- delayMicroseconds(2);
- digitalWrite(outputPin, HIGH); // Pulse for 10μs to trigger ultrasonic detection
- delayMicroseconds(10);
- digitalWrite(outputPin, LOW);
- const unsigned long duration = pulseIn(inputPin, HIGH);// Read receiver pulse time
- int distance = duration/58 ; // Transform pulse time to distance
- if ( duration == 0 ){
- Serial.println( "Warning: no pulse from sensor");
- }
- else{
- Serial.print ( "distance is:" );
- Serial.println ( distance );
- }
- delay(100);
- if(distance<30){
- back();
- delay(500);
- r_advance();
- delay(300);
- run();
- }
- else{
- run();
- }
- if(distance<10){
- back();
- delay(1000);
- r_advance();
- delay(300);
- run();
- }
- /*int i = analogRead(limit_switch);
- if(i==0){
- back();
- delay(600);
- }*/
-
- if((current=millis())-before > interval)
- {
- before = current;
- Serial.print("left speed is:");
- Serial.println(speed_left);
- Serial.print("right speed is:");
- Serial.println(speed_right);
- speed_left=abs(left_position - left_position_pre)/300;
- speed_right=abs(right_position - right_position_pre)/300;
- left_position_pre = left_position;
- right_position_pre = right_position;
- }
- u8g2.firstPage();
- do {
- u8g2.setFont(u8g2_font_courR12_tr);
- u8g2.setCursor(0,15);
- u8g2.print("distance:");
- u8g2.setCursor(0,30);
- u8g2.print(distance);
- u8g2.print("cm");
- u8g2.setCursor(0,45);
- u8g2.print("l_s:");
- u8g2.print((float)speed_left,2);
- u8g2.setCursor(0,60);
- u8g2.print("r_s:");
- u8g2.print((float)speed_right,2);
- } while ( u8g2.nextPage() );
- delay(1000);
- if(speed_left=0){
- digitalWrite(fm,HIGH);
- }
- else{
- digitalWrite(fm,LOW);
- }
- }
- /*if(speed_left>speed_right){
- r_pwm++;
- }else{
- r_pwm++;
- }
- else if(speed_left=speed_right)
- r_pwm=r_pwm;
- }*/
- void EncoderInit()
- {
- right_Direction = true;//default -> Forward
- pinMode(right_motor_encoder_b,OUTPUT);
- attachInterrupt(digitalPinToInterrupt(2), right_Speed, RISING);
- left_Direction = true;//default -> Forward
- pinMode(left_motor_encoder_b,OUTPUT);
- attachInterrupt(digitalPinToInterrupt(3), left_Speed, RISING);
- }
- void left_Speed()
- {
- //Serial.println("left interrupt is ok");
- int a = digitalRead(left_motor_encoder_a);
- int b = digitalRead(left_motor_encoder_b);
- if((a==HIGH&&b==LOW)||(a==LOW&&b==HIGH)){
- left_position++;
- }
- else if((a==HIGH&&b==HIGH)||(a==LOW&&b==LOW)){
- left_position--;
- }
- }
-
- void right_Speed()
- {
- //Serial.println("right interrupt is ok");
- int c = digitalRead(right_motor_encoder_a);
- int d = digitalRead(right_motor_encoder_b);
- if((c==HIGH&&d==LOW)||(c==LOW&&d==HIGH)){
- right_position++;
- }
- else if((c==HIGH&&d==HIGH)||(c==LOW&&d==LOW)){
- right_position--;
- }
- }
復制代碼 自己做的小車 有點丑
PWM.rar
(2.18 KB, 下載次數: 14)
2019-8-2 11:08 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|