Mapping 2 sets of numbers to switch case help please

Hi all I'm mapping rotation per minute of 2000rpm-16500rpm to case switch between 1-30 with this:

 rpmState = map(RPM, 2000, 16500, 1, 30);

I was just wondering what happens if my RPM is under 2000 or over 17000?

If the RPM is under 2000 or over 17000 how can I make sure that it goes to: switch DEFAULT, and not to -negative -1 case or case above 30... 31 or 32..... I only want to use 1-30 and default

Hi,
If you want limit the rpmState use Constrain function.

https://www.arduino.cc/reference/en/language/functions/math/constrain/

Tom... :slight_smile:

myele:
If the RPM is under 2000 or over 17000 how can I make sure that it goes to: switch DEFAULT, and not to -negative -1 case or case above 30… 31 or 32… I only want to use 1-30 and default

The switch statement will only go to the cases you define. Every other value goes to the “default:” case, if you defined one. If you don’t define the “default:” case then nothing is done with any values for which there is not a defined case.
It seems very odd that there are 30 different things you want to do based on RPM. I suspect you could just use a lookup table.

Yea I'm sure I'm making my code waaaaay too complicated but Im just learning Arduino so I don't know of a better way to do it unfortunately. Maybe you can help me simplify my LOOP code. So yes I have 30 cases which I didn't have to repeat almost the same code over and over.

  rpmState = map(RPM, 2000, 16500, 1, 30);
  switch (rpmState) {
    case 1:        
analogWrite(pwmOutputPin4, readEeprom20  * 10.23);
analogWrite(pwmOutputPin5, readEeprom20  * 10.23);
    break;
    case 2:
analogWrite(pwmOutputPin4, readEeprom25  * 10.23);
analogWrite(pwmOutputPin5, readEeprom25  * 10.23);
    break;
    case 3:
analogWrite(pwmOutputPin4, readEeprom30  * 10.23);
analogWrite(pwmOutputPin5, readEeprom30  * 10.23);
    break;
...
...
    case 30:
analogWrite(pwmOutputPin4, readEeprom165  * 10.23);
analogWrite(pwmOutputPin5, readEeprom165  * 10.23);
    break;
}