Need some help with pointers

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.

I declare the array like this at the top.

int (*functionsArray[6]) = {
  oneAfterAnotherLoop,
  oneAfterAnotherNoLoop,
  oneOnAtATime,
  pingPong,
  marquee,
  randomLED
};

And i have made a function at the bottom like this

void potmeterChoice(int potmeterInput){
  int choise = floor(potmeterInput/1023)*6;
  *functionsArray[choise]();
}

And this is whats running in the loop function

potmeterChoice(analogRead(potmeterPin));

I attached the ino file just in case.

Oppgave6_code_ino2.ino (8.99 KB)

  int choise = floor(potmeterInput/1023)*6;

For every value of potmeterInput that is less than 1023, choise will be zero. Is that what you want?

oh snap forgot some parentesis

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
};

oh snap forgot some parentesis

That's not all you forgot.

(potmeterInput/1023)

will still result in 0 or 1 (mostly 0).

Use the map() function.

PaulS:

oh snap forgot some parentesis

That's not all you forgot.

(potmeterInput/1023)

will still result in 0 or 1 (mostly 0).

Use the map() function.

(512/1023)*6 = 3.002
floored = 3

edit: I didnt want to come off rude so, thanks for reminding me of the map function sir.

512/1023 != 0.5

An int divided by an int is an int.

512/1023 = 0

On the other hand, 512/1023.0 IS 0.5 (or thereabouts).

PaulS:
512/1023 != 0.5

An int divided by an int is an int.

512/1023 = 0

On the other hand, 512/1023.0 IS 0.5 (or thereabouts).

Aha thanks. Btw which has the most overhead, the map function or just using math?

Using float math has higher overhead that integer math

Try

512 * 3 / 1023

instead of

512 / 1023.0 * 3

The results will be the same, and be a lot simpler for the Arduino to calculate.

Try...instead of...The results will be the same

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.

You could also put an L or a UL on the end of your numbers to force them to be recognised as long or unsigned long instead of int.

PaulS:

Try...instead of...The results will be the same

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

Wouldn't the value have to go past [-32768,32768] to overflow ? and 6*1024 is 6144.

When I did the math, I came up with 60,000+. Seems like I slipped up somewhere. Overflow would not be a problem in this case.

funkake:

PaulS:

Try...instead of...The results will be the same

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.

  int choise = floor(potmeterInput/1023)*6;

All you have to do is multiply first:

  int choise = potmeterInput * 6 / 1023;