I need to write 256 cases or find another alternative, to turn on certain LEDs is there any way to do this?
It would help if you showed specifically what each case is required to do but I'd guess that using an array is going to make the switch statement unnecessary.
Pete
P.S. In some cases even the array won't be necessary. For example if you have this sort of thing:
switch(amp) {
case 0:
analogWrite(A2,0);
break;
case 1:
analogWrite(A2,1);
break;
etc.
}
then it will obviously reduce to just this:
analogWrite(amp);
Without knowing what you're trying to do it's impossible to give a correct answer, but possibly you could use a for loop...
for(int i = 0; i < 255; i++) {
//do logic here
}
bwoogie:
Without knowing what you're trying to do it's impossible to give a correct answer, but possibly you could use a for loop...for(int i = 0; i < 255; i++){
//do logic here
}
sp. "for(int i = 0; i < 256; i++)"
krishv11:
I need to write 256 cases or find another alternative, to turn on certain LEDs is there any way to do this?
There is an advanced method to create a series of unnamed anonymous functions stored in an array... But I'm not sure this is the best way for you to go it is advanced! With that said, it is a way to create lots of conditions and this would execute extremely fast.
From Gammon Forum: Lambda functions Last Example Slightly Modified to use all 5 anonymous (lambda) functions
// array of function pointers also known as lambda functions or anonymous functions
// put functions you want represented above the array or: or you will need t
// void AlternateFunction(); // Declared function must be above the Lambda array and the actual function definition can be elseware
void AlternateFunction(){
Serial.println (3);
}
void (*doActionsArray []) () =
{
[] { Serial.println (0); } ,
[] { Serial.println (1); } ,
[] { Serial.println (2); } ,
[] { AlternateFunction(); } , // Lets call another function from within the Lambda Function
[] { // just like a regular function you can do lots of things here
Serial.print (4);
Serial.println ("The End");
}
};
void setup (){
Serial.begin (115200);
Serial.println ("Lambda anonomous funciton array test");
byte action; // up to 256 functions. use an int to have more.
for(action = 0; action < 5; action++){
doActionsArray [action] ();
}
} // end of setup
void loop () { }
Z
P.S. If nothing else, compile it and try it :). I was amazed when I discovered this programming option. It has many uses. You see it used in Javascript web development. I just never knew it was available for arduino platform until recently.
I agree. You have to tell us what you need this for. It's very possible that there is a simple solution that is not what you expect. Specifically, what do you need to do with the LEDs?