5153| 0
|
[进阶] 猪年小猪送吉 |
猪年年关快要到了。先祝大家新年愉快。这个项目是用一个Arduino uno来主控,通过小猪头上的超声模块来探测前方物体。如果探测到了。就通过DFPlayer mini MP3模块来说话。并且小猪为表示愉快,还会通过伺服电机来摇头晃脑。而且随着PING前方物体的向前的移动,MP3会回以不同的问候语句。 首先这个连线图上的连线是要调整的。原因是我用的那个连线图工具,不能自由的改连线的接脚。大家看我改过的图。 [mw_shl_code=applescript,true]/*---------Made by Gada888 2019-01-10------*/ [mw_shl_code=applescript,true]//-----------IMPORT LIB----------- #include <Servo.h> #include <SoftwareSerial.h> #include <DFPlayer_Mini_Mp3.h> //--------INICIALIZE---------- Servo myServo; int pos = 0; int trig = 2; int echo = 3; //--------------SETUP------------- void setup () { Serial.begin (9600); myServo.attach(9); pinMode(trig, OUTPUT); pinMode(echo, INPUT); mp3_set_serial (Serial); mp3_set_volume (25); } //-------------LOOPING------------ void loop () { int duration , distance; digitalWrite(trig, HIGH); delayMicroseconds(10); digitalWrite(trig, LOW); duration = pulseIn(echo, HIGH); distance = (duration / 2) / 21.9; Serial.print(distance); Serial.print("cm"); delay(50); if (distance < 3) { mp3_play (1); delay (2000); { } } else if ((3 < distance) && (distance < 6)) { mp3_play (2); delay (2000); } else if ((6< distance) && (distance < 9)) { mp3_play (3); delay (2000); myServo.write(30); delay (500); myServo.write(-30); delay (500); myServo.write(30); delay (500); myServo.write(-30); delay (500); myServo.write(30); delay (500); myServo.write(-30); delay (500); } else if ((9 < distance) && (distance < 12)) { mp3_play (4); delay (1000); myServo.write(30); delay (500); myServo.write(-30); delay (500); myServo.write(30); delay (500); myServo.write(-30); delay (500); myServo.write(30); delay (500); myServo.write(-30); delay (500); } }[/mw_shl_code] |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed