|
原来用1637正常显示,屏幕太小,现在换成1650,温度能显示,但是按键不能切换了,哪位帮看看输出那块问题
- #include <Arduino.h>
- #include <Wire.h>
- #include <TM1650.h>
- TM1650 d;
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #define ONE_WIRE_BUS 14 //温度引脚
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature DS18B20(&oneWire);
- #define BTN_INPUT 12 //按键引脚
- const int MAX_DISPLAY_MODE = 2;//按键二级菜单
- int display_mode = 0;
- uint8_t data[] = { 0x00, 0x00, 0x00, 0x00 };
- int brightness = 1; //亮度设置0-7
- int alarm_setup_mode = -1; //-1: off 0~3分别对应四位
- float temperature = 0;
- uint32_t timestamp_TEMP = 0; //温度
- uint32_t timestamp_btn = 0; //按键
- uint32_t timestamp_lasthit = 0;
- bool btn_state = false;
- int x;//定义变量x,储存下限阈值
- int s;//定义变量s,储存上限阈值
- void set_colon(bool flag)
- {
- uint8_t num = 0x00;
- if (flag)
- num = 0x00;
- int i;
- for (i = 0; i < 4; i++)
- {
- data[i] = data[i] | num;
- }
- }
- char line[4];
- void set_data_use_temperature() //温度输出
- {
- int num = (int)temperature;
- if (num < 10)
- data[0] = 0x00;
- else
- itoa(temperature, line, 10);
- d.displayString(line);
- // data[0] = d.displayRunning(num / 10);
- // data[1] = d.displayString(num % 10);
- // data[2] = SEG_A | SEG_B | SEG_F | SEG_G; //o
- // data[3] = SEG_A | SEG_D | SEG_E | SEG_F; //C
- set_colon(false);
- }
- void set_data_use_alarm()
- {
- uint32_t num1, num2;
- num1 = x;
- num2 = s;
- data[0] = d.displayString(num1 / 10);
- data[1] = d.displayString(num1 % 10);
- data[2] = d.displayString(num2 / 10);
- data[3] = d.displayString(num2 % 10);
- if (alarm_setup_mode != -1)
- {
- if (millis() % 700 < 350)
- data[alarm_setup_mode] = 0x00;
- }
- set_colon(true);
- }
- void set_data(int display_mode) //显示模式切换
- {
- switch (display_mode)
- {
- case 0:
- set_data_use_temperature();
- break;
- case 1:
- set_data_use_alarm();
- break;
- default:
- data[0] = 0xff;
- data[1] = 0xff;
- data[2] = 0xff;
- data[3] = 0xff;
- }
- }
- void setup()
- {
- Serial.begin(115200);
- DS18B20.begin();
- x = 10;// x初始值
- s = 20;// s初始值
- pinMode(BTN_INPUT, INPUT);//按键输出
- attachInterrupt(digitalPinToInterrupt(BTN_INPUT), btn_trigger, CHANGE);
- d.setBrightness(brightness);
- Wire.begin();
- d.init();
- pinMode(13, OUTPUT);// 继电器输出
- digitalWrite(13, HIGH);//默认输出高电平
- }
- void loop()
- {
- uint32_t time_now = millis();
- //更新温度到数码管
- if (time_now - timestamp_TEMP > 2000)
- {
- DS18B20.requestTemperatures();// 发送命令获取温度
- temperature = DS18B20.getTempCByIndex(0);
- // Serial.print(temperature);//调试输出
- }
- set_data(display_mode);
- // d.setSegments(data);
- // d.displayString("3214");
- if(temperature<x)
- {
- ;
- digitalWrite(13, HIGH);
- }
-
- if(temperature>s)
- {
- ;
- digitalWrite(13, LOW);
- }//高于上限关闭
- }
- void alarm_add(int digit)
- {
- if (digit == 0)
- {
- x += 1;
- if (x >= 30)
- {
- x = x % 10;
- }
- }
- if (digit == 1)
- {
- x -= 1;
- if (x >= 30)
- {
- x -= 1;
- }
- }
- if (digit == 2)
- {
- s += 1;
- if (s >= 50)
- {
- s = s % 10;
- }
- }
- if (digit == 3)
- {
- s -= 1;
- if (s>= 50)
- {
- s -= 1;
- }
- }
- }
- void btn_press()
- {
- switch (display_mode)
- {
- case -1:
- display_mode = 0;
- break;
- case 1:
- if (alarm_setup_mode != -1)
- {
- alarm_add(alarm_setup_mode);
- break;
- }
- else
- {
- display_mode++;
- if (display_mode == MAX_DISPLAY_MODE)
- display_mode = 0;
- }
- break;
- default:
- display_mode++;
- if (display_mode == MAX_DISPLAY_MODE)
- display_mode = 0;
- }
- }
- void btn_hold()
- {
- switch (display_mode)
- {
- case 1:
- alarm_setup_mode++;
- if (alarm_setup_mode ==4)
- alarm_setup_mode = -1;
- break;
- default:
- display_mode = -1;
- }
- }
- ICACHE_RAM_ATTR void btn_trigger()
- {
- uint32_t time_now = millis();
- if (!btn_state)
- {
- timestamp_btn = time_now;
- }
- else
- {
- if (time_now - timestamp_lasthit > 200)
- {
- if (time_now - timestamp_btn < 500)
- {
- //Serial.println("短按");
- btn_press();
- }
- else
- {
- //Serial.println("长按");
- btn_hold();
- }
- timestamp_lasthit = time_now;
- }
- }
- btn_state = digitalRead(BTN_INPUT);
- }
-
复制代码
|
|