3262| 4
|
[项目] 室内驱狗神器的诞生 |
不知你们有没有这样的经历,刚买的沙发,自己都不舍得坐呢,一个不看住,狗狗把沙发造的不要不要的。 这次这个项目就是告诉大家怎么造一个驱宠神器,神器放在沙发的上面,当PIR传感识别靠近的生物时,mp3马上语音警告,同时伺服电机带动打开的激光随机扫2秒,一方面吸引狗狗的注意,一方面驱赶狗狗。 硬件清单 1.Arduino uno 2.DF mp3 3.激光头5v 4.伺服电机9g 5.PIR传感 连线图 PIR脚位图 MP3文件拷贝目录 下面是代码 [mw_shl_code=applescript,false]/* made by gada888 in Luoyang China.Date:2020-1-20 */ #include <SoftwareSerial.h> SoftwareSerial Serial1(10, 11); #include "Servo.h" Servo ServoOne; // Generates an instance of a servo object int ServoPosition = 0; // Variable to be used to assign position of int laserPin = 12; // choose the pin for the LED int inputPin = 2; // choose the input pin (for PIR sensor) int pirState = LOW; // we start, assuming no motion detected int val = 0; // variable for reading the pin status unsigned char order[4] = {0xAA,0x06,0x00,0xB0}; void setup() { ServoOne.attach(9); // Assigns pin 9 as a servo pinMode(laserPin, OUTPUT); // declare LED as output pinMode(inputPin, INPUT); // declare sensor as input Serial.begin(115200); Serial1.begin(9600); volume(0x1E);//闊抽噺璁剧疆0x00-0x1E } void loop(){ val = digitalRead(inputPin); // read input value if (val == HIGH) { // check if the input is HIGH digitalWrite(laserPin, HIGH); // turn LED ON ServoPosition = random(0,90); // Generates a random number and stores it in ServoPosition ServoOne.write(ServoPosition); // Commands servo to spin to position delay(2000); // Delays for 1000 ms if (pirState == LOW) { // we have just turned on Serial.println("Motion detected!"); // We only want to print on the output change, not state pirState = HIGH; play(0x01);//鎸囧畾鎾斁:0x01-鏂囦欢0001 //delay(1000); } } else { digitalWrite(laserPin, LOW); // turn LED OFF //playMP3; //delay(1000); if (pirState == HIGH){ // we have just turned off Serial.println("Motion ended!"); // We only want to print on the output change, not state pirState = LOW; } } } // =========FUNCTION============ void play(unsigned char Track) { unsigned char play[6] = {0xAA,0x07,0x02,0x00,Track,Track+0xB3};//0xB3=0xAA+0x07+0x02+0x00,鍗虫渶鍚庝竴浣嶄负鏍¢獙鍜? Serial1.write(play,6); } void volume( unsigned char vol) { unsigned char volume[5] = {0xAA,0x13,0x01,vol,vol+0xBE};//0xBE=0xAA+0x13+0x01,鍗虫渶鍚庝竴浣嶄负鏍¢獙鍜? Serial1.write(volume,5); }[/mw_shl_code] 运行代码成功图 演示图 最后说一句,本人不养宠物。 |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed