-
Notifications
You must be signed in to change notification settings - Fork 7
/
DMX_LED_Strips.ino
173 lines (133 loc) · 4.74 KB
/
DMX_LED_Strips.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
DMX_LED_Strips
Copyright (c) 2015, Matthew Tong
https://github.com/mtongnz/DMX_LED_Strips
http://www.instructables.com/id/DMX-LED-Strips/
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.gnu.org/licenses/
This sketch takes the DMX input and controls 5 RGB LED strips.
Channel allocation is red, green, blue, strobe for each output (20 channels total).
Strobe: 0-19 off
20 - 189 slow - fast
190 - 205 slow random
206 - 219 fast random
220 - 255 off
Libraries Used:
ShiftPWM by Elco Jacobs. www.elcojacobs.com
DMXSerial by Mat Hertel. www.mathertel.de
*/
// ShiftPWM settings
const int ShiftPWM_latchPin=8;
const bool ShiftPWM_invertOutputs = false;
const bool ShiftPWM_balanceLoad = true;
#include <DMXSerial.h>
#include <ShiftPWM.h>
// ShiftPWM settings
unsigned char maxBrightness = 255;
unsigned char pwmFrequency = 125;
unsigned int numRegisters = 2;
unsigned int numOutputs = numRegisters*8;
// Status LED pin
const int statusLEDPin = 5;
// Output settings
const int numRGBS = 5;
const int dmxNumChan = numRGBS * 4;
// DMX address.
const int dmxStartAddr = 1; // unit 1 & 2 = 61, unit 3 & 4 = 81
// Counter variables
int strobeCount[numRGBS];
int randStrobeCount[numRGBS];
int timerCount;
void setup(){
int x;
// Random Seed
randomSeed(analogRead(0));
// Setup DMX receiver
DMXSerial.init(DMXReceiver);
// Setup PWM output
ShiftPWM.SetAmountOfRegisters(numRegisters);
ShiftPWM.Start(pwmFrequency,maxBrightness);
// set all channels to 0 default
ShiftPWM.SetAll(0);
// Setup status LED
pinMode(statusLEDPin, OUTPUT);
digitalWrite(statusLEDPin, 1);
// Set counters to zero
for (x = 0; x < numRGBS; x++) {
strobeCount[x] = 0;
randStrobeCount[x] = random(5, 200);
}
timerCount = 0;
//set timer0 interrupt at 200Hz
cli(); // disable interupts
TCCR0A = 0; // set entire TCCR0A register to 0
TCCR0B = 0; // same for TCCR0B
TCNT0 = 0; // initialize counter value to 0
OCR0A = 78; // set compare match register for 200hz increments
TCCR0A |= (1 << WGM01); // turn on CTC mode
TCCR0B |= (1 << CS02) | (1 << CS00); // Set CS02 and CS00 bits for 1024 prescaler
TIMSK0 |= (1 << OCIE0A); // enable timer compare interrupt
sei(); // enable interupts
}
// Timer0 interupt handler. Increments all the counters.
ISR(TIMER0_COMPA_vect){
int x;
for (x = 0; x < numRGBS; x++)
strobeCount[x]++;
timerCount++;
}
void loop() {
int x, y, s;
if (timerCount >= 100) {
// Blink status LED if DMX receiving
if (DMXSerial.noDataSince() < 1000) {
digitalWrite(statusLEDPin, digitalRead(statusLEDPin) ^ 1);
// Turn status LED red if no DMX received
}else
digitalWrite(statusLEDPin, 1);
timerCount = 0;
}
// Turn off all outputs if we haven't received DMX in 10 seconds
if (DMXSerial.noDataSince() > 10000)
ShiftPWM.SetAll(0);
else {
// loop through each of the 'y' RGB leds
for (y = 0; y < numRGBS; y++) {
s = DMXSerial.read(dmxStartAddr + (y * 4));
// strobe mode
if (s > 20 && s <= 220) {
int t;
// random strobe
if (s > 190) {
t = (40 - (s - 190)) * 10;
s = randStrobeCount[y];
}
// strobe on
if (strobeCount[y] >= ( (192 - s) + 5) && strobeCount[y] < 500) {
for (x = 0; x < 3; x++)
ShiftPWM.SetOne((y*3) + x, DMXSerial.read(dmxStartAddr + (y * 4) + x + 1));
strobeCount[y] = 500;
TCNT0 = 0; //clear timer
}
// strobe off
if (strobeCount[y] >= 501 ) {
for (x = 0; x < 3; x++)
ShiftPWM.SetOne((y*3) + x, 0);
strobeCount[y] = 0;
randStrobeCount[y] = random(1, t);
}
// non strobe mode
} else {
strobeCount[y] = 0;
// Get DMX values and set outputs
for (x = 0; x < 3; x++)
ShiftPWM.SetOne((y*3) + x, DMXSerial.read(dmxStartAddr + (y * 4) + x + 1));
}
}
}
}