电子工程师技术服务社区
公告
登录
|
注册
首页
技术问答
厂商活动
正点原子
板卡试用
资源库
下载
文章
社区首页
文章
用Arduino做一个有点傻的声控灯
分 享
扫描二维码分享
用Arduino做一个有点傻的声控灯
arduino
声控灯
DIY
☻
关注
发布时间: 2020-06-04
丨
阅读: 4853
今天又折腾了一个有点傻的声控灯,它能满足不想下床关灯的需求,我不想承认它很鸡肋,但他确实鸡肋。我更多的是不想让Arduino吃灰太久。。。。。。。 ------------ 废话少说,开始。 先看看他是怎样控制灯,请勿吐槽: ![](https://cf04.ickimg.com/bbsimages/202006/f8d08f863dab469205a2348b9b6a66cd.png) # 一.需要准备 1. Arduino Uno 1. 面包板 1. 声音探测模块 1. 伺服电机 1. 跳线若干 ![](https://cf04.ickimg.com/bbsimages/202006/28afb61490831638d5ce3deae60e8a52.png) #二. 将声音探测模块和电机连接到Arduino ## 声音探测模块 - 将声音探测模块上标记为“envelope”的引脚连接到Arduino - 将声音探测模块上标记为“GND”的引脚连接到Arduino上的GND引脚。 - 将声音探测模块上标记为“VCC”的引脚连接到Arduino上的3.3V引脚。 ## 电机 - 将电机的棕色线连接到Arduino的GND。 - 将电机的橙色线(中间)连接到Arduino的5V。 - 将电机的浅褐色线连接到Arduino的9脚。 ![](https://cf04.ickimg.com/bbsimages/202006/f48766e1d15ee2af58b0b3880c8f719f.png) #### 然后把电机像一图那样固定好。。。 # 三.**代码** ```cpp //include the servo library #include //pin variable const int soundPin = 0; const int servoPin = 9; int clap_on = 0; // declare servo Servo servo1; //variables for storing raw sound and scaled value int sound; void setup() { Serial.begin(9600); // attach servo to pin 9 servo1.attach(servoPin); // always initialize the servo as light off. servo1.write (180); servo1.write (150); clap_on = 0; } void loop() { //read and store the audio from Envelope pin sound = analogRead(soundPin); //map sound which in a quiet room a clap is 300 //from 0 to 3 to be used with switch case //print values over the serial port for debugging Serial.println(sound); if (sound > 20){ if (clap_on == 0){ // check if the servo still hasnt rotated servo1.write(150); // turn light on clap_on = 1;} // set a toggle on clap so then next time it //happens it will turn the light off else if (clap_on == 1){ servo1.write(180); // turn light off clap_on = 0;} // set toggle again. } delay (500); } ``` **声音模块实际上并没有分析声音,而是仅仅作为声音信号。也就是说,可以拍手或发出任何其他声音来打开或关闭它。** # 玩得开心~!
原创作品,未经权利人授权禁止转载。详情见
转载须知
。
举报文章
点赞
(
1
)
☻
关注
评论
(0)
登录后可评论,请
登录
或
注册
相关文章推荐
MK-米客方德推出工业级存储卡
Beetle ESP32 C3 蓝牙数据收发
Beetle ESP32 C3 wifi联网获取实时天气信息
开箱测评Beetle ESP32-C3 (RISC-V芯片)模块
正点原子数控电源DP100测评
DP100试用评测-----开箱+初体验
Beetle ESP32 C3环境搭建
【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812硬屏之二
X
你的打赏是对原创作者最大的认可
请选择打赏IC币的数量,一经提交无法退回 !
100IC币
500IC币
1000IC币
自定义
IC币
确定
X
提交成功 ! 谢谢您的支持
返回
我要举报该内容理由
×
广告及垃圾信息
抄袭或未经授权
其它举报理由
请输入您举报的理由(50字以内)
取消
提交