代码编号:ultrasonic_test
[code]
//HC-SR04 pins define
#define TRIG 11
#define ECHO 12
unsigned long pulse_length ; //记录超声波脉冲宽度的变量,单位是微秒
float distance; //存储超声波测距结果的变量,单位是厘米
float warning_dis = 5; //预设的警告阈值,当测得距离小于该值时向电脑发出警报,单位是厘米
void setup() {
// put your setup code here, to run once:
pinMode(TRIG, OUTPUT); //定义超声波传感器的触发信号管脚为输出
pinMode(ECHO, INPUT); //定义超声波传感器的返回信号管脚为输入
Serial.begin(9600); //定义串口波特率为9600,用于在电脑屏幕上显示实时数据
}
void loop() {
// put your main code here, to run repeatedly:
//向TRIG发出宽度为1毫秒的高电平脉冲
digitalWrite(TRIG, LOW);
delay(1);
digitalWrite(TRIG, HIGH);
delay(1);
digitalWrite(TRIG, LOW);
//delay(2);
pulse_length = pulseIn(ECHO, HIGH);//从ECHO管脚读取高电平脉冲的长度,如果没有,返回值为0
distance = 0.017*pulse_length; //根据得到的脉冲长度计算出障碍物的距离
if(distance > 0 ){
Serial.print("障碍物距离:"); //输出敌人距离
Serial.println(distance);
}
delay(1000);
}
[/code]
=========================================================================
代码编号:car_test
[code]
//小车两侧电机驱动管脚定义
//IN1,IN2右侧
//IN3,IN4左侧
#define IN1 10
#define IN2 9
#define IN3 6
#define IN4 5
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
//前进
forward();
delay(1000);
//停止
turnoff();
delay(500);
//后退
backward();
delay(1000);
//停止turnoff();
delay(500);
//左转
turnleft();
delay(1000);
//停止
turnoff();
delay(500);
//右转
turnright();
delay(1000);
//停止
turnoff();
delay(500);
} // end loop
//------------------------------------------------------------
void forward(){ //前进
analogWrite(IN1, 150);
analogWrite(IN2, 0);
analogWrite(IN3, 150);
analogWrite(IN4, 0);
}
void backward(){ //后退
analogWrite(IN1, 0);
analogWrite(IN2, 255);
analogWrite(IN3, 0);
analogWrite(IN4, 255);
}
void turnleft(){ //左转
analogWrite(IN1, 255);
analogWrite(IN2, 0);
analogWrite(IN3, 0);
analogWrite(IN4, 255);
}
void turnright(){ //右转
analogWrite(IN1, 0);
analogWrite(IN2, 255);
analogWrite(IN3, 255);
analogWrite(IN4, 0);
}
void turnoff(){ //停止
analogWrite(IN1, 0);
analogWrite(IN2, 0);
analogWrite(IN3, 0);
analogWrite(IN4, 0);
}
[/code]
========================================================================
代码编号:obstacle_detect_ultrasonic_car
[code]
//HC-SR04 pins define
#define TRIG 11
#define ECHO 12
#define IN1 10
#define IN2 9
#define IN3 6
#define IN4 5
unsigned long pulse_length ; //记录超声波脉冲宽度的变量,单位是微秒
float distance; //存储超声波测距结果的变量,单位是厘米
float warning_dis = 10; //预设的警告阈值,当测得距离小于该值时向电脑发出警报,单位是厘米
int runtime; //用来存储小车变向动作的时间长短,单位微秒
int action; //决定小车变向动作,1=左转,2=右转,3=后退
void setup() {
// put your setup code here, to run once:
pinMode(TRIG, OUTPUT); //定义超声波传感器的触发信号管脚为输出
pinMode(ECHO, INPUT); //定义超声波传感器的返回信号管脚为输入
Serial.begin(9600); //定义串口波特率为9600,用于在电脑屏幕上显示实时数据
}
void loop() {
// put your main code here, to run repeatedly:
//向TRIG发出宽度为1毫秒的高电平脉冲
digitalWrite(TRIG, LOW);
delay(1);
digitalWrite(TRIG, HIGH);
delay(1);
digitalWrite(TRIG, LOW);
//delay(2);
pulse_length = pulseIn(ECHO, HIGH);//从ECHO管脚读取高电平脉冲的长度,如果没有,返回值为0
distance = 0.017*pulse_length; //根据得到的脉冲长度计算出障碍物的距离
bool obstacle = ( (distance > 0)&&(distance < warning_dis) );
if( obstacle == HIGH ){ //前方有障碍物
digitalWrite(13, HIGH);
turnoff(); //停下
//delay(5000);
action = random(1,4); //产生一个1到3范围内的随机数
switch (action){
case 1: turnleft();
break;
case 2: turnright();
break;
case 3: backward();
break;
default: backward();
break;
} // end case
runtime = random(1,6) * 500;
delay(runtime);
}
else {
forward(); //end if
digitalWrite(13, LOW);
}
}// end loop
//------------------------------------------------------------
void forward(){ //前进
analogWrite(IN1, 150);
analogWrite(IN2, 0);
analogWrite(IN3, 150);
analogWrite(IN4, 0);
}
void backward(){ //后退
analogWrite(IN1, 0);
analogWrite(IN2, 150);
analogWrite(IN3, 0);
analogWrite(IN4, 150);
}
void turnleft(){ //左转
analogWrite(IN1, 150);
analogWrite(IN2, 0);
analogWrite(IN3, 0);
analogWrite(IN4, 150);
}
void turnright(){ //右转
analogWrite(IN1, 0);
analogWrite(IN2, 150);
analogWrite(IN3, 150);
analogWrite(IN4, 0);
}
void turnoff(){ //停止
analogWrite(IN1, 0);
analogWrite(IN2, 0);
analogWrite(IN3, 0);
analogWrite(IN4, 0);
}
[/code]
===============================================================
代码编号:washing_machine
#define LFT 9 //电机左边电极
#define RGHT 10 //电机右边电极
int cnt; //控制电机加减速计数器
void setup() {
// put your setup code here, to run once:
cnt = 0;
}
void loop() {
// put your main code here, to run repeatedly:
//控制电机正转过程
for(cnt = 0; cnt <= 250; cnt=cnt +10){ //加速过程
analogWrite(LFT, cnt);
analogWrite(RGHT, 0);
delay(200);
}
delay(4000); //匀速过程
for(cnt = 250; cnt >= 0; cnt=cnt -10){ //减速过程
analogWrite(LFT, cnt);
analogWrite(RGHT, 0);
delay(200);
}
delay(1000); //停止过程
//-----------------------------------
//控制电机反转过程
for(cnt = 0; cnt <= 250; cnt=cnt +10){ //加速过程
analogWrite(LFT, 0);
analogWrite(RGHT, cnt);
delay(200);
}
delay(4000); //匀速过程
for(cnt = 250; cnt >= 0; cnt=cnt -10){ //减速过程
analogWrite(LFT, 0);
analogWrite(RGHT, cnt);
delay(200);
}
delay(1000); //停止过程
}
=============================================================