Site updated at 2016-04-14 15:50:18 UTC
This commit is contained in:
parent
ca40d62a3d
commit
e1af4db93e
17 changed files with 398 additions and 184 deletions
|
@ -159,6 +159,80 @@
|
|||
|
||||
<p>For more information, visit the <a href="https://www.mysensors.org/download/serial_api_15">serial api</a> of MySensors.</p>
|
||||
|
||||
<h3><a class="title-link" name="example-sketch" href="#example-sketch"></a> Example sketch</h3>
|
||||
|
||||
<p>```c++<br />
|
||||
/*<br />
|
||||
* Documentation: http://www.mysensors.org<br />
|
||||
* Support Forum: http://forum.mysensors.org<br />
|
||||
*<br />
|
||||
* http://www.mysensors.org/build/dimmer<br />
|
||||
*/</p>
|
||||
|
||||
<p>#include <mysensor.h>
|
||||
#include <spi.h></spi.h></mysensor.h></p>
|
||||
|
||||
<p>#define SN “DimmableRGBLED”<br />
|
||||
#define SV “1.0”<br />
|
||||
#define CHILD_ID 1<br />
|
||||
#define LED_PIN 5</p>
|
||||
|
||||
<p>MySensor gw;</p>
|
||||
|
||||
<p>char rgb[7] = “ffffff”; // RGB value.<br />
|
||||
int currentLevel = 0; // Current dimmer level.<br />
|
||||
MyMessage dimmerMsg(CHILD_ID, V_PERCENTAGE);<br />
|
||||
MyMessage lightMsg(CHILD_ID, V_STATUS);<br />
|
||||
MyMessage rgbMsg(CHILD_ID, V_RGB);</p>
|
||||
|
||||
<p>void setup()<br />
|
||||
{<br />
|
||||
gw.begin(incomingMessage);<br />
|
||||
gw.sendSketchInfo(SN, SV);<br />
|
||||
gw.present(CHILD_ID, S_RGB_LIGHT);<br />
|
||||
// Send initial values.<br />
|
||||
gw.send(lightMsg.set(currentLevel > 0 ? 1 : 0));<br />
|
||||
gw.send(dimmerMsg.set(currentLevel));<br />
|
||||
gw.send(rgbMsg.set(rgb));<br />
|
||||
}</p>
|
||||
|
||||
<p>void loop()<br />
|
||||
{<br />
|
||||
gw.process();<br />
|
||||
}</p>
|
||||
|
||||
<p>void incomingMessage(const MyMessage &message) {<br />
|
||||
if (message.type == V_RGB) {<br />
|
||||
// Retrieve the RGB value from the incoming message.<br />
|
||||
// RGB LED not implemented, just a dummy print.<br />
|
||||
String hexstring = message.getString();<br />
|
||||
hexstring.toCharArray(rgb, sizeof(rgb));<br />
|
||||
Serial.print(“Changing color to “);<br />
|
||||
Serial.println(rgb);<br />
|
||||
gw.send(rgbMsg.set(rgb));<br />
|
||||
}</p>
|
||||
|
||||
<p>if (message.type == V_STATUS || message.type == V_PERCENTAGE) {<br />
|
||||
// Retrieve the light status or dimmer level from the incoming message.<br />
|
||||
int requestedLevel = atoi(message.data);</p>
|
||||
|
||||
<pre><code>// Adjust incoming level if this is a V_LIGHT update [0 == off, 1 == on].
|
||||
requestedLevel *= (message.type == V_STATUS ? 100 : 1);
|
||||
|
||||
// Clip incoming level to valid range of 0 to 100
|
||||
requestedLevel = requestedLevel > 100 ? 100 : requestedLevel;
|
||||
requestedLevel = requestedLevel < 0 ? 0 : requestedLevel;
|
||||
|
||||
// Change level value of LED pin.
|
||||
analogWrite(LED_PIN, (int)(requestedLevel / 100. * 255));
|
||||
currentLevel = requestedLevel;
|
||||
|
||||
// Update the gateway with the current V_STATUS and V_PERCENTAGE.
|
||||
gw.send(lightMsg.set(currentLevel > 0 ? 1 : 0));
|
||||
gw.send(dimmerMsg.set(currentLevel));
|
||||
} } ```
|
||||
</code></pre>
|
||||
|
||||
|
||||
|
||||
</article>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue