计时器蓝牙同步显示装置

最近居家办公,孩子则在上网课。我总是将房门关上,然后转动我的番茄钟,使用番茄钟工作法(25分钟专注工作💨5分钟休息💨25分钟工作💨5分钟休息。。。)这种方式是我在工作上保持高效率的秘诀。

但是因为现在在家里,孩子网课课间总想找我,而且老婆也时不时想找我说点事。一开始他们都是直接敲门找我,但发现我在番茄钟时间里,就赶紧退出去了,但其实我的思路已经被打断了。这件事对于我和家人来说感受都不太好,我对于被打断感到苦恼,而老婆和孩子也会因为不敢打扰我而不来找我了,这也不是我想要的效果。毕竟我是有休息的时间,这时大家一起聊几句,玩些小游戏还是不错的体验,这才是居家办公应有的节奏。

作为一个想成为创客的人,我知道这时是要解决问题的时候了。

视频

 代码

代码也放在了Github上供大家参考啦:

Github


目录

一、造型分析

二、基本思路

三、接线图

四、2个关键

五、代码


一、选型分析

Q1 我在房间里是有一个计时器的,但是25分钟是否已结束,房间外的家人看不见。怎么样才能让他们看见呢?

A1 我需要一种方式让向面的家人看见我的时间到哪了,还要多久才会结束番茄钟时间。或者其实我已经在休息,可以大胆过来找我。所以我要有一个小小的设备放在房间外,让家人看见。

Q2 这个小设备跟我的计时器之间要怎么连接呢?我怎么样才能在房间内启动房间外的设备呢?

A2 其实这很简单,只要用无线就行了。但选用Wifi还是蓝牙?蓝牙的操作会简单得多,不用去考虑IP地址之类的,连上后就可以直接发信息了。同时,蓝牙的理论距离是10米,从房间内到房间外距离很短,中间虽然隔了墙,但厚度还好,应该不会影响到最终的传输效果。

所以,我就选用了蓝牙啦。当然也是因为这段时间在做的公交乘车相关的系统有用到蓝牙,对于蓝牙模块已经有些了解,手上也还有几个开始时买错了的CC41蓝牙模块(只是功能比较简单)也正好可以用上。

二、基本思路

🚥设备A 放在房间里我来操作,基于Arduino,使用1个按钮、1个CC41蓝牙模块、1个绿色LED、1个红色LED。(我偷懒没有加电阻)

  • 设备A🚥知道设备B🧭的蓝牙模块地址,所以设备A🚥启动时直接连接设备B🧭
  • 使用者按按钮启动设备A🚥内的计时器功能,亮起红色,并通过蓝牙模块发送当前计时时间给设备B🧭
  • 当计时器正在计时的过程中,使用者按动按钮,计时器停止,亮起绿灯,并通过蓝牙模块发送0给设备B🧭

🧭设备B 放在房间外给家人看,基于于Arduino,使用1个CC41蓝牙模块、1个基于TM1637的数码管。

  • 设备B🧭收到设备A🚥发来的时间信息,并将它转化为数字,并显示到数码管上。
  • 如果收到的数字为0,则显示"Done"。

三、接线图

🚥设备A 

🧭设备B

四、2个关键

基于TM1637的数码管怎么用?

我手上的这个数码管其实是儿子小学时学校信息课上发的,疫情期间也没办法去买别的管,所以将就用着。

  1. 打开Arduino的IDE,打开菜单“工具”=>“管理库”。
  2. 搜索TM1637,你会发现有很多可用的库,但我选用的是TM1637这个库。(因为我已经安装了,所以“安装”按钮消失了)

  3. 根据代码显示,这个数码管上的CLK针接D2,DIO针接D3。
    // Module connection pins (Digital Pins)
    #define CLK 2
    #define DIO 3
  4. 大家可以通过找到示例去查看怎么用。

  5.  不想看示例的朋友,可以看一看以下我简化了的代码。
    #include <TM1637Display.h>
    
    
    /***************************数码管开始**********************************/
    // Module connection pins (Digital Pins)
    #define CLK 2
    #define DIO 3
    
    TM1637Display display(CLK, DIO);
    /***************************数码管结束**********************************/
    
    
    void setup()
    {
      //将亮度设置为1
      display.setBrightness(0x01);
      
      //显示“00 00”
      display.showNumberDecEx(0, 0, true); 
    
      //显示“24:59”,64表示要显示:,true表示如果给的数字不足4位,就在前面补上0
      display.showNumberDecEx(2459, 64, true); 
    }
    
    void loop()
    {
    
    }

Arduino计时器怎么做?

  1. 打开Arduino的IDE,打开菜单“工具”=>“管理库”。
  2. 找到Arduino-timer,并安装

  3. 简化代码如下
    #include <arduino-timer.h>
    
    /***************************Timer开始**********************************/
    auto timer = timer_create_default(); //create a timer with default settings
    
    int theTime = 0;
    /***************************Timer结束**********************************/
    
    
    void setup() {
      //设置1000毫秒(1秒)跳一次,countDown对应的是后面自定义的方法,就是每1秒运行一次这个自定义的方法
      timer.every(1000, countDown); 
    }
    
    void loop() {
      timer.tick();//让Timer保持变化
    }
    
    /***************************Timer开始**********************************/
    void countDown(void)
    {
      if (theTime > 0)
      {
        theTime--;
      }  
    }
    /***************************Timer结束**********************************/
    

五、代码

🚥设备A 代码公开

#include <arduino-timer.h>
#include <SoftwareSerial.h>


/***************************蓝牙开始**********************************/
SoftwareSerial mySerial(10, 11); // RX, TX
char val; //存储接收的变量
String fromBT; 
String sendStr;
/***************************蓝牙结束**********************************/


/***************************Timer开始**********************************/
auto timer = timer_create_default(); //create a timer with default settings

int theTime = 0;
/***************************Timer结束**********************************/


void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  /***************************蓝牙开始**********************************/
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);

  connectDevice();
  /***************************蓝牙结束**********************************/
  
  pinMode(2, INPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);

  timer.every(1000, countDown); //Timer
}

void loop() {
  if (digitalRead(2) == HIGH)
  {
    if (theTime > 0)
    {
      theTime = 0;

      digitalWrite(7, LOW);
      digitalWrite(8, HIGH);
    }
    else
    {
      theTime = 25 * 60;

      digitalWrite(7, HIGH);
      digitalWrite(8, LOW);          
    }
    sendTime();

    delay(1000);
  }

  if (theTime == 0)
  {
    digitalWrite(7, LOW);
    digitalWrite(8, HIGH);
  }


  readBT();

  //将调试工具里发的内容转发给蓝牙模块
  if (Serial.available()>0) {
    val = Serial.read();
    mySerial.write(val);
  }

  timer.tick();//tick the timer
}

/***************************蓝牙开始**********************************/
void readBT()
{
   if (mySerial.available()>0) {
    fromBT = mySerial.readString();
  
    Serial.println(fromBT);
  }
}

void connectDevice()
{
  sendMessage("at+cona0x882583F3A998\r\n");
  Serial.println("Try to connect to the device.");
  delay(1000);
}

void sendTime()
{
  sendStr = String((int)(theTime/60)*100 + theTime%60);
  Serial.println(sendStr);
  sendMessage(sendStr);
}

//通用的发送信息方法
void sendMessage(String msg)
{
  mySerial.print(msg);
  delay(100);
}
/***************************结束开始**********************************/


void countDown(void)
{
  if (theTime > 0)
  {
    theTime--;
    if (theTime%5 == 0)
    {
      sendTime();
    }
  }
  
  Serial.println(theTime);
}

🧭设备B 代码公开

#include <Arduino.h>
#include <TM1637Display.h>
#include <SoftwareSerial.h>


/***************************数码管开始**********************************/
// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3

// The amount of time (in milliseconds) between tests
#define TEST_DELAY   2000

const uint8_t SEG_DONE[] = {
  SEG_B | SEG_C | SEG_D | SEG_E | SEG_G,           // d
  SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F,   // O
  SEG_C | SEG_E | SEG_G,                           // n
  SEG_A | SEG_D | SEG_E | SEG_F | SEG_G            // E
  };

TM1637Display display(CLK, DIO);
/***************************数码管结束**********************************/


/***************************蓝牙开始**********************************/
SoftwareSerial mySerial(10, 11); // RX, TX
char val; //存储接收的变量
String fromBT; 
/***************************蓝牙结束**********************************/

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Hello Thomas!");
  
  /***************************蓝牙开始**********************************/
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  /***************************蓝牙结束**********************************/


  display.setBrightness(0x01);
  display.showNumberDecEx(0, 0, true);
}

void loop()
{
//  display.showNumberDecEx(301, 64, true);
//  delay(TEST_DELAY);


  // Done!
//  display.setSegments(SEG_DONE);
//  delay(TEST_DELAY);

  readBT();

  //将调试工具里发的内容转发给蓝牙模块
  if (Serial.available()>0) {
    val = Serial.read();
    mySerial.write(val);
  }

}

/***************************蓝牙开始**********************************/
void readBT()
{
   if (mySerial.available()>0) {
    fromBT = mySerial.readString();
  
    Serial.println(fromBT);

    showInt(fromBT);
  }
}

void showInt(String &sTime)
{
  int theTime = sTime.toInt();
  Serial.println(String(theTime));
  if (theTime == 0)
  {
    // Done!
    display.setSegments(SEG_DONE);
    delay(TEST_DELAY);
  }
  else
  {
    display.showNumberDecEx(theTime, 64, true);
  }
}
/***************************蓝牙结束**********************************/

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长风Eric(fengcan)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值