Following my last article on EnOcean library for Arduino I received some email because the usage example was not good. Indeed I probably didn’t copy past the last version of code so I decided to do a new example (and also improve the library).
This time I will command 1 LED strip using an EnOcean button PTM210. A short click on the button will change the LED strip color to a random one. A long click (more than 2s) turns it off.
Here is the result:
The system requires a strip RGB led, an Arduino Leonardo, an EnOcean TCM 310 receiver and an EnOcean PTM210 emitter (button).
A made few modifications to my first EnOcean library by adding a new variable to check if a message were received on the TCM310. I also add a method to clean the “message” object. The EnOcean library is still available on my bitbucket repo (HERE)
The code is pretty simple. On each loop we check if a new message is available on the TCM310.
void loop() { delay(50); _Msg.decode(); if (_Msg.dataAvailable() == true) { actionTaker(); _Msg.reset(); } }
If yes we will either start a timer if it is a button press or take an action if it is a button release. The action depend if the timer is finish or not (to determine if it is a long or short click).
void actionTaker() { if (_Msg.getPayload() == 0x70) { Serial.println("The user push the button. Nothing to do appart starting the timer"); _TimerExpire = false; MsTimer2::start(); // active Timer 2 } else if (_Msg.getPayload() == 0x00) { Serial.println("The user stop pushing the enocean button"); if (_TimerExpire == true) { Serial.println("The user push was more than the timer (long push)"); TurnOffLed(); _NbClick=0; } else { Serial.println("The user push was quick (short push)"); RandomLedColor(); _NbClick=0; } } else { Serial.print("Received an unsupported command : "); Serial.println(_Msg.getPayload()); } }
There is also some extra function to determine a rand color and send it to the LED. The whole code is available on bitbucket (HERE)