|
| 1 | +/** |
| 2 | + * The MySensors Arduino library handles the wireless radio link and protocol |
| 3 | + * between your home built sensors/actuators and HA controller of choice. |
| 4 | + * The sensors forms a self healing radio network with optional repeaters. Each |
| 5 | + * repeater and gateway builds a routing tables in EEPROM which keeps track of the |
| 6 | + * network topology allowing messages to be routed to nodes. |
| 7 | + * |
| 8 | + * Created by Henrik Ekblad <[email protected]> |
| 9 | + * Copyright (C) 2013-2015 Sensnology AB |
| 10 | + * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors |
| 11 | + * |
| 12 | + * Documentation: http://www.mysensors.org |
| 13 | + * Support Forum: http://forum.mysensors.org |
| 14 | + * |
| 15 | + * This program is free software; you can redistribute it and/or |
| 16 | + * modify it under the terms of the GNU General Public License |
| 17 | + * version 2 as published by the Free Software Foundation. |
| 18 | + * |
| 19 | + ******************************* |
| 20 | + * |
| 21 | + * REVISION HISTORY |
| 22 | + * Version 1.0 - Created by vil1driver |
| 23 | + * |
| 24 | + * DESCRIPTION |
| 25 | + * RGB led strip controled with three dimmers + one On/Off for run/stop rgb color cycle :p |
| 26 | + * |
| 27 | + */ |
| 28 | + |
| 29 | +#define SN "RGB Led strip 3D" |
| 30 | +#define SV "v1" |
| 31 | + |
| 32 | + |
| 33 | + // Load mysensors library |
| 34 | + #include <MySensor.h> |
| 35 | + // Load Serial Peripheral Interface library |
| 36 | + #include <SPI.h> |
| 37 | + |
| 38 | + // Arduino pin attached to MOSFET Gate pin |
| 39 | + #define RED_PIN 3 |
| 40 | + #define GREEN_PIN 5 |
| 41 | + #define BLUE_PIN 6 |
| 42 | + |
| 43 | + MySensor gw; |
| 44 | + |
| 45 | + // Define message name and type to send sensor info |
| 46 | + MyMessage RedStatus(RED_PIN, V_DIMMER); |
| 47 | + MyMessage GreenStatus(GREEN_PIN, V_DIMMER); |
| 48 | + MyMessage BlueStatus(BLUE_PIN, V_DIMMER); |
| 49 | + MyMessage Status(1, V_DIMMER); |
| 50 | + MyMessage rgbShowState(0, V_LIGHT); |
| 51 | + |
| 52 | + // Serial.print translate sensor id to sensor name |
| 53 | + char color[][6] = {"","","","RED","","GREEN","BLUE"}; |
| 54 | + |
| 55 | + // Vars for rgbShow function |
| 56 | + int redval = 0; |
| 57 | + int greenval = 0; |
| 58 | + int blueval = 0; |
| 59 | + long time=0; |
| 60 | + int isShow; |
| 61 | + |
| 62 | + void setup() |
| 63 | + { |
| 64 | + // Initializes the sensor node (Callback function for incoming messages, node id, is repeater) |
| 65 | + gw.begin(incomingMessage, 31, true); |
| 66 | + |
| 67 | + // Present sketch (name, version) |
| 68 | + gw.sendSketchInfo(SN, SV); |
| 69 | + |
| 70 | + // Register sensors (id, type, description, ack back) |
| 71 | + gw.present(RED_PIN, S_DIMMER, "present RED light", false); |
| 72 | + gw.present(GREEN_PIN, S_DIMMER, "present GREEN light", false); |
| 73 | + gw.present(BLUE_PIN, S_DIMMER, "present BLUE light", false); |
| 74 | + gw.present(0, S_LIGHT, "present Show button", false); |
| 75 | + |
| 76 | + // Define pin mode (pin number, type) |
| 77 | + pinMode(RED_PIN, OUTPUT); |
| 78 | + pinMode(GREEN_PIN, OUTPUT); |
| 79 | + pinMode(BLUE_PIN, OUTPUT); |
| 80 | + |
| 81 | + // Correct saved RGB value for first start |
| 82 | + gw.saveState(RED_PIN, constrain(gw.loadState(RED_PIN), 0, 100)); |
| 83 | + gw.saveState(GREEN_PIN, constrain(gw.loadState(GREEN_PIN), 0, 100)); |
| 84 | + gw.saveState(BLUE_PIN, constrain(gw.loadState(BLUE_PIN), 0, 100)); |
| 85 | + |
| 86 | + // Get value from eeprom and write to output |
| 87 | + analogWrite(RED_PIN, 255 * gw.loadState(RED_PIN) / 100); |
| 88 | + analogWrite(GREEN_PIN, 255 * gw.loadState(GREEN_PIN) / 100); |
| 89 | + analogWrite(BLUE_PIN, 255 * gw.loadState(BLUE_PIN) / 100); |
| 90 | + |
| 91 | + // Write some debug info |
| 92 | + Serial.print("Load from eeprom RED: "); |
| 93 | + Serial.print(gw.loadState(RED_PIN)); |
| 94 | + Serial.println("%"); |
| 95 | + Serial.print("Load from eeprom GREEN: "); |
| 96 | + Serial.print(gw.loadState(GREEN_PIN)); |
| 97 | + Serial.println("%"); |
| 98 | + Serial.print("Load from eeprom BLUE: "); |
| 99 | + Serial.print(gw.loadState(BLUE_PIN)); |
| 100 | + Serial.println("%"); |
| 101 | + |
| 102 | + // Send RGB value to controler (request ack back: true/false) |
| 103 | + Serial.println("Send eeprom value to controler"); |
| 104 | + gw.send( RedStatus.set(gw.loadState(RED_PIN)), false ); |
| 105 | + gw.send( GreenStatus.set(gw.loadState(GREEN_PIN)), false ); |
| 106 | + gw.send( BlueStatus.set(gw.loadState(BLUE_PIN)), false ); |
| 107 | + |
| 108 | + // Correct RGB show state for first start and load it (set to 'On' at first start) |
| 109 | + gw.saveState(0, constrain(gw.loadState(0), 0, 1)); |
| 110 | + isShow=gw.loadState(0); |
| 111 | + |
| 112 | + // Send RGB show state to controler (request ack back: true/false) |
| 113 | + gw.send( rgbShowState.set(isShow), false); |
| 114 | + |
| 115 | + if (isShow==1){ Serial.println("RGB show running..."); } |
| 116 | + Serial.println("Ready to receive messages..."); |
| 117 | + } |
| 118 | + |
| 119 | + void loop() |
| 120 | + { |
| 121 | + // Process incoming messages (like config and light state from controller) |
| 122 | + gw.process(); |
| 123 | + |
| 124 | + // Run RGB show if is set |
| 125 | + if (isShow==1) |
| 126 | + { |
| 127 | + rgbShow(); |
| 128 | + analogWrite(RED_PIN, redval); |
| 129 | + analogWrite(GREEN_PIN, greenval); |
| 130 | + analogWrite(BLUE_PIN, blueval); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + void incomingMessage(const MyMessage &message) |
| 136 | + { |
| 137 | + if (message.isAck()) |
| 138 | + { |
| 139 | + Serial.println("Got ack from gateway"); |
| 140 | + } |
| 141 | + if (message.type == V_LIGHT) |
| 142 | + { |
| 143 | + // Incoming on/off command sent from controller ("1" or "0") |
| 144 | + int lightState = message.getString()[0] == '1'; |
| 145 | + |
| 146 | + // if receive RGB Show On commands, start the show |
| 147 | + if (message.sensor==0 && lightState==1){ rgbShowOn(); } |
| 148 | + // if receive RGB Show Off commands, stop the show |
| 149 | + else if (message.sensor==0 && lightState==0){ rgbShowOff(); } |
| 150 | + |
| 151 | + // if receive RGB switch On command |
| 152 | + else if (lightState==1) |
| 153 | + { |
| 154 | + // Write some debug info |
| 155 | + Serial.print("Incoming change for "); |
| 156 | + Serial.print(color[message.sensor]); |
| 157 | + Serial.println(": On"); |
| 158 | + Serial.print("Load from eeprom: "); |
| 159 | + |
| 160 | + if ( gw.loadState(message.sensor) == 0) |
| 161 | + { |
| 162 | + // Pick up last saved dimmer level from the eeprom |
| 163 | + analogWrite(message.sensor, 255 * gw.loadState(10*message.sensor) / 100); |
| 164 | + // Save loaded value to current |
| 165 | + gw.saveState(message.sensor, gw.loadState(10*message.sensor)); |
| 166 | + Serial.print(gw.loadState(10*message.sensor)); |
| 167 | + Serial.println("%"); |
| 168 | + // Send value to controler |
| 169 | + Serial.println("Send value to controler"); |
| 170 | + gw.send(Status.setSensor(message.sensor).set(gw.loadState(10*message.sensor)),false); |
| 171 | + } |
| 172 | + else |
| 173 | + { |
| 174 | + // Pick up last saved dimmer level from the eeprom |
| 175 | + analogWrite(message.sensor, 255 * gw.loadState(message.sensor) / 100); |
| 176 | + Serial.print(gw.loadState(message.sensor)); |
| 177 | + Serial.println("%"); |
| 178 | + // Send value to controler |
| 179 | + Serial.println("Send value to controler"); |
| 180 | + gw.send(Status.setSensor(message.sensor).set(gw.loadState(message.sensor)),false); |
| 181 | + } |
| 182 | + // Stop the show if it's running |
| 183 | + if (isShow==1){ rgbShowStop(message.sensor); } |
| 184 | + } |
| 185 | + // if recieve switch Off command |
| 186 | + else if (lightState==0) |
| 187 | + { |
| 188 | + // Write output to 0 (Off) |
| 189 | + analogWrite(message.sensor, 0); |
| 190 | + // Save old value to eeprom if it'was not zero |
| 191 | + if ( gw.loadState(message.sensor) != 0 ) |
| 192 | + { |
| 193 | + gw.saveState(10*message.sensor, constrain(gw.loadState(message.sensor), 0, 100)); |
| 194 | + } |
| 195 | + // Save new value to eeprom |
| 196 | + gw.saveState(message.sensor, 0); |
| 197 | + // Write some debug info |
| 198 | + Serial.print("Incoming change for "); |
| 199 | + Serial.print(color[message.sensor]); |
| 200 | + Serial.print(": "); |
| 201 | + Serial.println("Off"); |
| 202 | + Serial.print("Store old value: "); |
| 203 | + Serial.print(gw.loadState(10*message.sensor)); |
| 204 | + Serial.println("%"); |
| 205 | + // Send value to controler |
| 206 | + Serial.println("Send value to controler"); |
| 207 | + gw.send(Status.setSensor(message.sensor).set(gw.loadState(message.sensor)),false); |
| 208 | + // Stop the show if it's running |
| 209 | + if (isShow==1){ rgbShowStop(message.sensor); } |
| 210 | + } |
| 211 | + } |
| 212 | + else if (message.type == V_DIMMER) |
| 213 | + { |
| 214 | + uint8_t incomingDimmerStatus = message.getByte(); |
| 215 | + // limits range of sensor values to between 0 and 100 |
| 216 | + incomingDimmerStatus = constrain(incomingDimmerStatus, 0, 100); |
| 217 | + // Change Dimmer level |
| 218 | + analogWrite(message.sensor, 255 * incomingDimmerStatus / 100); |
| 219 | + //Save value to eeprom |
| 220 | + gw.saveState(message.sensor, incomingDimmerStatus); |
| 221 | + // Write some debug info |
| 222 | + Serial.print("Incoming change for "); |
| 223 | + Serial.print(color[message.sensor]); |
| 224 | + Serial.print(": "); |
| 225 | + Serial.print(incomingDimmerStatus); |
| 226 | + Serial.println("%"); |
| 227 | + // Send value to controler |
| 228 | + Serial.println("Send value to controler"); |
| 229 | + gw.send(Status.setSensor(message.sensor).set(gw.loadState(message.sensor)),false); |
| 230 | + // Stop the show if it's running |
| 231 | + if (isShow==1){ rgbShowStop(message.sensor); } |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + void rgbShow() |
| 236 | + { |
| 237 | + time = millis(); |
| 238 | + redval = 128+250*cos(2*PI/300000*time); |
| 239 | + greenval = 128+250*cos(2*PI/300000*time-222); |
| 240 | + blueval = 128+250*cos(2*PI/300000*time-111); |
| 241 | + // limits range of sensor values to between 0 and 255 |
| 242 | + redval = constrain(redval, 0, 255); |
| 243 | + greenval = constrain(greenval, 0, 255); |
| 244 | + blueval = constrain(blueval, 0, 255); |
| 245 | + } |
| 246 | + |
| 247 | + void rgbShowOn() |
| 248 | + { |
| 249 | + // define show On |
| 250 | + isShow=1; |
| 251 | + // Save state |
| 252 | + gw.saveState(0, 1); |
| 253 | + // Write some debug info |
| 254 | + Serial.println("Show must go on"); |
| 255 | + } |
| 256 | + |
| 257 | + void rgbShowOff() |
| 258 | + { |
| 259 | + // define show Off |
| 260 | + isShow=0; |
| 261 | + // Save state |
| 262 | + gw.saveState(0, 0); |
| 263 | + // Save RGB value to eeprom |
| 264 | + gw.saveState(RED_PIN, 100 * redval / 255); |
| 265 | + gw.saveState(GREEN_PIN, 100 * greenval / 255); |
| 266 | + gw.saveState(BLUE_PIN, 100 * blueval / 255); |
| 267 | + // Write some debug info |
| 268 | + Serial.println("Stop the show"); |
| 269 | + // Send actual RGB value and state to controler and request ack back (true/false) |
| 270 | + Serial.println("Send eeprom value to controler"); |
| 271 | + gw.send( RedStatus.set(gw.loadState(RED_PIN)), false ); |
| 272 | + gw.send( GreenStatus.set(gw.loadState(GREEN_PIN)), false ); |
| 273 | + gw.send( BlueStatus.set(gw.loadState(BLUE_PIN)), false ); |
| 274 | + gw.send( rgbShowState.set(0), false); |
| 275 | + } |
| 276 | + |
| 277 | + void rgbShowStop(int sensor) |
| 278 | + { |
| 279 | + // define show Off |
| 280 | + isShow=0; |
| 281 | + // Save state |
| 282 | + gw.saveState(0, 0); |
| 283 | + // Write some debug info |
| 284 | + Serial.println("Stop the show"); |
| 285 | + // Send actual RGB value and state to controler and request ack back (true/false) |
| 286 | + Serial.println("Send eeprom value to controler"); |
| 287 | + if (sensor != RED_PIN) |
| 288 | + { |
| 289 | + gw.saveState(RED_PIN, 100 * redval / 255); |
| 290 | + gw.send( RedStatus.set(gw.loadState(RED_PIN)), false ); |
| 291 | + } |
| 292 | + if (sensor != GREEN_PIN) |
| 293 | + { |
| 294 | + gw.saveState(GREEN_PIN, 100 * greenval / 255); |
| 295 | + gw.send( GreenStatus.set(gw.loadState(GREEN_PIN)), false ); |
| 296 | + } |
| 297 | + if (sensor != BLUE_PIN) |
| 298 | + { |
| 299 | + gw.saveState(BLUE_PIN, 100 * blueval / 255); |
| 300 | + gw.send( BlueStatus.set(gw.loadState(BLUE_PIN)), false ); |
| 301 | + } |
| 302 | + gw.send( rgbShowState.set(0), false); |
| 303 | + } |
| 304 | + |
| 305 | + |
| 306 | + |
0 commit comments