Ⅰ 求arino避障小車程序,急!(高額懸賞)
//舵機和超聲波調試代碼
#include <Servo.h>
#include <Metro.h>
Metro measureDistance = Metro(50);
Metro sweepServo = Metro(20);
unsigned long actualDistance = 0;
Servo myservo; //創建舵機
int pos = 60;
int sweepFlag = 1;
int URPWM = 3; //PWM輸出0-25000us,每50us代表1cm
int URTRIG= 10; // PWM trigger pin PWM串口為10
uint8_t EnPwmCmd[4]={0x44,0x02,0xbb,0x01}; // distance measure command 距離測量命令
void setup(){ // Serial initialization 串列初始化
myservo.attach(9); //舵機串口為9
Serial.begin(9600); // Sets the baud rate to 9600
SensorSetup();
}
void loop(){
if(measureDistance.check() == 1){
actualDistance = MeasureDistance();
// Serial.println(actualDistance);
// delay(100);
}
if(sweepServo.check() == 1){
servoSweep();
}
}
void SensorSetup(){
pinMode(URTRIG,OUTPUT); // A low pull on pin COMP/TRIG
digitalWrite(URTRIG,HIGH); // Set to HIGH
pinMode(URPWM, INPUT); // Sending Enable PWM mode command 發送使能控制模式命令
for(int i=0;i<4;i++){
Serial.write(EnPwmCmd[i]);
}
}
int MeasureDistance(){ // a low pull on pin COMP/TRIG triggering a sensor reading 觸發感測器讀數
digitalWrite(URTRIG, LOW);
digitalWrite(URTRIG, HIGH); // reading Pin PWM will output pulses 讀引腳脈寬調制將輸出脈沖
unsigned long distance=pulseIn(URPWM,LOW);
if(distance==50000){ // the reading is invalid.閱讀無效
Serial.print("Invalid");
}else{
distance=distance/50; // every 50us low level stands for 1cm
}
return distance;
}
void servoSweep(){
if(sweepFlag ){
if(pos>=60 && pos<=120){
pos=pos+1; // in steps of 1 degree 1度角度的轉動
myservo.write(pos); // tell servo to go to position in variable 'pos' 告訴舵機轉動的角度
}
if(pos>119) sweepFlag = false; // assign the variable again 重新分配變數
}else {
if(pos>=60 && pos<=120){
pos=pos-1;
myservo.write(pos);
}
if(pos<61) sweepFlag = true;
}
}
////////////////////////////////////////////////////////////
需要載入一個Metro.h的庫,這只是調試機器,餘下的完全看你的發揮了,加上電機
Ⅱ arino小車前進代碼
小車前進依賴車輪,車輪要連接電機,電機運轉需要連接電機驅動器以獲得前進速度、方向都控制。arino要控制小車前進,實際上需要控制電機驅動器。
因此,arino小車前進的代碼要適配你的電機驅動器是哪一個,並沒有通用的代碼。