给墙表添加夜灯,让它变成夜晚墙上最靓的物件
本帖最后由 gada888 于 2020-5-30 15:19 编辑家里有个墙表,用了几年了没坏,也不想换。可是自己经常晚上起夜时候是半夜,从厕所回来就想看下时间了解下自己的起夜规律。但是老墙表由于没夜灯,所以都看不到时间。又迷迷糊糊的去睡了。但这个问题一直都想解决呀。于是想了好一阵子怎么去弄。用微波测距是早已经想好的主零件,然后就是灯的问题,这个选了好久,最初的想法是用几个LED串联一起,但是又觉得不好看,而且要焊接。用WS2812B led吧,可以是可以,就是怎么弄成圆形的呢。有点麻烦,而且有个要求是灯不必那么亮,刚刚看清表盘就好,而且还要灯平滑关闭。最后是想到了用点阵,也就是MAX7219模块,这个模块既经济又连线方便。
https://v.youku.com/v_show/id_XNDY5MjM4MzM2NA==.html?spm=a2hzp.8244740.0.0
经过几天的密集测试,未发现Bug,准备把主控换成arduino nano,并配上外壳,让它正式服役。
{:6_211:}
--------------物料清单如下-------------
元件除了点阵均来自DFRobot,具体参数参考商城的产品WIKI,点阵有配专门的介绍,购自某宝
微波传感
arduino UNO Romeo
{:5_122:}
--------------连线图如下-------------
-----------------代码如下-------------
/*
这个代码是gada888所做,2020-05-25
转载请注明出处
*/
#include <MsTimer2.h> //Timer interrupt function
int pbIn = 0; // Define the interrupt PIN is 0, that is, digital pins 2
//int ledOut = 13;
int count=0;
volatile int state = LOW; //Define ledOut, default is off
#include "LedControlMS.h"
//pin 5 is connected to the DataIn
// pin 4 is connected to the CLK
//pin 3 is connected to LOAD
#define NUM_MTX 1 //number of matrices attached is one
LedControl lc=LedControl(5,4,3, NUM_MTX);//
/////////////////SETUP/////////////////
void setup()
{
Serial.begin(9600);
//pinMode(ledOut, OUTPUT);
attachInterrupt(pbIn, stateChange, FALLING); // Sets the interrupt function, falling edge triggered interrupts.
MsTimer2::set(1000,process); // Set the timer interrupt time 1000ms
MsTimer2::start();//Timer interrupt start
lc.shutdown(0,true);
lc.shutdown(1,true);
lc.setIntensity(0,8);
lc.clearDisplay(0);
lc.setIntensity(1,8);
lc.clearDisplay(1);
}
void loop()
{
Serial.println(count); // Printing times of 1000ms suspension
delay(1);
if(state == HIGH) //When moving objects are detected later, 2s shut down automatically after the ledout light is convenient.
{
delay(2000);
state = LOW;
Alloff();
}
}
void stateChange() //Interrupt function
{
count++;
}
void process() //Timer handler
{
if(count>1) //1000ms interrupt number greater than 1 is considered detected a moving object (this value can be adjusted according to the actual situation, equivalent to adjust the de
{
state = HIGH;
Allon();
count=0; //Count zero
}
else
count=0; //In 1000ms, interrupts does not reach set threshold value is considered not detect moving objects, interrupt the count number is cleared to zero.
}
void Allon() {
lc.shutdown(1,true);
lc.shutdown(0,false);
for (int row=0; row<8; row++)
{
for (int col=0; col<8; col++)
{
lc.setLed(0,col,row,true); // turns on LED at col, row
delay(10);
}
}
}
void Alloff() {
lc.shutdown(1,true);
lc.shutdown(0,false);
for (int row=0; row<8; row++)
{
for (int col=0; col<8; col++)
{
lc.setLed(0,col,row,false); // turns on LED at col, row
delay(10);
}
}
}
首先本人并没有逐行注释的习惯,但为了让阅读者顺利读码,特意在关键的地方打上了注释,
你就是最靓的仔 LuckyE 发表于 2020-5-30 16:01
你就是最靓的仔
可不敢当 果然很闪亮
页:
[1]