Hello
I'm trying to program a LED-cube(3x3x3) to light every LED one by one and when one layer has been lit, it stays that way and continues on to the next layer. I have tried with millis() but doesn't get it to work. Can someone help me?
Hello
I'm trying to program a LED-cube(3x3x3) to light every LED one by one and when one layer has been lit, it stays that way and continues on to the next layer. I have tried with millis() but doesn't get it to work. Can someone help me?
Oh sorry, here it is
int layer[] = {0, 1, 2};
int led[] = {3, 4, 5, 6, 7, 8, 9, 10, 11};
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0;
long interval = 300;
void setup() {
for (int i = 0; i < 3; i++) {
pinMode(layer[i], OUTPUT);
}
for (int i = 0; i < 9; i++) {
pinMode(led[i], OUTPUT);
}
for (int i = 0; i < 3; i++) {
digitalWrite(layer[i], HIGH);
}
}
void fastBlinkLayer0() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
}
else
ledState = LOW;
// set the LED with the ledState of the variable:
for (int i = 0; i < 9; i++) {
digitalWrite(led[i], ledState);
digitalWrite(layer[1], ledState);
}
}
}
void firstLayer() {
digitalWrite(layer[0], LOW);
for (int i = 0; i < 9; i++) {
digitalWrite(led[i], HIGH);
delay(500);
}
digitalWrite(layer[0], HIGH);
}
void secondLayer() {
digitalWrite(layer[0], LOW);
for(int i=0; i<9; i++){
digitalWrite(led[i], HIGH); //Here we'd want some sort of delay or something
}
fastBlinkLayer0();
}
void loop() {
/*
for (int i = 0; i < 3; i++) {
digitalWrite(layer[i], LOW);
fastBlink();
digitalWrite(layer[i], HIGH);
fastBlink();
}
*/
// firstLayer();
secondLayer();
}
OK, seems you are working from scratch. Good fun!
The first thing is, this whole project is a matter of "multiplexing" which means that to permit potentially all the LEDs to appear lit, you must continuously illuminate the LEDs in each of the layers, one after the other, so that each layer is illuminated for a third of the time and all three layers are illuminated one after the other, at least fifty times each second.
This means that the "delay()" function has no part whatsoever in your code.
Now where you say "Here we'd want some sort of delay or something", you actually have the logic backwards and inside out! What you actually want, is a subroutine ("function") which de-activates the LEDs in the current layer, sets up all of the LEDs in the next layer and activates that layer before returning.
This function is then called each time millis() advances by the prescribed number (5 would do fine) as you pass through the main loop. That is to say, the main loop as merely one of its functions, checks millis() and if it has advanced by the prescribed number (5), executes your function to switch to the next layer, otherwise or in fact if that function has just been called, proceeding to do the other necessary parts of your overall project which means of course, to make the necessary calculations to decide just which of the 27 LEDs should be illuminated according to the present pattern.
Quite frankly, 3 by 3 by 3 is an inconvenient number, 4 by 4 by 4 is much easier if you perform all the display multiplexing and driving using a MAX7219; you then only have to generate the LED patterns and feed them to the MAX7219 - it does all the display for you.
Can you please write the code?
Paul__B:
OK, seems you are working from scratch. Good fun!The first thing is, this whole project is a matter of "multiplexing" which means that to permit potentially all the LEDs to appear lit, you must continuously illuminate the LEDs in each of the layers, one after the other, so that each layer is illuminated for a third of the time and all three layers are illuminated one after the other, at least fifty times each second.
This means that the "delay()" function has no part whatsoever in your code.
Now where you say "Here we'd want some sort of delay or something", you actually have the logic backwards and inside out! What you actually want, is a subroutine ("function") which de-activates the LEDs in the current layer, sets up all of the LEDs in the next layer and activates that layer before returning.
This function is then called each time millis() advances by the prescribed number (5 would do fine) as you pass through the main loop. That is to say, the main loop as merely one of its functions, checks millis() and if it has advanced by the prescribed number (5), executes your function to switch to the next layer, otherwise or in fact if that function has just been called, proceeding to do the other necessary parts of your overall project which means of course, to make the necessary calculations to decide just which of the 27 LEDs should be illuminated according to the present pattern.
Quite frankly, 3 by 3 by 3 is an inconvenient number, 4 by 4 by 4 is much easier if you perform all the display multiplexing and driving using a MAX7219; you then only have to generate the LED patterns and feed them to the MAX7219 - it does all the display for you.
Hi, i'm doing a similar project and have the same problem. So the layer should be illuminated 50-60 times each second. Then i need a function that makes the current layer HIGH(deactivate) and jump to the next layer and make it LOW, which will lit the chosen LEDS in this layer, and make it HIGH again before returning to the current layer?
When i call the function, can i use something like: if millis() are bigger than prescribed number, then go to function?