Unfortunately i left my arduino at uni so i cant check if this code works. And i thought since you guys are experienced you might be able to spot eventual flaws.
I am using the code from example 4. I am going to use a potentiometer to choose between the premade functions. Instead of using switch case or if else i had this idea that i would use a array of pointers.
void potmeterChoice(int potmeterInput){
int choise = floor((potmeterInput/1023)*6);
*functionsArray[choise]; // changed from *functionsArray[choise]();
}
thanks for pointing it out.
void (*functionsArray[6])(void) = { // added (void) not sure if it is needed
oneAfterAnotherLoop,
oneAfterAnotherNoLoop,
oneOnAtATime,
pingPong,
marquee,
randomLED
};
Watch for overflow, though, as would happen when changing the 3 to 6.
Using map() is less intensive than floating point arithmetic, since map does what majenko alluded to, but using longs so overflow is avoided.
You could change the type of potmeterInput to long, instead of int, and perform the multiplication first, then the division, without overflow or truncation issues, and without the need to use map() or floats.
Watch for overflow, though, as would happen when changing the 3 to 6.
Using map() is less intensive than floating point arithmetic, since map does what majenko alluded to, but using longs so overflow is avoided.
You could change the type of potmeterInput to long, instead of int, and perform the multiplication first, then the division, without overflow or truncation issues, and without the need to use map() or floats.
Wouldn't the value have to go past [-32768,32768] to overflow ? and 6*1024 is 6144. I am a bit puzzeled here xD
Watch for overflow, though, as would happen when changing the 3 to 6.
Using map() is less intensive than floating point arithmetic, since map does what majenko alluded to, but using longs so overflow is avoided.
You could change the type of potmeterInput to long, instead of int, and perform the multiplication first, then the division, without overflow or truncation issues, and without the need to use map() or floats.
Wouldn't the value have to go past [-32768,32768] to overflow ? and 6*1024 is 6144. I am a bit puzzeled here xD
That's -32768 to 32767. 0 is classed as positive, which makes 32878 negative and 32768 positive numbers.