So Ive got 12 leds and I am trying to flash them in different combinations. I store the combinations in an enumerator in this format
pattern1 = 0b010101010101
1 is flash it, 0 is dont...
I've got this function:
static int lastLED = -1;
for (int i = 0; i < 12; i++){
if(((1<<(11-i)) & pattern1) && i != lastLED) {
flash(i);
lastLED = i;
}
}
for (int i = 11; i >= 0; i--){
if(((1<<(11-i)) & pattern1) && i != lastLED) {
flash(i);
lastLED = i;
}
}
lastLED stops it repeat flashing the leds at the end of the pattern.
Flash also has a delay in.
What I want to do however is change this to use a timer interupt - which I already have setup. So in loop() i check the state of a flag, and if its 1 (the time has ticked...) flash the next led that should be flashed, i.e the if conditons above.
My problem is how to move i along to find the next led that should be flashed BETWEEN timer ticks, so that the variable is ready for when the led needs to flash ALSO how to know whether its going up or down - like the above code does. I think I could use lastLED somehow to store the last position, however Im not sure how to get it to go up or down the binary represrentation of the pattern
Thanks
This is one way you could do it:
volatile boolean tick = false;
void loop(){
static byte led = 0;
static boolean direction = false;
if(tick){
//Timer has ticked.
tick = false; //set false for next time.
if(bitRead(pattern1,(11-led)){
flash(led); //Flash the current LED if the leds bit in the pattern is set.
}
//I gather from your example that you are trying to count up through the pattern then back down
if (direction){
led--; //if counting down, move to the next LED down
if(led == 255){ //Overflow so gone past led 0
led = 0; //return to led 0
direction = false; //change direction
}
} else {
led++;//if counting up, move to the next LED up
if(led == 12){ //there are only 12 leds, so if we reach 12, we have gone too far
led = 11; //move back to 11
direction = true; //change direction
}
}
}
}
Then just put:
tick = true;
in your ISR
You may want to have a look at the different experiments on my webpage. If you want to blink LEDs my page will most probably contain answers to some of the questions that you did not even think about 
For your current question: you may want to have a look at the "persistence of vision" and the "knight rider" as well as the "removing flicker" experiments.
Ok I'll have a look at your page,
Tom, yeah i got to a similar place as your example code there, the problem with it being that your code doesnt jump forward to the next '1' in the pattern, and wait until tick = true, to display the "flash", what it does is each time that tick = true, it checks whether the next bit is a 1, and if so it flashes. In other words, if the time is set to 1 hz, and the pattern is 101010101010, then it is 2 second gaps between flashes (except when it has gone up and down again and is repeating because then it is 11...)
I cant get my head around how to get it to read the pattern until the next 1, then wait, so the moment that tick = true, the next led in the sequence flashes.
Udo, just looking at your knight rider code, you dont use an interupt to deal with the situation. I have got everything working without an interupt fine, but the issue is if you want the pattern to happen along side other things happening - in my case the user being able to navigate a menu at the same time.
I want the loop to work out whether it needs to move the led on to the next one or the timer has ticked, therefore display the led. Its a real sudoku...
There is more than one knight rider example. For example Knight Rider Without Flicker | Blinkenlight. If you refer to my code please also refer to the URL. Otherwise it gets pretty hard to figure which code you are talking about.
The point is: the pov example Persistence of Vision | Blinkenlight shows how to animate LEDs without doing anything at all in the main loop.
The knight rider example shows how to fill arrays at compile time. You can combine these two approaches to reach your goal. Especially if you do not want to have PWM as well this is not to hard to do.
Thus you can use the pov approach for the cycling and the knight rider approach for filling the array. Which leaves the question how to switch the effects. For this I also have quite some examples.