宿舍蓝牙开关灯

前言

每次从实验室回宿舍,都快11点多了,洗完澡,宿舍就熄灯了。所以灯经常忘关。所以早晨六点总是被明亮的灯光刺醒,但宿舍又是最简陋的上下铺,居住在上铺的我苦此已久。终于受不了了,做了一个蓝牙开关灯。

一、耗材清单

1.电源(3.3-6V)都行,找你能找得到的电池盒/小电源。

2.Arduino板(最好选mini的小板,方便),我用的是YWRduino Lite
注意:如果跟我用同样型号的mini arduino的话,下载器需要选用带DTR的USB转TTL。

3.Arduino IDE(https://www.arduino.cc/en/donate/)官网下载即可,如果跟我用同样型号的mini arduino的话,记得板子型号选uno板即可。

4.蓝牙模块,我用的是HC-06。
TX: 接Arduino"RX"引脚
RX: 接Arduino"TX"引脚
GND: 接Arduino"GND"引脚
VCC: 接Arduino"5V"或"3.3V"引脚

5.伺服舵机,丰俭由人。最普通的SG90即可,塑料舵机轻,好黏在墙上。
信号线:接Arduino"P9"引脚
GND: 接Arduino"GND"引脚
VCC: 接Arduino"5V"或"3.3V"引脚

二、步骤

1.代码

就用Arduino自带的例程改一下就行

#include <Servo.h>

Servo myservo;
int pos = 0; 
char cmd ='s';//初始化

void setup() {
  myservo.attach(9);  //定义舵机接口为pin9
  Serial.begin(9600); //初始化蓝牙,波特率为9600
}

void loop() {
if(Serial.available()){
  cmd = Serial.read();//读取蓝牙模块发送到串口的数据
  if(cmd=='a'){       //手机蓝牙串口发送数据a,表示关灯
    for (pos = 20; pos <= 40; pos += 1) { 
    myservo.write(pos);              
    delay(30);}} 
  else if(cmd=='b'){ //手机蓝牙串口发送数据b,表示开灯
    for (pos = 40; pos >= 0; pos-= 1) { 
    myservo.write(pos);              
    delay(30);}}
  else if(cmd=='r'){ //手机蓝牙串口发送数据r,表示恢复初始状态
    myservo.write(20);}      
}
}

这里要注意的是,如果你用的sg90这类的180度舵机,他只能从0度摆动到180度,所以,可以先用函数myservo.write(0); 寻找一下舵机的0度在哪里。

2.手机的蓝牙控制

很倒霉我用的手机是iPhone,找了很久,终于找到一个比较合适的蓝牙串口助手名字叫:FeasyBlue。
以下是操作的例子
这里找到你的蓝牙模块,一般pin码为0000或1234
在这里插入图片描述


其他

因为宿舍是两盏灯,所以3D打印了一个两开的小构件。需要文件的可以留言邮箱,博主私发给你。
在这里插入图片描述

  • 22
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
好的,以下是用uniapp写一个关游戏的代码: <template> <view class="container"> <view class="title">关游戏</view> <view class="board"> <view class="row" v-for="(row, rowIndex) in board" :key="rowIndex"> <view class="cell" v-for="(cell, cellIndex) in row" :key="cellIndex" :class="{ on: cell }" @click="toggle(rowIndex, cellIndex)"></view> </view> </view> <view class="btn" @click="reset">重新开始</view> </view> </template> <script> export default { data() { return { board: [ [true, true, true, true, true], [true, true, true, true, true], [true, true, true, true, true], [true, true, true, true, true], [true, true, true, true, true] ] } }, methods: { toggle(row, col) { this.board[row][col] = !this.board[row][col] if (row > 0) this.board[row - 1][col] = !this.board[row - 1][col] if (row < 4) this.board[row + 1][col] = !this.board[row + 1][col] if (col > 0) this.board[row][col - 1] = !this.board[row][col - 1] if (col < 4) this.board[row][col + 1] = !this.board[row][col + 1] if (this.checkWin()) { setTimeout(() => { uni.showToast({ title: '你赢了!', icon: 'success' }) }, 500) } }, checkWin() { return this.board.every(row => row.every(cell => !cell)) }, reset() { this.board = [ [true, true, true, true, true], [true, true, true, true, true], [true, true, true, true, true], [true, true, true, true, true], [true, true, true, true, true] ] } } } </script> <style> .container { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; } .title { font-size: 36px; font-weight: bold; margin-bottom: 20px; } .board { display: flex; flex-direction: column; align-items: center; justify-content: center; width: 300px; height: 300px; background-color: #eee; border-radius: 10px; padding: 10px; } .row { display: flex; flex-direction: row; } .cell { width: 50px; height: 50px; background-color: #fff; border: 1px solid #ccc; margin: 5px; border-radius: 5px; } .cell.on { background-color: #333; } .btn { margin-top: 20px; padding: 10px 20px; background-color: #333; color: #fff; border-radius: 5px; cursor: pointer; } </style>
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值