Home > Net >  What is an algorithm for displaying rpm on set of LEDs?
What is an algorithm for displaying rpm on set of LEDs?

Time:07-13

So I have an Arduino that is reading the rpm from my car. I also have a 15-pixel neopixel strip connected to the Arduino.

What I want the Arduino to do is to make the led strip show the rpm. As the rpm increases, the number of LEDs that turn on is increased from the left side of the strip.

What I am stuck on and would love help with is that I don’t just want the LEDs to turn on or off based of the rpm, but also change brightness.

For example let’s say the rpm is at 2000, so pixel 4 (arbitrary number that will be calculated by the equation) is the top pixel to turn on. Now as the rpm is increased, pixel 5 will increase in brightness from 0 to 255. Then pixel 6 will increase in brightness and so on, creating a smooth transition between pixels.

So what I want help with is being able to input the rpm and output the top pixel and it’s brightness. From there I will be able to just fill in the LEDs below the top pixel.

I want the top rpm to be 8000.

Let me know if you need more info. Thanks!

CodePudding user response:

Does this code help? This has the code to calculate the last pixel - and the brightness of that pixel.

#define NUM_LEDS 15
#define NUM_BRIGHTNESS_LEVELS 256
#define MAX_REVS 8000

void setup()
{
  Serial.begin(9600); 
}

void loop()
{
    for ( int revs = 0 ; revs <= MAX_REVS ; revs  = 500 )
    {
      int totalBrightness = ((float)revs / MAX_REVS) * (NUM_LEDS * NUM_BRIGHTNESS_LEVELS);
      int lastPixel = (totalBrightness / NUM_BRIGHTNESS_LEVELS); 
      int brightness = totalBrightness % NUM_BRIGHTNESS_LEVELS; 

      if ( lastPixel >= NUM_LEDS )
      {
        lastPixel = NUM_LEDS - 1; 
        brightness = NUM_BRIGHTNESS_LEVELS - 1; 
      }

      Serial.print("revs = ");
      Serial.print(revs);
      Serial.print(", pixel = ");
      Serial.print(lastPixel);
      Serial.print(", brightness = ");
      Serial.println(brightness); 
      
      delay(100); 
    }
    delay(2000);
}
  • Related