add hardware button

This commit is contained in:
interfisch 2019-02-10 21:13:37 +01:00
parent 40d1f31a17
commit 5f97df2ab9
1 changed files with 46 additions and 5 deletions

View File

@ -11,6 +11,9 @@
#define PIN_LAMP1 D8 #define PIN_LAMP1 D8
#define PIN_SENSOR D0 #define PIN_SENSOR D0
#define PIN_BUTTON0 D2
#define PIN_BUTTON1 D3
//#define FULL 255 //#define FULL 255
//#define LOWER 50 //#define LOWER 50
//#define MINIMUM 1 //#define MINIMUM 1
@ -56,8 +59,8 @@ int tempincreasemax1=10; //the higher the faster lightup
#define FLASHPROBABILITY_MAX 600 #define FLASHPROBABILITY_MAX 600
#define FLASHPROBABILITYMIN_MIN 50 #define FLASHPROBABILITYMIN_MIN 50
#define FLASHPROBABILITYMIN_MAX 100 //should not be more than FLASHPROBABILITY_MIN #define FLASHPROBABILITYMIN_MAX 100 //should not be more than FLASHPROBABILITY_MIN
#define TEMPINCREASEMAX_MIN 5 #define TEMPINCREASEMAX_MIN 1
#define TEMPINCREASEMAX_MAX FLUORESCENTTEMPMAX/10 #define TEMPINCREASEMAX_MAX FLUORESCENTTEMPMAX/5
bool lastSensorValue = false; bool lastSensorValue = false;
@ -66,6 +69,9 @@ HomieNode lightNode("lamp", "lamp");
HomieNode sensorNode("sensor", "sensor"); HomieNode sensorNode("sensor", "sensor");
Bounce debouncer = Bounce(); Bounce debouncer = Bounce();
Bounce debouncer_btn0 = Bounce();
Bounce debouncer_btn1 = Bounce();
bool fluorescentHandler(const HomieRange& range, const String& value) { bool fluorescentHandler(const HomieRange& range, const String& value) {
@ -88,6 +94,10 @@ bool fluorescentHandler(const HomieRange& range, const String& value) {
return true; return true;
} }
bool fluorescent0Handler(const HomieRange& range, const String& value) { bool fluorescent0Handler(const HomieRange& range, const String& value) {
return fluorescent0Handler_change(value);
}
bool fluorescent0Handler_change(String value){
Homie.getLogger() << "fluorescent0 " << ": " << value << endl; Homie.getLogger() << "fluorescent0 " << ": " << value << endl;
lightNode.setProperty("fluorescent0").send(value); lightNode.setProperty("fluorescent0").send(value);
setbrightness0 = value.toInt(); setbrightness0 = value.toInt();
@ -103,6 +113,9 @@ bool fluorescent0Handler(const HomieRange& range, const String& value) {
return true; return true;
} }
bool fluorescent1Handler(const HomieRange& range, const String& value) { bool fluorescent1Handler(const HomieRange& range, const String& value) {
return fluorescent1Handler_change(value);
}
bool fluorescent1Handler_change(String value){
Homie.getLogger() << "fluorescent1 " << ": " << value << endl; Homie.getLogger() << "fluorescent1 " << ": " << value << endl;
lightNode.setProperty("fluorescent1").send(value); lightNode.setProperty("fluorescent1").send(value);
setbrightness1 = value.toInt(); setbrightness1 = value.toInt();
@ -137,10 +150,10 @@ void resetLamp1(){
float speedpercent=fluorescent1Quality*1.0/FLUORESCENQUALITYMAX; float speedpercent=fluorescent1Quality*1.0/FLUORESCENQUALITYMAX;
flashprobability1=random(FLASHPROBABILITY_MIN,FLASHPROBABILITY_MAX); flashprobability1=random(FLASHPROBABILITY_MIN,FLASHPROBABILITY_MAX);
flashprobabilitymin1=random(FLASHPROBABILITYMIN_MIN,FLASHPROBABILITYMIN_MAX); flashprobabilitymin1=random(FLASHPROBABILITYMIN_MIN,FLASHPROBABILITYMIN_MAX);
tempincreasemax0=TEMPINCREASEMAX_MIN+speedpercent*(TEMPINCREASEMAX_MAX-TEMPINCREASEMAX_MIN); tempincreasemax1=TEMPINCREASEMAX_MIN+speedpercent*(TEMPINCREASEMAX_MAX-TEMPINCREASEMAX_MIN);
} }
bool fluorescentAgeHandler(const HomieRange& range, const String& value) { bool fluorescentQualityHandler(const HomieRange& range, const String& value) {
Homie.getLogger() << "fluorescentAge " << ": " << value << endl; Homie.getLogger() << "fluorescentAge " << ": " << value << endl;
fluorescent0Quality = constrain(value.toInt(),0,FLUORESCENQUALITYMAX); fluorescent0Quality = constrain(value.toInt(),0,FLUORESCENQUALITYMAX);
fluorescent1Quality = constrain(value.toInt(),0,FLUORESCENQUALITYMAX); fluorescent1Quality = constrain(value.toInt(),0,FLUORESCENQUALITYMAX);
@ -283,6 +296,24 @@ void loopHandler()
lastSensorValue = sensorValue; lastSensorValue = sensorValue;
} }
bool btn0 = debouncer_btn0.read();
bool btn1 = debouncer_btn1.read();
if (!btn0){
if (setbrightness0==0){
fluorescent0Handler_change("255");
}else{
fluorescent0Handler_change("0");
}
}
if (!btn1){
if (setbrightness1==0){
fluorescent1Handler_change("255");
}else{
fluorescent1Handler_change("0");
}
}
} }
void setup() { void setup() {
@ -294,6 +325,12 @@ void setup() {
pinMode(PIN_LAMP1_EDGE, OUTPUT); pinMode(PIN_LAMP1_EDGE, OUTPUT);
pinMode(PIN_LAMP1, OUTPUT); pinMode(PIN_LAMP1, OUTPUT);
debouncer_btn0.attach(PIN_BUTTON0, INPUT_PULLUP);
debouncer_btn0.interval(50);
debouncer_btn1.attach(PIN_BUTTON1, INPUT_PULLUP);
debouncer_btn1.interval(50);
debouncer.attach(PIN_SENSOR, INPUT); debouncer.attach(PIN_SENSOR, INPUT);
debouncer.interval(50); debouncer.interval(50);
@ -306,7 +343,7 @@ void setup() {
lightNode.advertise("lamp0").settable(lamp0Handler); lightNode.advertise("lamp0").settable(lamp0Handler);
lightNode.advertise("lamp1").settable(lamp1Handler); lightNode.advertise("lamp1").settable(lamp1Handler);
lightNode.advertise("fluorescent").settable(fluorescentHandler); lightNode.advertise("fluorescent").settable(fluorescentHandler);
lightNode.advertise("fluorescentage").settable(fluorescentAgeHandler); lightNode.advertise("fluorescentquality").settable(fluorescentQualityHandler);
lightNode.advertise("fluorescent0").settable(fluorescent0Handler); lightNode.advertise("fluorescent0").settable(fluorescent0Handler);
lightNode.advertise("fluorescent0Quality").settable(fluorescent0QualityHandler); lightNode.advertise("fluorescent0Quality").settable(fluorescent0QualityHandler);
lightNode.advertise("fluorescent1").settable(fluorescent1Handler); lightNode.advertise("fluorescent1").settable(fluorescent1Handler);
@ -329,5 +366,9 @@ void setup() {
void loop() { void loop() {
Homie.loop(); Homie.loop();
debouncer.update(); debouncer.update();
debouncer_btn0.update();
debouncer_btn1.update();
ArduinoOTA.handle(); ArduinoOTA.handle();
} }