blinker电源插座-Arduino中文社区 - Powered by Discuz! Archiver

lwq1947 发表于 2020-1-10 06:41

blinker电源插座

这台blinker电源插座有blinker控制和手动控制两种工作方式.
1.blinker控制:滑条用于设定时时间,数据块用于显示定时剩余时间,注意两者最大值应统一.两个开关按键一个用于起停定时控制,一个用于开关电源插座.
2.手动控制:插座上有三个按纽和一个LED显示器.一个按纽用于定时起停或开关电源(短按起或停长按开或关).另两个按纽为增减定时时间设定(最大值为999分)
短按为慢调,长按为快调.
如有需求程序再上传.

shark526 发表于 2020-1-10 16:14

发下代码呗, 我想看下你按钮怎么去抖的:lol

shark526 发表于 2020-1-10 16:15

另外有没有硬件组装过程图呀?比较好奇你用了什么继电器, 怎么塞进去的,嘿嘿

lwq1947 发表于 2020-1-10 18:03

本帖最后由 lwq1947 于 2020-1-10 18:05 编辑

由于空间有限所以电源控制采用了双向可控硅元件.组装过程没拍照.上传一张最终图片.程序如下:
#define BLINKER_WIFI
#include <Blinker.h>
#include <Arduino.h>
#include <Ticker.h>
#include <TM1637Display.h>
#define CLK 3
#define DIO 1
TM1637Display display(CLK, DIO);
Ticker flipper;
char auth[] = "**********";
char ssid[] ="**********";
char pswd[] = "*********";

#define BUTTON_1 "ButtonKey"
BlinkerButton Button1(BUTTON_1);
BlinkerButton Button("star");
#define Slider_1 "SliderKey"
BlinkerSlider Slider1(Slider_1);
BlinkerNumber Tval("tval");
int t;
int i=0;
int j=0;
int k=0;
int l=0;
uint32_t BlinkerTime;
uint32_t pTime=0;
uint32_t dTime=0;
float tval_read=1;

void flip() {   
if(digitalRead(2)==LOW) i=0;
if(digitalRead(2)==HIGH){
i++;
if((i>59)&&(t>0)){
i=0;
t--;
Tval.print(tval_read);
tval_read = t;
Tval.print(tval_read);
tval_read = t;
if(t==0)digitalWrite(2, LOW);
}
}
}
void button_callback(const String & state)   
{
BLINKER_LOG("get button state: ", state);
   if (state == BLINKER_CMD_BUTTON_TAP) {
      BLINKER_LOG("Button tap!");
      digitalWrite(2, !digitalRead(2));
      if(digitalRead(2)==HIGH)
      {Button.icon("fas fa-lightbulb fa-spin");
      Button.color("#FF0000");
      Button.text("启动");   
      i=0;
      j=0;}
       else
       { Button.icon("fal fa-lightbulb ");
      Button.color("#0000FF");
      Button.text("停止");
       }
      Button.print();
    }

}
void button1_callback(const String & state)   
{
BLINKER_LOG("get button state: ", state);
if (state=="on")
   {
   Button1.icon("fas fa-sun fa-spin");//fa-lightbulb
      Button1.color("#FF0000");
      Button1.text("开");
    digitalWrite(15, HIGH);
   Button1.print("on");
    }
    else if(state=="off")
    {
       Button1.icon("fal fa-moon");
      Button1.color("#0000FF");
      Button1.text("关");
      digitalWrite(15, LOW);
      digitalWrite(2, LOW);
      Button1.print("off");
   }
}

void slider1_callback(int32_t value)
{
    BLINKER_LOG("get slider value: ", value);
    t=value;
    Tval.print(tval_read);
   tval_read = t;
    t=value;
   Tval.print(tval_read);
   tval_read = t;
}
void dataRead(const String & data)
{
    BLINKER_LOG("Blinker readString: ", data);

    Blinker.vibrate();
   
    uint32_t BlinkerTime = millis();
Blinker.print(BlinkerTime);
    Blinker.print("millis", BlinkerTime);
}
void heartbeat()
{ Tval.print(tval_read);
}
void setup()
{
    Serial.begin(115200);
    BLINKER_DEBUG.stream(Serial);
    BLINKER_DEBUG.debugAll();
flipper.attach(1, flip);
pinMode(0,INPUT);
pinMode(12,INPUT);
pinMode(16,INPUT);
pinMode(15,OUTPUT);
pinMode(2,OUTPUT);
digitalWrite(0,HIGH);
digitalWrite(12,HIGH);
digitalWrite(16,HIGH);
digitalWrite(15,LOW);
digitalWrite(2, LOW);   
    Blinker.begin(auth, ssid, pswd);
   Blinker.attachHeartbeat(heartbeat);
    Blinker.attachData(dataRead);
    Button1.attach(button1_callback);
   Button.attach(button_callback);
    Slider1.attach(slider1_callback);
}

void loop()
{
    Blinker.run();
   tval_read = t;
    display.setBrightness(0x04);
   display.showNumberDec(t, false);
delay(10);
BlinkerTime = millis();
if ( BlinkerTime - pTime > 50) {
j++;
pTime= BlinkerTime;}
if((k==0)&&((digitalRead(16)==LOW)||(digitalRead(12)==LOW)||(digitalRead(0)==LOW))){
k=1;
dTime= BlinkerTime;
if(digitalRead(0)==LOW)digitalWrite(2, !digitalRead(2));
}
if((digitalRead(16)==HIGH)&&(digitalRead(12)==HIGH)&&(digitalRead(0)==HIGH)){
k=0;
l=0;
}
if(( BlinkerTime - dTime > 3000)&&(digitalRead(16)==LOW))t++;
if(( BlinkerTime - dTime > 3000)&&(digitalRead(12)==LOW)&&(t>0))t--;

if(( BlinkerTime - dTime > 3000)&&(digitalRead(0)==LOW)&&(l==0)){
digitalWrite(15, !digitalRead(15));
if(digitalRead(15)==LOW)digitalWrite(2, LOW);
l=1;
}
if(j>9)
{
j=0;
if(digitalRead(16)==LOW)t++;
if((digitalRead(12)==LOW)&&(t>0))t--;

}
}


lvshouxi180 发表于 2020-1-14 21:01

厉害,膜拜中

yangjie_sz 发表于 2020-2-26 00:32

膜拜大神
页: [1]
查看完整版本: blinker电源插座