本帖最后由 SimpleChen 于 2024-10-7 17:30 编辑
所需器材:- DFRduino UNO R3(含USB线|传感器扩展板)*1
- URM37 V5.0超声波测距传感器(创客商城有售,文末有链接)*1
- 单芯优质 杜邦线(10根) 母母头(创客商城有售,文末有链接)*1
- DFRobot DF9GMS 180° 微型舵机(创客商城有售,文末有链接)*2
- 68段语音模块(某宝有售)
接线图:(为了使图片更加简洁,本图舍去了VCC和GND线的连接,并且本图中所有连线相交处均不连接,接线图在文末)
上传模式ArduinoC代码:(Mind+程序在文末)
- /*!
- * MindPlus
- * uno
- *
- */
- #include <Voice68.h>
- #include <DFRobot_URM10.h>
- #include <DFRobot_Servo.h>
-
- // 动态变量
- volatile float mind_n_nearOrLeft, mind_n_distance, mind_n_preDist, mind_n_distMin,
- mind_n_distMax;
- // 创建对象
- VOICE68 voice(11,12,13);
- DFRobot_URM10 urm10;
- Servo servo_A3;
- Servo servo_A1;
-
-
- // 主程序开始
- void setup() {
- servo_A3.attach(A3);
- servo_A1.attach(A1);
- mind_n_nearOrLeft = 0;
- mind_n_distance = 0;
- mind_n_preDist = 0;
- mind_n_distMin = 10;
- mind_n_distMax = 30;
- }
- void loop() {
- mind_n_distance = (urm10.getDistanceCM(3, 2));
- if ((mind_n_preDist<mind_n_distance)) {
- mind_n_nearOrLeft = 0;
- }
- else {
- mind_n_nearOrLeft = 1;
- }
- if (((mind_n_nearOrLeft==0) && (mind_n_distance==mind_n_distMax))) {
- servo_A3.angle(abs(10));
- delay(5);
- voice.play(50);
- voice.play(25);
- voice.play(59);
- }
- if (((mind_n_nearOrLeft==1) && (mind_n_distance==mind_n_distMin))) {
- servo_A1.angle(abs(10));
- delay(5);
- voice.play(26);
- }
- delay(100);
- mind_n_preDist = mind_n_distance;
- servo_A3.angle(abs(90));
- delay(5);
- servo_A1.angle(abs(90));
- delay(5);
- }
复制代码
Python模式代码:(Mind+程序在文末)- # -*- coding: UTF-8 -*-
-
- # MindPlus
- # Python
- import time
- from pinpong.board import Pin
- from pinpong.board import Board
- from pinpong.board import Servo
- from pinpong.board import SR04_URM10
-
- # 自定义函数
- def readSentence_num(num):
- Rst.write_digital(0)
- time.sleep(0.000002)
- Rst.write_digital(1)
- time.sleep(0.0001)
- Rst.write_digital(0)
- time.sleep(0.0001)
- for i in range(1, num, 1):
- Data.write_digital(1)
- time.sleep(0.0001)
- Data.write_digital(0)
- time.sleep(0.0001)
- while not ((Busy.read_digital() == 0)):
- pass
-
-
- Board("uno").begin()
- global nearOrLeft
- nearOrLeft = 0
- global TrigPin
- TrigPin = 3
- global EchoPin
- EchoPin = 2
- global servoLeftPin
- servoLeftPin = A1
- global servoRightPin
- servoRightPin = A3
- global distance
- distance = 0
- global preDist
- preDist = 0
- global distMin
- distMin = 10
- global distMax
- distMax = 30
- Rst = 11
- Data = 12
- Busy = 13
- TrigPin = Pin(TrigPin, Pin.OUT)
- EchoPin = Pin(EchoPin, Pin.IN)
- servoLeftPin = Pin(servoLeftPin, Pin.PWM)
- servoRightPin = Pin(servoRightPin, Pin.PWM)
- Rst = Pin(Rst, Pin.PWM)
- Data = Pin(Data, Pin.PWM)
- Busy = Pin(Busy, Pin.PWM)
- servoLeftPin = Servo(servoLeftPin)
- servoRightPin = Servo(servoRightPin)
- (str(servoLeftPin) + str(servoRightPin)) = SR04_URM10(TrigPin,EchoPin)
-
- while True:
- distance = (str(servoLeftPin) + str(servoRightPin)).distance_cm()
- if (preDist < distance):
- nearOrLeft = 0
- else:
- nearOrLeft = 1
- if ((nearOrLeft == 0) and (distance == distMax)):
- servoRightPin.write_angle(10)
- time.sleep(0.005)
- readSentence_num(50)
- readSentence_num(25)
- readSentence_num(59)
- if ((nearOrLeft == 1) and (distance == distMin)):
- servoLeftPin.write_angle(10)
- time.sleep(0.005)
- readSentence_num(26)
- time.sleep(0.1)
- servoRightPin.write_angle(90)
- time.sleep(0.01)
- servoLeftPin.write_angle(90)
- time.sleep(0.01)
复制代码
运行效果:
上传程序后,用书本在超声波前做前后移动,当距离分别为10cm和30cm时,舵机动作同时扬声器播报内容。
|