My first Blogs:基于arduino uno的四轮(前后超声波,两侧红外)智能避障小车
/*所需材料:
1#arduino uno板子
2#L293D电极驱动扩展版
3#4个减速电机TT马达
4#两个超声波HC-SR04
5#两个红外循迹避障模块
6#一部分母对母杜邦线
7#18650电池两节和一个电池盒
代码部分
插入代码:
#include<NewPing.h>
#include<Servo.h>
#include<AFMotor.h>
#define SONAR_NUM 2
#define RIGHT A4
#define LEFT A5
#define TRIGGER1_PIN A0
#define ECHO1_PIN A1
#define TRIGGER2_PIN A3
#define ECHO2_PIN A2
#define MAX1_DISTANCE 100
#define MAX2_DISTANCE 100
NewPing sonar[2] = {
NewPing(TRIGGER1_PIN, ECHO1_PIN, MAX1_DISTANCE),
NewPing(TRIGGER2_PIN, ECHO2_PIN, MAX2_DISTANCE)};
AF_DCMotor Motor1(1,MOTOR12_1KHZ);
AF_DCMotor Motor2(2,MOTOR12_1KHZ);
AF_DCMotor Motor3(3,MOTOR34_1KHZ);
AF_DCMotor Motor4(4,MOTOR34_1KHZ);
int pos =0;
int i;
void setup() {
Serial.begin(9600);
pinMode(RIGHT, INPUT);
pinMode(LEFT, INPUT);
}
void loop() {
delay(50);
unsigned int head=sonar[0].ping_cm();
Serial.print(“head”);
Serial.println(head);
delay(50);
unsigned int rear=sonar[1].ping_cm();
Serial.print(“rear”);
Serial.println(rear); //序列谱
int Right_Value = digitalRead(RIGHT);
int Left_Value = digitalRead(LEFT);
Serial.print(“RIGHT”);
Serial.println(Right_Value);
Serial.print(“LEFT”);
Serial.println(Left_Value);
if((Right_Value1)&&(Left_Value1)&&(head>15 && head<100)){
Motor1.setSpeed(150);
Motor1.run(FORWARD);
Motor2.setSpeed(150);
Motor2.run(FORWARD);
Motor3.setSpeed(150);
Motor3.run(FORWARD);
Motor4.setSpeed(150);
Motor4.run(FORWARD);}
else if((Right_Value0)&&(Left_Value1)&&(head>15 && head<100)){
for(i=1000;i>0;i–){
Motor1.setSpeed(0);
Motor1.run(FORWARD);
Motor2.setSpeed(150);
Motor2.run(FORWARD);
Motor3.setSpeed(150);
Motor3.run(FORWARD);
Motor4.setSpeed(0);
Motor4.run(FORWARD);}}
else if((Right_Value1)&&(Left_Value0)&&(head>15 && head<100)){
for(i=1000;i>0;i–){
Motor1.setSpeed(150);
Motor1.run(FORWARD);
Motor2.setSpeed(0);
Motor2.run(FORWARD);
Motor3.setSpeed(0);
Motor3.run(FORWARD);
Motor4.setSpeed(150);
Motor4.run(FORWARD);}}
else if(((Right_Value0)&&(Left_Value0))&&(head>15 && head<100)){
Motor1.setSpeed(150);
Motor1.run(FORWARD);
Motor2.setSpeed(150);
Motor2.run(FORWARD);
Motor3.setSpeed(150);
Motor3.run(FORWARD);
Motor4.setSpeed(150);
Motor4.run(FORWARD);}
else if((head>1 && head<15)&&((Right_Value0)&&(Left_Value0))){
Motor1.setSpeed(120);
Motor1.run(BACKWARD);
Motor2.setSpeed(120);
Motor2.run(BACKWARD);
Motor3.setSpeed(120);
Motor3.run(BACKWARD);
Motor4.setSpeed(120);
Motor4.run(BACKWARD);}
else if((head>1 && head<15)&&((Right_Value0)&&(Left_Value1))){
for(i=3000;i>0;i–){
Motor1.setSpeed(120);
Motor1.run(BACKWARD);
Motor2.setSpeed(0);
Motor2.run(BACKWARD);
Motor3.setSpeed(0);
Motor3.run(BACKWARD);
Motor4.setSpeed(120);
Motor4.run(BACKWARD);}}
else if((head>1 && head<15)&&((Right_Value1)&&(Left_Value0))){
for(i=3000;i>0;i–){
Motor1.setSpeed(0);
Motor1.run(BACKWARD);
Motor2.setSpeed(120);
Motor2.run(BACKWARD);
Motor3.setSpeed(120);
Motor3.run(BACKWARD);
Motor4.setSpeed(0);
Motor4.run(BACKWARD);}}
else if((head>1 && head<15)&&((Right_Value1)&&(Left_Value1))){
for(i=3000;i>0;i–){
Motor1.setSpeed(0);
Motor1.run(BACKWARD);
Motor2.setSpeed(120);
Motor2.run(BACKWARD);
Motor3.setSpeed(120);
Motor3.run(BACKWARD);
Motor4.setSpeed(0);
Motor4.run(BACKWARD);}}
else if((rear>1 && rear<15)&&((Right_Value0)&&(Left_Value0))){
Motor1.setSpeed(150);
Motor1.run(FORWARD);
Motor2.setSpeed(150);
Motor2.run(FORWARD);
Motor3.setSpeed(150);
Motor3.run(FORWARD);
Motor4.setSpeed(150);
Motor4.run(FORWARD);}
else if((rear>1 && rear<15)&&((Right_Value0)&&(Left_Value1))){
for(i=3000;i>0;i–){
Motor1.setSpeed(0);
Motor1.run(FORWARD);
Motor2.setSpeed(150);
Motor2.run(FORWARD);
Motor3.setSpeed(150);
Motor3.run(FORWARD);
Motor4.setSpeed(0);
Motor4.run(FORWARD);}}
else if((rear>1 && rear<15)&&((Right_Value1)&&(Left_Value0))){
for(i=3000;i>0;i–){
Motor1.setSpeed(150);
Motor1.run(FORWARD);
Motor2.setSpeed(0);
Motor2.run(FORWARD);
Motor3.setSpeed(0);
Motor3.run(FORWARD);
Motor4.setSpeed(150);
Motor4.run(FORWARD);}}
else if((rear>1 && rear<15)&&((Right_Value1)&&(Left_Value1))){
Motor1.setSpeed(150);
Motor1.run(FORWARD);
Motor2.setSpeed(150);
Motor2.run(FORWARD);
Motor3.setSpeed(150);
Motor3.run(FORWARD);
Motor4.setSpeed(150);
Motor4.run(FORWARD);}
else if((rear>1 && rear<15)&&(head>1 && head<15)){
Motor1.setSpeed(0);
Motor1.run(RELEASE);
Motor2.setSpeed(0);
Motor2.run(RELEASE);
Motor3.setSpeed(0);
Motor3.run(RELEASE);
Motor4.setSpeed(0);
Motor4.run(RELEASE);}
}
##有需要AFMotor和NewPing库的小伙伴也可以联系我