[应用教程]猪年小猪送吉

查看: 162|回复: 0

[应用教程] 猪年小猪送吉

[复制链接]
猪年年关快要到了。先祝大家新年愉快。这个项目是用一个Arduino uno来主控,通过小猪头上的超声模块来探测前方物体。如果探测到了。就通过DFPlayer mini MP3模块来说话。并且小猪为表示愉快,还会通过伺服电机来摇头晃脑。而且随着PING前方物体的向前的移动,MP3会回以不同的问候语句。

IMG20190112150701_已压缩.jpg
part list.jpg
schematic.jpg
首先这个连线图上的连线是要调整的。原因是我用的那个连线图工具,不能自由的改连线的接脚。大家看我改过的图。
[AppleScript] 纯文本查看 复制代码
/*---------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]



您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
wifi气象站

硬件清单

btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2019 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail