for my project I am trying to have 2 animation of LEDs (10 and 4) in the same time. Each LED is controled separetly by PWM. Each animation, blinking and constant lighting is turn on/off by different button (only 5V or GND). I have tried to use delay() function but of course the delay() is blocking the second animation... So when I inspected this forum and I have seen function millis() I did think that I am winner but I still have problem with implementation.
I have exact matrix 10x200 (10 LEDs; 200 miliseconds) with PWM values from 0-255... :
And for the second function I have different matrix with different LENGTH where are differen possibility in case push different button... just blinking or constant lighting etc.
void AnimationLEDs2(){
// AnimationLEDs2
if (buttonStateLEDs1>2){
.
.
.
So two loops in one main loops. It seems like that is not possible to use millis(), is it? How I can secure that each animation will works properly without interuption and on each LED will be correct value in requred time?
Welcome to the forum! +1 for using code tags with your code. An improvement would be to post your ENTIRE sketch so people can see it all.
Check out the tutorial several things at the same time to see how you cam make multiple things "appear" to run at the same time. Also check out the Blink Without Delay example in the IDE (File->examples->02.Digital->Blink Without Delay)
Finally, not all pins on a UNO are capable of analogWrite(), only certain ones. It would be much better to do a digitalWrite() if you are setting them HIGH (255) or LOW (0).
You also will never see this animation with a 1millisecond delay. Did you intend a 1 second delay (1000 msec)?
void AnimationLEDs1() {
static unsigned long startTime;
static unsigned long duration;
static int state;
unsigned long currentTime = millis();
// AnimationLEDs1
if (buttonStateLEDs1 < 1000) {
return;
}
switch (state) {
case 0: // setup
analogWrite(2, 128);
for ( int i = 3; i < 12; ++i ) {
digitalWrite(i, LOW);
}
startTime = currentTime;
duration = 1000;
state++;
break;
case 1:
if ( currentTime - startTime >= duration ) {
digitalWrite(2, HIGH);
analogWrite(3, 13);
startTime = currentTime;
duration = 1000;
state++;
}
break;
case 2:
if ( currentTime - startTime >= duration ) {
analogWrite(3, 53);
startTime = currentTime;
duration = 1000;
state++;
}
break;
case 3:
if ( currentTime - startTime >= duration ) {
analogWrite(3, 93);
startTime = currentTime;
duration = 1000;
state++;
}
break;
case 4:
if ( currentTime - startTime >= duration ) {
analogWrite(3, 134);
startTime = currentTime;
duration = 1000;
state = 0; // start again
}
break;
}
}
Thank you for welcome. Yes, I will improve myself in future.
I will look to the documents/links today. So thank you again.
Regarding the rest. I do not use UNO but MEGA 2560 so I really have so many PWM channels (yes I know it is like that I am killing ant by bazooka... But you know I am toyer ). You are right that eyes will not see difference between 2 miliseconds (limit is somewhere around 20 miliseconds). So I can do some compromis like change data each 10 milisends or somethin else. But what is definitely visible is difference between diming and just changing turn on/turn off (LOW/HIGH). So I have to use analogWrite(). Just for example... This is one my table for one animation ("table of pain" for me right now ):
Hello, I am tuning it on single LEDs but after I will fix all bugs.. I will use these outputs for opening transistors where I will have something around 2-4 LEDs in an each string.
Animation itself is working really 1/5s (200ms) but after 200ms there is 200ms of maximum value (255 PWM) in each otput (each LEDs is lighting) and after 400ms there is 400ms of off. So duration of complete sequence is 800ms. But it is only one animation.... In parallel there should be possibility to lit up different animation on different LEDs.
Using these tables will be helpful for you to design the patterns, but I would avoid making these tables into data tables in the code. Instead, once I had the design of the pattern in the table in the spreadsheet, I would then try to describe the pattern as an algorithm which is as simple as possible. Then I would code that algorithm into the sketch.
For example with your example above, what are the times that the fade from red to yellow begins, and what are the times each led turns from yellow to green? How can that series of times be described as a formula which depends on the position/number of the led? You can then test your formula in the spreadsheet. Once you have it correct, then you can use that formula in your code.