LED bargraph with max7219 based on rpm/speed input
chetw77cruiser
Posts: 12
Well, this looks to be my first post here, so I will start off easy. I have been working some on a digital instrument sluster and I have a programmers cramp. I want to be able to input a max rpm variable, divide that by 40(number of leds attached to a max7219) and lite individual leds based on rpm*1, rpm*2, etc. mainly in that order. I am trying the if-else if with little success. I am stumped. I want to be able to enter anyrhing from 4000 rpm to 8000 rpm and be able to divide that between 40 leds, maybe more if needed. Would the lookup/lookdown be a better way?
If this is to little information, let me know and I will fill in the blanks.
Chet W.
If this is to little information, let me know and I will fill in the blanks.
Chet W.

Comments
(rpm - rpmmin) / (rpmmax - rpmmin) = n / number_of_LEDs
with numbers:
(rpm - 4000) / (8000 - 4000) = n / 40
or
n = (rpm - 4000) / 100
For rpm = 4000 this gives you n = 0 (no LED), rpm = 8000 will result in n = 40 (all LEDs). HTH
I am not sure how to code that and have it output to the max7219. I forgot to mention that I am using spin and I only have a basic knowledge of programing in general, mainly from 15 years ago using qbasic. I am using this project as a learing experience.
How do you get the RPM into your system? Also, if your min/max values are different simply substitute them in the equation.
con _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 #0, CLS, HOME, #8, BKSP, TAB, LF, CLREOL, CLRDN, CR ' PST formmatting control 'cyl4 = 30 'cyl6 = 20 'cyl8 = 15 tachin = 2 var long rpm,cylsel,redline obj fc : "jm_freqin" term : "jm_txserial" pub main | f cylsel := 8 redline := 4500 fc.init(tachin) ' freq cntr on p0 term.init(30, 115_200) ' start terminal waitcnt(clkfreq/10 + cnt) term.tx(CLS) repeat f := fc.freq ' get frequency if cylsel := 4 rpm := (f*30/10) if cylsel := 6 rpm := (f*20/10) if cylsel := 8 rpm := (f*15/10) term.str(string(HOME, "revs: ")) 'rpm := (f*30/10) term.dec(rpm) ' print whole part, 30 constant is for 4-cylinder, four-stroke ICE. term.str(string(" rpm", CLREOL)) if rpm > redline term.str(string("WARNING")) waitcnt(clkfreq/15 + cnt)con _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 #0, CLS, HOME, #8, BKSP, TAB, LF, CLREOL, CLRDN, CR ' PST formmatting control 'cyl4 = 30 'cyl6 = 20 'cyl8 = 15 tachin = 2 var long rpm,cylsel,redline obj fc : "jm_freqin" term : "jm_txserial" pub main | f cylsel := 8 redline := 4500 fc.init(tachin) ' freq cntr on p0 term.init(30, 115_200) ' start terminal waitcnt(clkfreq/10 + cnt) term.tx(CLS) repeat f := fc.freq ' get frequency if cylsel := 4 rpm := (f*30/10) if cylsel := 6 rpm := (f*20/10) if cylsel := 8 rpm := (f*15/10) term.str(string(HOME, "revs: ")) 'rpm := (f*30/10) term.dec(rpm) ' print whole part, 30 constant is for 4-cylinder, four-stroke ICE. term.str(string(" rpm", CLREOL)) if rpm > redline term.str(string("WARNING")) waitcnt(clkfreq/15 + cnt)