1 | #include <ShiftRegister74HC595.h>
|
2 |
|
3 | //config variables
|
4 | #define NUM_LED_COLUMNS (4)
|
5 | #define NUM_LED_ROWS (4)
|
6 | #define NUM_COLORS (1)
|
7 |
|
8 | // Global variables
|
9 | static bool LED_buffer[NUM_LED_COLUMNS][NUM_LED_ROWS];
|
10 | static int32_t next_advance;
|
11 | static uint8_t led_index;
|
12 |
|
13 | static const uint8_t ledcolumnpins[NUM_LED_COLUMNS] = {4,2,23,8};;
|
14 | static const uint8_t colorpins[NUM_LED_ROWS] = {21,17,12,7};
|
15 |
|
16 |
|
17 | ShiftRegister74HC595 sr (3, 7, 8, 6);
|
18 |
|
19 |
|
20 | static void setuppins()
|
21 | {
|
22 | uint8_t i;
|
23 |
|
24 | // initialize all of the output pins
|
25 |
|
26 | // LED column lines
|
27 | for(i = 0; i < NUM_LED_COLUMNS; i++)
|
28 | {
|
29 | // with nothing selected by default
|
30 | sr.set(ledcolumnpins[i], HIGH);
|
31 | }
|
32 |
|
33 | // LED row lines
|
34 | for(i = 0; i < NUM_LED_ROWS; i++)
|
35 | {
|
36 | // with nothing driven by default
|
37 | sr.set(colorpins[i], LOW);
|
38 | }
|
39 |
|
40 | }
|
41 |
|
42 | static void scan()
|
43 | {
|
44 | static uint8_t current = 0;
|
45 | uint8_t val;
|
46 | uint8_t i, j;
|
47 |
|
48 | // Select a column
|
49 | sr.set(ledcolumnpins[current], LOW);
|
50 |
|
51 | // write the row pins
|
52 | for(i = 0; i < NUM_LED_ROWS; i++)
|
53 | {
|
54 | if(LED_buffer[current][i])
|
55 | {
|
56 | sr.set(colorpins[i], HIGH);
|
57 | }
|
58 | }
|
59 |
|
60 | delay(1);
|
61 |
|
62 | sr.set(ledcolumnpins[current], HIGH);
|
63 |
|
64 | for(i = 0; i < NUM_LED_ROWS; i++)
|
65 | {
|
66 | sr.set(colorpins[i], LOW);
|
67 | }
|
68 |
|
69 | // Move on to the next column
|
70 | current++;
|
71 | if (current >= NUM_LED_COLUMNS)
|
72 | {
|
73 | current = 0;
|
74 | }
|
75 |
|
76 | }
|
77 |
|
78 | void setup()
|
79 | {
|
80 | // put your setup code here, to run once:
|
81 | Serial.begin(115200);
|
82 | Serial.print("Starting Setup...");
|
83 |
|
84 | // setup hardware
|
85 | setuppins();
|
86 |
|
87 | // init global variables
|
88 | next_advance = millis() + 1000;
|
89 | led_index = 0;
|
90 |
|
91 | // Initialize the LED display array
|
92 | for(uint8_t i = 0; i < NUM_LED_COLUMNS; i++)
|
93 | {
|
94 | for(uint8_t j = 0; j < NUM_LED_ROWS; j++)
|
95 | {
|
96 | LED_buffer[i][j] = false;
|
97 | }
|
98 | }
|
99 | // Set the first LED in the buffer on
|
100 | LED_buffer[0][0] = true;
|
101 |
|
102 | Serial.println("Setup Complete.");
|
103 |
|
104 | }
|
105 |
|
106 | void loop()
|
107 | {
|
108 | // put your main code here, to run repeatedly:
|
109 |
|
110 | scan();
|
111 |
|
112 | if(millis() >= next_advance)
|
113 | {
|
114 | next_advance = millis()+1000;
|
115 |
|
116 | // Vorherige LED Aus
|
117 | LED_buffer[led_index/NUM_LED_COLUMNS][led_index%NUM_LED_COLUMNS] = false;
|
118 |
|
119 | // Led 0-15 auswählen
|
120 | led_index++;
|
121 |
|
122 | // Nicht über die Maxiimale Array Size (16m in dem Fall)
|
123 | led_index %= (NUM_LED_COLUMNS * NUM_LED_ROWS);
|
124 |
|
125 | // Neue Led ausrechnen und anzeigen
|
126 | LED_buffer[led_index/NUM_LED_COLUMNS][led_index%NUM_LED_COLUMNS] = true;
|
127 | }
|
128 | }
|