Input Sensor for motion

This commit is contained in:
Juergen Jung 2017-02-17 03:01:52 +01:00
parent 7e308b3f07
commit 0a4eda713d
2 changed files with 21 additions and 5 deletions

View File

@ -6,12 +6,12 @@
#include "NeoPatterns.h" #include "NeoPatterns.h"
#define PIN D1 #define PIN D1
#define SENSOR D3
#define NUMPIXELS 144 #define NUMPIXELS 144
byte wPos = 0; bool lastSensorValue = false;
uint8_t state = 0;
void StripComplete(){ void StripComplete(){
return; return;
@ -20,6 +20,8 @@ void StripComplete(){
NeoPatterns pixels = NeoPatterns(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800,&StripComplete); NeoPatterns pixels = NeoPatterns(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800,&StripComplete);
HomieNode stripNode("strip", "strip"); HomieNode stripNode("strip", "strip");
HomieNode sensorNode("sensor", "sensor");
Bounce debouncer = Bounce();
bool onSetPixel(const HomieRange& range, const String& value){ bool onSetPixel(const HomieRange& range, const String& value){
if(!range.isRange) { if(!range.isRange) {
@ -60,12 +62,11 @@ bool onSetEffect(const HomieRange& range, const String& value){
pixels.TheaterChase(pixels.Color(0,0,255), pixels.Color(255,0,00), 100); pixels.TheaterChase(pixels.Color(0,0,255), pixels.Color(255,0,00), 100);
} }
else if(effect == "fade") { else if(effect == "fade") {
pixels.Fade(pixels.Wheel(255),pixels.Wheel(0),255,25); pixels.Fade(pixels.Wheel(0),pixels.Wheel(255),pixels.Color(255,255,255),25);
} }
else { else {
pixels.None(); pixels.None();
} }
stripNode.setProperty("effect").send(value); stripNode.setProperty("effect").send(value);
} }
@ -89,19 +90,33 @@ bool onSetLength(const HomieRange& range, const String& value){
void loopHandler() { void loopHandler() {
pixels.Update(); pixels.Update();
int sensorValue = debouncer.read();
if (Homie.isConfigured() && Homie.isConnected() && sensorValue != lastSensorValue) {
sensorNode.setProperty("motion").send(sensorValue ? "true" : "false");
lastSensorValue = sensorValue;
}
} }
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
debouncer.attach(SENSOR,INPUT);
debouncer.interval(50);
Homie_setFirmware("schild", "1.0.0"); Homie_setFirmware("schild", "1.0.0");
Homie.setLoopFunction(loopHandler); Homie.setLoopFunction(loopHandler);
stripNode.advertiseRange("pixel", 0, NUMPIXELS-1).settable(onSetPixel); stripNode.advertiseRange("pixel", 0, NUMPIXELS-1).settable(onSetPixel);
stripNode.advertise("brightness").settable(onSetBrightness); stripNode.advertise("brightness").settable(onSetBrightness);
stripNode.advertise("effect").settable(onSetEffect); stripNode.advertise("effect").settable(onSetEffect);
stripNode.advertise("clear").settable(onSetClear); stripNode.advertise("clear").settable(onSetClear);
stripNode.advertise("length").settable(onSetLength); stripNode.advertise("length").settable(onSetLength);
sensorNode.advertise("motion");
pixels.begin(); pixels.begin();
pixels.clear(); pixels.clear();
pixels.setBrightness(64); pixels.setBrightness(64);
@ -116,5 +131,6 @@ void setup() {
void loop() { void loop() {
Homie.loop(); Homie.loop();
debouncer.update();
ArduinoOTA.handle(); ArduinoOTA.handle();
} }

View File

@ -13,7 +13,7 @@ platform=espressif8266
board=d1_mini board=d1_mini
framework=arduino framework=arduino
;upload_port = /dev/tty.wchusbserial640 ;upload_port = /dev/tty.wchusbserial410
;upload_speed = 921600 ;upload_speed = 921600
;upload_port = 5ccf7f1db369.local ;upload_port = 5ccf7f1db369.local