Skip to content

Commit c11dd37

Browse files
committed
Merge pull request #166 from vil1driver/master
Create RGB-3D.ino
2 parents 9d7d0cc + 26f6a04 commit c11dd37

File tree

1 file changed

+305
-0
lines changed

1 file changed

+305
-0
lines changed
Lines changed: 305 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,305 @@
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+
// Load mysensors library
33+
#include <MySensor.h>
34+
// Load Serial Peripheral Interface library
35+
#include <SPI.h>
36+
37+
// Arduino pin attached to MOSFET Gate pin
38+
#define RED_PIN 3
39+
#define GREEN_PIN 5
40+
#define BLUE_PIN 6
41+
42+
MySensor gw;
43+
44+
// Define message name and type to send sensor info
45+
MyMessage RedStatus(RED_PIN, V_DIMMER);
46+
MyMessage GreenStatus(GREEN_PIN, V_DIMMER);
47+
MyMessage BlueStatus(BLUE_PIN, V_DIMMER);
48+
MyMessage Status(1, V_DIMMER);
49+
MyMessage rgbShowState(0, V_LIGHT);
50+
51+
// Serial.print translate sensor id to sensor name
52+
char color[][6] = {"","","","RED","","GREEN","BLUE"};
53+
54+
// Vars for rgbShow function
55+
int redval = 0;
56+
int greenval = 0;
57+
int blueval = 0;
58+
long time=0;
59+
int isShow;
60+
61+
void setup()
62+
{
63+
// Initializes the sensor node (Callback function for incoming messages, node id, is repeater)
64+
gw.begin(incomingMessage, 31, true);
65+
66+
// Present sketch (name, version)
67+
gw.sendSketchInfo(SN, SV);
68+
69+
// Register sensors (id, type, description, ack back)
70+
gw.present(RED_PIN, S_DIMMER, "present RED light", false);
71+
gw.present(GREEN_PIN, S_DIMMER, "present GREEN light", false);
72+
gw.present(BLUE_PIN, S_DIMMER, "present BLUE light", false);
73+
gw.present(0, S_LIGHT, "present Show button", false);
74+
75+
// Define pin mode (pin number, type)
76+
pinMode(RED_PIN, OUTPUT);
77+
pinMode(GREEN_PIN, OUTPUT);
78+
pinMode(BLUE_PIN, OUTPUT);
79+
80+
// Correct saved RGB value for first start
81+
gw.saveState(RED_PIN, constrain(gw.loadState(RED_PIN), 0, 100));
82+
gw.saveState(GREEN_PIN, constrain(gw.loadState(GREEN_PIN), 0, 100));
83+
gw.saveState(BLUE_PIN, constrain(gw.loadState(BLUE_PIN), 0, 100));
84+
85+
// Get value from eeprom and write to output
86+
analogWrite(RED_PIN, 255 * gw.loadState(RED_PIN) / 100);
87+
analogWrite(GREEN_PIN, 255 * gw.loadState(GREEN_PIN) / 100);
88+
analogWrite(BLUE_PIN, 255 * gw.loadState(BLUE_PIN) / 100);
89+
90+
// Write some debug info
91+
Serial.print("Load from eeprom RED: ");
92+
Serial.print(gw.loadState(RED_PIN));
93+
Serial.println("%");
94+
Serial.print("Load from eeprom GREEN: ");
95+
Serial.print(gw.loadState(GREEN_PIN));
96+
Serial.println("%");
97+
Serial.print("Load from eeprom BLUE: ");
98+
Serial.print(gw.loadState(BLUE_PIN));
99+
Serial.println("%");
100+
101+
// Send RGB value to controler (request ack back: true/false)
102+
Serial.println("Send eeprom value to controler");
103+
gw.send( RedStatus.set(gw.loadState(RED_PIN)), false );
104+
gw.send( GreenStatus.set(gw.loadState(GREEN_PIN)), false );
105+
gw.send( BlueStatus.set(gw.loadState(BLUE_PIN)), false );
106+
107+
// Correct RGB show state for first start and load it (set to 'On' at first start)
108+
gw.saveState(0, constrain(gw.loadState(0), 0, 1));
109+
isShow=gw.loadState(0);
110+
111+
// Send RGB show state to controler (request ack back: true/false)
112+
gw.send( rgbShowState.set(isShow), false);
113+
114+
if (isShow==1){Serial.println("RGB show running..."); }
115+
Serial.println("Ready to receive messages...");
116+
}
117+
118+
void loop()
119+
{
120+
// Process incoming messages (like config and light state from controller)
121+
gw.process();
122+
123+
// Run RGB show if is set
124+
if (isShow==1)
125+
{
126+
rgbShow();
127+
analogWrite(RED_PIN, redval);
128+
analogWrite(GREEN_PIN, greenval);
129+
analogWrite(BLUE_PIN, blueval);
130+
}
131+
}
132+
133+
134+
void incomingMessage(const MyMessage &message)
135+
{
136+
if (message.isAck())
137+
{
138+
Serial.println("Got ack from gateway");
139+
}
140+
if (message.type == V_LIGHT)
141+
{
142+
// Incoming on/off command sent from controller ("1" or "0")
143+
int lightState = message.getString()[0] == '1';
144+
145+
// if receive RGB Show On commands, start the show
146+
if (message.sensor==0 && lightState==1){ rgbShowOn(); }
147+
// if receive RGB Show Off commands, stop the show
148+
else if (message.sensor==0 && lightState==0){ rgbShowOff(); }
149+
150+
// if receive RGB switch On command
151+
else if (lightState==1)
152+
{
153+
// Write some debug info
154+
Serial.print("Incoming change for ");
155+
Serial.print(color[message.sensor]);
156+
Serial.println(": On");
157+
Serial.print("Load from eeprom: ");
158+
159+
if ( gw.loadState(message.sensor) == 0)
160+
{
161+
// Pick up last saved dimmer level from the eeprom
162+
analogWrite(message.sensor, 255 * gw.loadState(10*message.sensor) / 100);
163+
// Save loaded value to current
164+
gw.saveState(message.sensor, gw.loadState(10*message.sensor));
165+
Serial.print(gw.loadState(10*message.sensor));
166+
Serial.println("%");
167+
// Send value to controler
168+
Serial.println("Send value to controler");
169+
gw.send(Status.setSensor(message.sensor).set(gw.loadState(10*message.sensor)),false);
170+
}
171+
else
172+
{
173+
// Pick up last saved dimmer level from the eeprom
174+
analogWrite(message.sensor, 255 * gw.loadState(message.sensor) / 100);
175+
Serial.print(gw.loadState(message.sensor));
176+
Serial.println("%");
177+
// Send value to controler
178+
Serial.println("Send value to controler");
179+
gw.send(Status.setSensor(message.sensor).set(gw.loadState(message.sensor)),false);
180+
}
181+
// Stop the show if it's running
182+
if (isShow==1){ rgbShowStop(message.sensor); }
183+
}
184+
// if recieve switch Off command
185+
else if (lightState==0)
186+
{
187+
// Write output to 0 (Off)
188+
analogWrite(message.sensor, 0);
189+
// Save old value to eeprom if it'was not zero
190+
if ( gw.loadState(message.sensor) != 0 )
191+
{
192+
gw.saveState(10*message.sensor, constrain(gw.loadState(message.sensor), 0, 100));
193+
}
194+
// Save new value to eeprom
195+
gw.saveState(message.sensor, 0);
196+
// Write some debug info
197+
Serial.print("Incoming change for ");
198+
Serial.print(color[message.sensor]);
199+
Serial.print(": ");
200+
Serial.println("Off");
201+
Serial.print("Store old value: ");
202+
Serial.print(gw.loadState(10*message.sensor));
203+
Serial.println("%");
204+
// Send value to controler
205+
Serial.println("Send value to controler");
206+
gw.send(Status.setSensor(message.sensor).set(gw.loadState(message.sensor)),false);
207+
// Stop the show if it's running
208+
if (isShow==1){ rgbShowStop(message.sensor); }
209+
}
210+
}
211+
else if (message.type == V_DIMMER)
212+
{
213+
uint8_t incomingDimmerStatus = message.getByte();
214+
// limits range of sensor values to between 0 and 100
215+
incomingDimmerStatus = constrain(incomingDimmerStatus, 0, 100);
216+
// Change Dimmer level
217+
analogWrite(message.sensor, 255 * incomingDimmerStatus / 100);
218+
//Save value to eeprom
219+
gw.saveState(message.sensor, incomingDimmerStatus);
220+
// Write some debug info
221+
Serial.print("Incoming change for ");
222+
Serial.print(color[message.sensor]);
223+
Serial.print(": ");
224+
Serial.print(incomingDimmerStatus);
225+
Serial.println("%");
226+
// Send value to controler
227+
Serial.println("Send value to controler");
228+
gw.send(Status.setSensor(message.sensor).set(gw.loadState(message.sensor)),false);
229+
// Stop the show if it's running
230+
if (isShow==1){ rgbShowStop(message.sensor); }
231+
}
232+
}
233+
234+
void rgbShow()
235+
{
236+
time = millis();
237+
redval = 128+250*cos(2*PI/300000*time);
238+
greenval = 128+250*cos(2*PI/300000*time-222);
239+
blueval = 128+250*cos(2*PI/300000*time-111);
240+
// limits range of sensor values to between 0 and 255
241+
redval = constrain(redval, 0, 255);
242+
greenval = constrain(greenval, 0, 255);
243+
blueval = constrain(blueval, 0, 255);
244+
}
245+
246+
void rgbShowOn()
247+
{
248+
// define show On
249+
isShow=1;
250+
// Save state
251+
gw.saveState(0, 1);
252+
// Write some debug info
253+
Serial.println("Show must go on");
254+
}
255+
256+
void rgbShowOff()
257+
{
258+
// define show Off
259+
isShow=0;
260+
// Save state
261+
gw.saveState(0, 0);
262+
// Save RGB value to eeprom
263+
gw.saveState(RED_PIN, 100 * redval / 255);
264+
gw.saveState(GREEN_PIN, 100 * greenval / 255);
265+
gw.saveState(BLUE_PIN, 100 * blueval / 255);
266+
// Write some debug info
267+
Serial.println("Stop the show");
268+
// Send actual RGB value and state to controler and request ack back (true/false)
269+
Serial.println("Send eeprom value to controler");
270+
gw.send( RedStatus.set(gw.loadState(RED_PIN)), false );
271+
gw.send( GreenStatus.set(gw.loadState(GREEN_PIN)), false );
272+
gw.send( BlueStatus.set(gw.loadState(BLUE_PIN)), false );
273+
gw.send( rgbShowState.set(0), false);
274+
}
275+
276+
void rgbShowStop(int sensor)
277+
{
278+
// define show Off
279+
isShow=0;
280+
// Save state
281+
gw.saveState(0, 0);
282+
// Write some debug info
283+
Serial.println("Stop the show");
284+
// Send actual RGB value and state to controler and request ack back (true/false)
285+
Serial.println("Send eeprom value to controler");
286+
if (sensor != RED_PIN)
287+
{
288+
gw.saveState(RED_PIN, 100 * redval / 255);
289+
gw.send( RedStatus.set(gw.loadState(RED_PIN)), false );
290+
}
291+
if (sensor != GREEN_PIN)
292+
{
293+
gw.saveState(GREEN_PIN, 100 * greenval / 255);
294+
gw.send( GreenStatus.set(gw.loadState(GREEN_PIN)), false );
295+
}
296+
if (sensor != BLUE_PIN)
297+
{
298+
gw.saveState(BLUE_PIN, 100 * blueval / 255);
299+
gw.send( BlueStatus.set(gw.loadState(BLUE_PIN)), false );
300+
}
301+
gw.send( rgbShowState.set(0), false);
302+
}
303+
304+
305+

0 commit comments

Comments
 (0)