-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_charlieplex.ino
More file actions
165 lines (145 loc) · 5.09 KB
/
test_charlieplex.ino
File metadata and controls
165 lines (145 loc) · 5.09 KB
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
/*
Control 12 LEDs with 4 pins
Multiplexing but with 'input state' to disconnect some pins
'Charlieplexing'
*/
int charlie_pins[] = {2, 3, 4, 5};
const int DISCONNECT = -1;
int pins[][11] = { {DISCONNECT, DISCONNECT, LOW, HIGH},
{DISCONNECT, DISCONNECT, HIGH, LOW},
{DISCONNECT, LOW, HIGH, DISCONNECT},
{DISCONNECT, HIGH, LOW, DISCONNECT},
{LOW, HIGH, DISCONNECT, DISCONNECT},
{HIGH, LOW, DISCONNECT, DISCONNECT},
{DISCONNECT, LOW, DISCONNECT, HIGH},
{DISCONNECT, HIGH, DISCONNECT, LOW},
{LOW, DISCONNECT, HIGH, DISCONNECT},
{HIGH, DISCONNECT, LOW, DISCONNECT},
{LOW, DISCONNECT, DISCONNECT, HIGH},
{HIGH, DISCONNECT, DISCONNECT, LOW} };
const int ONE[] = {DISCONNECT, DISCONNECT, LOW, HIGH};
const int TWO[] = {DISCONNECT, DISCONNECT, HIGH, LOW};
const int THREE[] = {DISCONNECT, LOW, HIGH, DISCONNECT};
const int FOUR[] = {DISCONNECT, HIGH, LOW, DISCONNECT};
const int FIVE[] = {LOW, HIGH, DISCONNECT, DISCONNECT};
const int SIX[] = {HIGH, LOW, DISCONNECT, DISCONNECT};
const int SEVEN[] = {DISCONNECT, LOW, DISCONNECT, HIGH};
const int EIGHT[] = {DISCONNECT, HIGH, DISCONNECT, LOW};
const int NINE[] = {LOW, DISCONNECT, HIGH, DISCONNECT};
const int TEN[] = {HIGH, DISCONNECT, LOW, DISCONNECT};
const int ELEVEN[] = {LOW, DISCONNECT, DISCONNECT, HIGH};
const int TWELVE[] = {HIGH, DISCONNECT, DISCONNECT, LOW};
// Shortcut for setting mode of and digital level of the pins
void set_pin( int pin, int value){
if (value == DISCONNECT){
pinMode(pin, INPUT);
digitalWrite(pin, LOW);
}
else if (value == LOW){
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
else if (value == HIGH ){
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
}
}
// loop through 1-12 and set pins accordingly.
// send current number to serial port
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Awaiting 1st communication"); // send an initial string
delay(300);
}
// the loop function runs over and over again forever
void loop() {
for (int x = 1; x < 13; x++){
Serial.print("Setting value: ");
Serial.println(x);
// essentially set pins as for current number x.
// maybe abbreviate with 2D array later
// set_pin( charlieplex[pin][x] )
switch(x){
case 1:
set_pin(charlie_pins[0], ONE[0]);
set_pin(charlie_pins[1], ONE[1]);
set_pin(charlie_pins[2], ONE[2]);
set_pin(charlie_pins[3], ONE[3]);
//set_pins( 1 );
break;
case 2:
set_pin(charlie_pins[0], TWO[0]);
set_pin(charlie_pins[1], TWO[1]);
set_pin(charlie_pins[2], TWO[2]);
set_pin(charlie_pins[3], TWO[3]);
break;
case 3:
set_pin(charlie_pins[0], THREE[0]);
set_pin(charlie_pins[1], THREE[1]);
set_pin(charlie_pins[2], THREE[2]);
set_pin(charlie_pins[3], THREE[3]);
break;
case 4:
set_pin(charlie_pins[0], FOUR[0]);
set_pin(charlie_pins[1], FOUR[1]);
set_pin(charlie_pins[2], FOUR[2]);
set_pin(charlie_pins[3], FOUR[3]);
break;
case 5:
set_pin(charlie_pins[0], FIVE[0]);
set_pin(charlie_pins[1], FIVE[1]);
set_pin(charlie_pins[2], FIVE[2]);
set_pin(charlie_pins[3], FIVE[3]);
break;
case 6:
set_pin(charlie_pins[0], SIX[0]);
set_pin(charlie_pins[1], SIX[1]);
set_pin(charlie_pins[2], SIX[2]);
set_pin(charlie_pins[3], SIX[3]);
break;
case 7:
set_pin(charlie_pins[0], SEVEN[0]);
set_pin(charlie_pins[1], SEVEN[1]);
set_pin(charlie_pins[2], SEVEN[2]);
set_pin(charlie_pins[3], SEVEN[3]);
break;
case 8:
set_pin(charlie_pins[0], EIGHT[0]);
set_pin(charlie_pins[1], EIGHT[1]);
set_pin(charlie_pins[2], EIGHT[2]);
set_pin(charlie_pins[3], EIGHT[3]);
break;
case 9:
set_pin(charlie_pins[0], NINE[0]);
set_pin(charlie_pins[1], NINE[1]);
set_pin(charlie_pins[2], NINE[2]);
set_pin(charlie_pins[3], NINE[3]);
break;
case 10:
set_pin(charlie_pins[0], TEN[0]);
set_pin(charlie_pins[1], TEN[1]);
set_pin(charlie_pins[2], TEN[2]);
set_pin(charlie_pins[3], TEN[3]);
break;
case 11:
set_pin(charlie_pins[0], ELEVEN[0]);
set_pin(charlie_pins[1], ELEVEN[1]);
set_pin(charlie_pins[2], ELEVEN[2]);
set_pin(charlie_pins[3], ELEVEN[3]);
break;
case 12:
set_pin(charlie_pins[0], TWELVE[0]);
set_pin(charlie_pins[1], TWELVE[1]);
set_pin(charlie_pins[2], TWELVE[2]);
set_pin(charlie_pins[3], TWELVE[3]);
break;
}
delay(3000); // 3 second delay
}
delay(10000);
}