-
Notifications
You must be signed in to change notification settings - Fork 1
/
2d-xmas-tree.ino
187 lines (140 loc) · 5.3 KB
/
2d-xmas-tree.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
2D XMAS Tree Code
charlieplexing christmas decoration
5 Pins with 20 LED´s
ATtiny25, 1MHZ Internal Clock, no BOD
Hosted on Github:
https://github.com/designer2k2/2d-xmas-tree
Made by:
https://www.designer2k2.at
MIT License
Copyright (c) [2018] [Stephan Martin]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <avr/pgmspace.h>
//Enter the light pattern here:
//use the online generator: https://designer2k2.github.io/2d-xmas-tree/
//dont forget to update the number of rows if changed
static const bool state_table[14][21] PROGMEM = {
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,1,0,1,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,0,1,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,0,0},
{1,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,1,1,0,0,0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0},
{0,1,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,0,0},
{0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0}
};
#define NumRows 14 //default is 14 (and about the space limit)
//the delay in ms between the patterns:
#define pattern_delay 500
//intro time (how long the random change happens) in ms:
#define introtime 10000
//time a led is on in us:
//increase this to have the leds "flicker"
#define ondelay 1000
//-------------------------------------------------------------------------------------
//below is the code to make this all happen, modify to make even fancier effects as you wish!
//This holds the pin configuration to make the 20 led charlieplexing,
//only 1 in PROGMEM, as the FLASH is full, but both are too large for RAM.
int pinvcc[] = {0,1,1,2,2,3,3,4,0,2,1,3,2,4,0,3,1,4,0,4};
const PROGMEM int pingnd[] = {1,0,2,1,3,2,4,3,2,0,3,1,4,2,3,0,4,1,4,0};
//placeholder for all current leds:
bool ledstate[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
unsigned long lasttime, lastintrotime;
int iState;
void setup() {
//Need to define the Pins as input (defaults)
tristate();
// disable ADC (power saving):
ADCSRA = 0;
//init the time with 0
lasttime = 0;
lastintrotime = introtime;
//start with the first pattern
iState = 0;
}
void loop(){
// put your main code here, to run repeatedly:
// every 100ms change a random led to a random state
if(millis()<lastintrotime){
if(millis()>(lasttime+10)){
int ledtoset = random(20);
bool ledbool = random(2);
ledstate[ledtoset] = ledbool;
lasttime = millis();
}
}else{
//loop trough the states in state_table
if(millis()>(lasttime+pattern_delay)){
for (int i=0; i<20; i++) {
int state = pgm_read_word(&state_table[iState][i]);
//PROGMEM with bool gives sometimes strange results, this fixes it:
if (state==256){
state = 0;
}
ledstate[i] = state;
}
iState = iState + 1;
if(iState>=NumRows){
iState = 0;
lastintrotime = millis() + introtime;
}
lasttime = millis();
}
}
showleds();
}
//-------------------------------------------------------------------------------------
void showleds(){
// this cycles trough all the leds in the array and show them.
for (int i=0; i<20; i++) {
bool state = ledstate[i];
showled(i,state);
}
}
void showled(int led, bool state){
//this outputs the led state
if(state==true){
//Lookup the pin for high and low:
int pvcc = pinvcc[led];
int pgnd = pgm_read_word(&pingnd[led]);
//set the pinmode for it:
pinMode(pvcc,OUTPUT);
pinMode(pgnd,OUTPUT);
//write the status:
digitalWrite(pvcc,HIGH);
digitalWrite(pgnd,LOW);
//wait a short time to let the LED shine
delayMicroseconds(ondelay);
//ensure its all turned off properly for the next state
tristate();
}
}
void tristate(){
//This tristates all pins
for (int i=0; i<5; i++) {
pinMode(i,INPUT); //Input state
digitalWrite(i,LOW);//ensure pullups are off.
}
}