Skip to content

Commit c21273e

Browse files
committed
Add example LedChain
This example works by default with 3 boards. Tested on HW
1 parent c41d06f commit c21273e

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

examples/LedChain/LedChain.ino

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#include "Modulino.h"
2+
3+
#define NUMBER_OF_BOARDS 3 //How many boards
4+
#define NUMBER_OF_LED_PER_BOARD 8 //How many leds per board.
5+
#define FIRST_ADDRESS 10 //First address of the chain. Address list should have no missing numbers
6+
#define LAST_ADDRESS 12 //Last address of the chain. Address list should have no missing numbers
7+
8+
//Define a new, custom color
9+
ModulinoColor YELLOW(255, 75, 0);
10+
11+
//Allocate memory for as many boards as desired
12+
ModulinoPixels* boards_chain = (ModulinoPixels*)malloc(sizeof(ModulinoPixels) * NUMBER_OF_BOARDS);
13+
14+
int i = 0; //Defined to count stuff
15+
16+
void setup() {
17+
Serial.begin(115200);
18+
Modulino.begin(); //Initialize library
19+
20+
//Inizialize each object of the boards with its own address
21+
for (i = 0; i < NUMBER_OF_BOARDS; i++) {
22+
boards_chain[i] = ModulinoPixels(FIRST_ADDRESS + i);
23+
boards_chain[i].begin();
24+
}
25+
//Set all leds at brigthness 0, shutting them off
26+
ledAllsame(boards_chain, NUMBER_OF_BOARDS, NUMBER_OF_LED_PER_BOARD, RED, 0);
27+
}
28+
29+
void loop() {
30+
//Switch on all leds with the same color at brightness 10
31+
ledAllsame(boards_chain, NUMBER_OF_BOARDS, NUMBER_OF_LED_PER_BOARD, YELLOW, 10);
32+
delay(200);
33+
ledAllsame(boards_chain, NUMBER_OF_BOARDS, NUMBER_OF_LED_PER_BOARD, WHITE, 10);
34+
delay(200);
35+
ledAllsame(boards_chain, NUMBER_OF_BOARDS, NUMBER_OF_LED_PER_BOARD, VIOLET, 10);
36+
delay(200);
37+
ledAllsame(boards_chain, NUMBER_OF_BOARDS, NUMBER_OF_LED_PER_BOARD, RED, 10);
38+
delay(200);
39+
ledAllsame(boards_chain, NUMBER_OF_BOARDS, NUMBER_OF_LED_PER_BOARD, GREEN, 10);
40+
delay(200);
41+
ledAllsame(boards_chain, NUMBER_OF_BOARDS, NUMBER_OF_LED_PER_BOARD, BLUE, 10);
42+
delay(200);
43+
44+
//From the last led to the first one, one by one, switch color to red and brightness to 100
45+
for (i = (NUMBER_OF_BOARDS * NUMBER_OF_LED_PER_BOARD - 1); i >= 0; i--) {
46+
ledOn(boards_chain, NUMBER_OF_BOARDS, NUMBER_OF_LED_PER_BOARD, RED, 100, i);
47+
delay(25);
48+
}
49+
50+
//From the first led to the last one, one by one, switch color to white and brightness to 10
51+
//shut off all the others so that one led is travelling the chain
52+
for (i = 0; i < (NUMBER_OF_BOARDS * NUMBER_OF_LED_PER_BOARD); i++) {
53+
ledOnOthersOff(boards_chain, NUMBER_OF_BOARDS, NUMBER_OF_LED_PER_BOARD, WHITE, 10, i);
54+
delay(25);
55+
}
56+
57+
//Breathe all the leds in blue color
58+
for (i = 0; i <= 100; i = i + 1) {
59+
ledAllsame(boards_chain, NUMBER_OF_BOARDS, NUMBER_OF_LED_PER_BOARD, BLUE, i);
60+
delay(10);
61+
}
62+
for (i = 100; i >= 0; i = i - 1) {
63+
ledAllsame(boards_chain, NUMBER_OF_BOARDS, NUMBER_OF_LED_PER_BOARD, BLUE, i);
64+
delay(10);
65+
}
66+
}
67+
68+
//Switch ON all LEDs on all boards
69+
void ledAllsame(ModulinoPixels* board_array, int number_of_boards, int number_of_leds, ModulinoColor rgb, int brightness) {
70+
int i_board = 0; //Local variable to count each board
71+
int i_led = 0; //Local variable to count each led on each board
72+
//For each board
73+
for (int i_board = 0; i_board < number_of_boards; i_board++) {
74+
//For each led on the current board
75+
for (i_led = 0; i_led < number_of_leds; i_led++) {
76+
//Set the current led on the current board to desired color and brightness
77+
board_array[i_board].set(i_led, rgb, brightness);
78+
}
79+
//Update the current board status
80+
board_array[i_board].show();
81+
}
82+
}
83+
84+
//Switch ON just the desired LED, do nothing to the others
85+
//Considers the chain of boards as a single, long board
86+
void ledOn(ModulinoPixels* board_array, int number_of_boards,
87+
int number_of_leds, ModulinoColor rgb, int brightness, int ledOn) {
88+
int i_board = 0; //Local variable to count each board
89+
int i_led = 0; //Local variable to count each led on each board
90+
int chain_led = 0; //Local variable to check if the desired led is reached
91+
//For each board
92+
for (int i_board = 0; i_board < number_of_boards; i_board++) {
93+
//For each led on the current board
94+
for (i_led = 0; i_led < number_of_leds; i_led++) {
95+
//Set the current led on the current board to desired color and brightness
96+
if (chain_led == ledOn) {
97+
board_array[i_board].set(i_led, rgb, brightness);
98+
}
99+
chain_led++;
100+
}
101+
//Update the current board status
102+
board_array[i_board].show();
103+
}
104+
}
105+
106+
//Switch ON just the desired LED, shut off the others
107+
//Considers the chain of boards as a single, long board
108+
void ledOnOthersOff(ModulinoPixels* board_array, int number_of_boards,
109+
int number_of_leds, ModulinoColor rgb, int brightness, int ledOn) {
110+
int i_board = 0; //Local variable to count each board
111+
int i_led = 0; //Local variable to count each led on each board
112+
int chain_led = 0; //Local variable to check if the desired led is reached
113+
//For each board
114+
for (int i_board = 0; i_board < number_of_boards; i_board++) {
115+
//For each led on the current board
116+
for (i_led = 0; i_led < number_of_leds; i_led++) {
117+
//Set the current led on the current board to desired color and brightness
118+
if (chain_led == ledOn) {
119+
board_array[i_board].set(i_led, rgb, brightness);
120+
} else {
121+
//Shut off the other leds
122+
board_array[i_board].set(i_led, RED, 0);
123+
}
124+
chain_led++;
125+
}
126+
//Update the current board status
127+
board_array[i_board].show();
128+
}
129+
}

0 commit comments

Comments
 (0)