RPM LEDs code help.

Hello all. I have a sketch that reads a cars ECU via OBD that all works great but now its time to move on and actually displaying some info. Am looking at using the i2c bus to control LEDs and seven segments but to start with i just want to use 8 LEDs to show RPM (1 LED = 1000RPM).

#define ERPM    0x0C //Engine RPM.

//Main.
void loop()
{
  unsigned long result = 0;
    
  get_PID(ERPM, &result);
  if(result <= 999)
  {
   // B00000000 value to be displayed on 8 LEDs. 1 = LED on, 0 = LED off.
  }
  else if(result >= 1000 && result <= 1999)
  {
    //B10000000 value to be displayed on 8 LEDs. 1 = LED on, 0 = LED off.
  }
  else if(result >= 2000 && result <= 2999)
  {
    //B11000000 value to be displayed on 8 LEDs. 1 = LED on, 0 = LED off.
  }
  else if(result >= 3000 && result <= 3999)
  {
    //B11100000 value to be displayed on 8 LEDs. 1 = LED on, 0 = LED off.
  }
  else if(result >= 4000 && result <= 4999)
  {
    //B11110000 value to be displayed on 8 LEDs. 1 = LED on, 0 = LED off.
  }
  else if(result >= 5000 && result <= 5999)
  {
    //B11111000 value to be displayed on 8 LEDs. 1 = LED on, 0 = LED off.
  }
  else if(result >= 6000 && result <= 6999)
  {
    //B11111100 value to be displayed on 8 LEDs. 1 = LED on, 0 = LED off.
  }
  else if(result >= 7000 && result <= 7999)
  {
    //B11111110 value to be displayed on 8 LEDs. 1 = LED on, 0 = LED off.
  }
  else if(result >= 8000 && result <= 8999)
  {
    //B11111111 value to be displayed on 8 LEDs. 1 = LED on, 0 = LED off.
  }
}

A little info about the above, unsigned long result = 0; is a variable that will contain anything from 0 to 9000 and get_PID(ERPM, &result); is the command that gets the info from the car that works fine.

As you can see from the above that i want to turn on some LEDs over i2c (to the LED driver) to show the rpm. Am i better using some kind of for loop and place the binary valuse in a const struct or array or PROGMEN?

Any ideas or examples would be great. In the end i will be adding more i2c ic's to display speed on a seven segment.

Also am wanting to have a shift LED that will flash when the RPM gets to a pre set value via a rotary switch / hex switch.

I had tried to do a similar thing with my truck, but with rgb leds in the gauges that would fade/change color depending on rpm. I did all my code in the loop, which worked fine for me. But since you aren't fading them, then it shouldn't take up much memory at all. As for the rotary encoder, there are a few different libraries to use, just be sure to debounce your hardware or software, or you will get erratic readings. Or you may just simply use a potentiometer and have the min value at 4 and max at 8, or whichever you choose. The pot idea is easier to implement, and requires less code; I would use one of the little pcb mounted ones, that way you don't accidentally bump it and change your shift rpm.

As for the driver, you can use an IC or just a few transistors. You're not using PWM so you can just tie transistor in place to drive the led load.

And the 7 segment display, another option to keep in mind, you can use a backlit lcd display. I know you can pick them up on ebay for less than 10 dollars. And it would allow a fancier UI for adjusting your shift light, or w/e else you wanna add to it.

Food for thought.

Food for thought is good, thank you. I do plan on using a LCD aswell to display engine load etc. I can get any info i want from the car now and display it in the serial monitor but i will be having lots of display items so am not sure how to code a routine to update all the displays with no visable delays. So far in the end i want 2 4 digit seven segment displays (1 for speed and 1 for voltage or something) a row of 8 LED's for RPM + 1 LED for the shit indicator and a LCD for as i said above) It's gonna be a lot of pins needed so i would like to use the i2c bus for all may display items.

I2C shift register and LEDs: Gammon Forum : Electronics : Microprocessors : Using a 74HC595 output shift register as a port-expander

Thinking about using one of these http://uk.farnell.com/on-semiconductor/cat9532wi-t1/led-driver-16bit-program-24soic/dp/1656134

You have all of those ELSE IFs based on your RPM result/s (so > 1000 gets 1 on, > 2000 gets 2 on, etc.)
You could use the RPM result (/1000) as the index for an array.

byte bargraph [9] = {0,1,3,7,15,31,63,127,255};

result = result /1000; // bargraph [result]