自己写的一个人蓝牙Arduino小车,附件有蓝牙串口助手
Arduino源程序如下:
- //因为数字端口2和13未被占用,所以我们就要2和13端口来接超声波模块。
- #include <AFMotor.h>
- //int inputPin=13; // 定义超声波Trig
- //int outputPin=2; // 定义超声波Echo
- AF_DCMotor motor1(1, MOTOR12_64KHZ);// 创建电机#1号,速度是64KHz pwm
- AF_DCMotor motor2(2, MOTOR12_64KHZ);// 创建电机#2号,速度是64KHz pwm
- AF_DCMotor motor3(3, MOTOR12_64KHZ);// 创建电机#2号,速度是64KHz pwm
- AF_DCMotor motor4(4, MOTOR12_64KHZ);// 创建电机#2号,速度是64KHz pwm
- void setup()
- {
- Serial.begin(9600);
- //pinMode(inputPin, INPUT);
- // pinMode(outputPin, OUTPUT);
- motor1.setSpeed(150);// 设置电机速度,从200/255之间任意(我这俩电机速度不一样,所以我就这样设置)
- motor2.setSpeed(150);// 设置电机速度,从200/255之间任意(大家可以按照自己的电机转速来调节一样的速度就可以了)
- motor3.setSpeed(150);
- motor4.setSpeed(150);
- Serial.println("Motor test!");
- }
- void loop()
- {
- //digitalWrite(outputPin, LOW); // 使发出发出超声波信号接口低电平2μs
- //delayMicroseconds(2);
- // digitalWrite(outputPin, HIGH); // 使发出发出超声波信号接口高电平10μs,这里是至少10μs
- // delayMicroseconds(10);
- // digitalWrite(outputPin, LOW); // 保持发出超声波信号接口低电平
- // int distance = pulseIn(inputPin, HIGH); // 读出脉冲时间
- //distance= distance/58; // 将脉冲时间转化为距离(单位:厘米)
- //Serial.println(distance); //输出距离值
- delay(3000);
- //if (distance < 120 ) //大于50厘米就前进
- //小车前进
- {
- //Serial.print("tick");
- motor1.run(FORWARD);// 电机前进
- motor2.run(FORWARD);// 电机前进
- motor3.run(FORWARD);// 电机前进
- motor4.run(FORWARD);// 电机前进
- }
- delay(3000);
- //if (distance > 50) //小雨50厘米就后退
- //小车后退
- {
- //Serial.print("tock");
- motor1.run(BACKWARD);// 电机后退
- motor2.run(BACKWARD);// 电机后退
- motor3.run(BACKWARD);// 电机后退
- motor4.run(BACKWARD);// 电机后退
- }
- //if (distance == 50) //等于50厘米就停止
- //小车停止
- delay(3000);
- {
- //Serial.print("tack");
- motor1.run(RELEASE);// 电机停止
- motor2.run(RELEASE);// 电机停止
- motor3.run(RELEASE);// 电机停止
- motor4.run(RELEASE);// 电机停止
- }
- //小车左转
- delay(3000);
- {
- motor1.run(FORWARD);// 电机前进
- motor2.run(FORWARD);// 电机前进
- motor3.run(BACKWARD);// 电机后退
- motor4.run(BACKWARD);// 电机后退
- }
- //小车右转
- delay(3000);
- {
- motor3.run(FORWARD);// 电机前进
- motor4.run(FORWARD);// 电机前进
- motor1.run(BACKWARD);// 电机后退
- motor2.run(BACKWARD);// 电机后退
- }
- }
复制代码
所有资料51hei提供下载:
lanyachuankou_41.zip
(1.69 MB, 下载次数: 18)
1.zip
(1.38 KB, 下载次数: 16)
|