source: pyramid/firmware/RGB_HEX/RGB_HEX.ino

Last change on this file was a96666e, checked in by Steve Castellotti <sc@…>, 7 years ago
  • production release
  • Property mode set to 100644
File size: 5.3 KB
Line 
1//  Puzzlebox RGB HEX board Firmware
2//  This firmware is developed for Puzzlebox's RGB HEX
3//  Choose Duemilanove w/ATmega328 when program this RGB HEX.
4//  This code ontrols all lights on RGB HEX, and can be updated
5//  via I2C/TWI communication.
6//  Author: Azureviolin <www.azureviolin.com>
7//  Copyright (C) 2013 Puzzlebox Productions LLC
8//
9//  This program is free software: you can redistribute it and/or modify
10//  it under the terms of the GNU General Public License as published by
11//  the Free Software Foundation, either version 2 of the License, or
12//  (at your option) any later version.
13// 
14//  This program is distributed in the hope that it will be useful,
15//  but WITHOUT ANY WARRANTY; without even the implied warranty of
16//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17//  GNU General Public License for more details.
18// 
19//  You should have received a copy of the GNU General Public License
20//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
22
23#include <Wire.h>
24#define DEBUG 1
25
26int b1 = 3;
27int r1 = 5;
28int g1 = 6;
29int b2 = 9;
30int r2 = 10;
31int g2 = 11;
32
33int d1d2 = 2;
34int d3d4 = 4;
35int d5d6 = 7;
36int d7d8 = 8;
37int d9d10 = 12;
38int d11d12 = 13;
39
40int t=1;
41int displayControl=1;
42int availableBytes=0;
43
44byte RGB[12][3]={
45    {255,255,255},
46    {255,255,255},
47    {255,255,255},
48    {255,255,255},
49    {255,255,255},
50    {255,255,255},
51    {255,255,255},
52    {255,255,255},
53    {255,255,255},
54    {255,255,255},
55    {255,255,255},
56    {255,255,255},
57  };
58
59
60void setup()
61{
62  #if DEBUG
63  Serial.begin(115200);
64  #endif
65  Wire.begin(1);                // join i2c bus with address #1
66  Wire.onReceive(receiveEvent); // register event
67  initPins();
68
69}
70
71void loop()
72{
73  scan();//scan the display
74}
75
76// function that executes whenever data is received from master
77// this function is registered as an event, see setup()
78void receiveEvent(int howMany)
79{
80  int beginPos=Wire.read();
81  availableBytes=Wire.available();
82  for(int i=beginPos; i<beginPos+availableBytes; i++)
83    RGB[i/3][i%3]=Wire.read();
84}
85
86#if DEBUG
87long loopStartTime;
88#endif
89
90void scan()
91{
92  for (int i = 0; i < displayControl; i++) {
93
94//    #if DEBUG   
95//    loopStartTime=micros();
96//    #endif
97   
98    digitalWrite(d1d2,HIGH);
99    analogWrite(r1,RGB[0][0]);
100    analogWrite(g1,RGB[0][1]);
101    analogWrite(b1,RGB[0][2]);
102    analogWrite(r2,RGB[1][0]);
103    analogWrite(g2,RGB[1][1]);
104    analogWrite(b2,RGB[1][2]);
105    analogWrite(r1,LOW);
106    analogWrite(g1,LOW);
107    analogWrite(b1,LOW);
108    analogWrite(r2,LOW);
109    analogWrite(g2,LOW);
110    analogWrite(b2,LOW);
111    digitalWrite(d1d2,LOW);
112   
113//    #if DEBUG
114//    Serial.print("D12:");
115//    Serial.println(micros()-loopStartTime);
116//    #endif
117   
118    digitalWrite(d3d4,HIGH);
119    analogWrite(r1,RGB[2][0]);
120    analogWrite(g1,RGB[2][1]);
121    analogWrite(b1,RGB[2][2]);
122    analogWrite(r2,RGB[3][0]);
123    analogWrite(g2,RGB[3][1]);
124    analogWrite(b2,RGB[3][2]);
125    analogWrite(r1,LOW);
126    analogWrite(g1,LOW);
127    analogWrite(b1,LOW);
128    analogWrite(r2,LOW);
129    analogWrite(g2,LOW);
130    analogWrite(b2,LOW);
131    digitalWrite(d3d4,LOW);
132   
133    digitalWrite(d5d6,HIGH);
134    analogWrite(r1,RGB[4][0]);
135    analogWrite(g1,RGB[4][1]);
136    analogWrite(b1,RGB[4][2]);
137    analogWrite(r2,RGB[5][0]);
138    analogWrite(g2,RGB[5][1]);
139    analogWrite(b2,RGB[5][2]);
140    analogWrite(r1,LOW);
141    analogWrite(g1,LOW);
142    analogWrite(b1,LOW);
143    analogWrite(r2,LOW);
144    analogWrite(g2,LOW);
145    analogWrite(b2,LOW);
146    digitalWrite(d5d6,LOW);
147   
148    digitalWrite(d7d8,HIGH);
149    analogWrite(r1,RGB[6][0]);
150    analogWrite(g1,RGB[6][1]);
151    analogWrite(b1,RGB[6][2]);
152    analogWrite(r2,RGB[7][0]);
153    analogWrite(g2,RGB[7][1]);
154    analogWrite(b2,RGB[7][2]);
155    analogWrite(r1,LOW);
156    analogWrite(g1,LOW);
157    analogWrite(b1,LOW);
158    analogWrite(r2,LOW);
159    analogWrite(g2,LOW);
160    analogWrite(b2,LOW);
161    digitalWrite(d7d8,LOW);
162   
163    digitalWrite(d9d10,HIGH);
164    analogWrite(r1,RGB[8][0]);
165    analogWrite(g1,RGB[8][1]);
166    analogWrite(b1,RGB[8][2]);
167    analogWrite(r2,RGB[9][0]);
168    analogWrite(g2,RGB[9][1]);
169    analogWrite(b2,RGB[9][2]);
170    analogWrite(r1,LOW);
171    analogWrite(g1,LOW);
172    analogWrite(b1,LOW);
173    analogWrite(r2,LOW);
174    analogWrite(g2,LOW);
175    analogWrite(b2,LOW);
176    digitalWrite(d9d10,LOW);
177   
178    digitalWrite(d11d12,HIGH);
179    analogWrite(r1,RGB[10][0]);
180    analogWrite(g1,RGB[10][1]);
181    analogWrite(b1,RGB[10][2]);
182    analogWrite(r2,RGB[11][0]);
183    analogWrite(g2,RGB[11][1]);
184    analogWrite(b2,RGB[11][2]);
185    analogWrite(r1,LOW);
186    analogWrite(g1,LOW);
187    analogWrite(b1,LOW);
188    analogWrite(r2,LOW);
189    analogWrite(g2,LOW);
190    analogWrite(b2,LOW);
191    digitalWrite(d11d12,LOW);
192   
193//    #if DEBUG
194////    Serial.print("LT:");
195//    Serial.println(micros()-loopStartTime);
196//    #endif
197    //delay(t);
198  }
199}
200
201void initPins()
202
203  pinMode(b1,OUTPUT);
204  pinMode(r1,OUTPUT);
205  pinMode(g1,OUTPUT);
206  pinMode(b2,OUTPUT);
207  pinMode(r2,OUTPUT);
208  pinMode(g2,OUTPUT);
209 
210  pinMode(d1d2,INPUT);
211  pinMode(d3d4,INPUT);
212  pinMode(d5d6,INPUT);
213  pinMode(d7d8,INPUT);
214  pinMode(d9d10,INPUT);
215  pinMode(d11d12,INPUT);
216 
217  analogWrite(b1,LOW);
218  analogWrite(r1,LOW);
219  analogWrite(g1,LOW);
220  analogWrite(b2,LOW);
221  analogWrite(r2,LOW);
222  analogWrite(g2,LOW);
223 
224  analogWrite(d1d2,LOW);
225  analogWrite(d3d4,LOW);
226  analogWrite(d5d6,LOW);
227  analogWrite(d7d8,LOW);
228  analogWrite(d9d10,LOW);
229  analogWrite(d11d12,LOW);
230}
Note: See TracBrowser for help on using the repository browser.