Shop OBEX P1 Docs P2 Docs Learn Events
LCD Bargraph help again please — Parallax Forums

LCD Bargraph help again please

tgraz34tgraz34 Posts: 16
edited 2010-02-25 19:55 in BASIC Stamp
Im trying to use this code for my bar graph so that i can display how many RPM my tachometer is counting. I just dont know where to put my RPM variable in the this code. This code is set up so that you type a certain number 0-80 in the debug screen and it displays the value on the LCD as a bar graph. Please reply if you want me to clarify something. THANK YOU.
' {$STAMP BS2}
' {$PBASIC 2.5}
' -----[noparse][[/noparse] Title ]--------------------------------------------------------------
' Smart Sensors and Applications - HorizBarGraph.bs2
' Display values entered into the Debug Terminal's Receive windowpane
' as horizontal bar graph data in the LCD.

' {$STAMP BS2}                               ' Target device = BASIC Stamp 2
' {$PBASIC 2.5}                              ' Language      = PBASIC 2.5

' -----[noparse][[/noparse] I/O Pins ]-----------------------------------------------------------

LcdPin         PIN     15                    ' I/O pin connected to LCD's RX

' -----[noparse][[/noparse] Constants ]----------------------------------------------------------

T9600          CON     84                    ' True, 8-bits, no parity, 9600

LcdCls         CON     12                    ' Form feed -> clear screen
LcdCr          CON     13                    ' Carriage return
LcdOff         CON     21                    ' Turns display off
LcdOn          CON     22                    ' Turns display on
Line0          CON     128                   ' Line 0, character 0
Line1          CON     148                   ' Line 1, character 0
Define         CON     248                   ' Address defines cust char 0

' -----[noparse][[/noparse] Variables ]----------------------------------------------------------

custChar       VAR     Nib                   ' Custom charcter selector
index          VAR     Nib                   ' Eeprom index variable
dotLine        VAR     Byte                  ' 5-pixel dotted line
cursor         VAR     Nib                   ' Cursor placement
value          VAR     Byte                  ' Value to be graphed.
charCnt        VAR     Byte                  ' Character counting variable
line           VAR     Byte                  ' Line0 or Line1

' -----[noparse][[/noparse] Initialization ]-----------------------------------------------------

PAUSE 100                                    ' Debounce power supply
SEROUT LcdPin, T9600, [noparse][[/noparse]LcdOn, LcdCls]        ' Initialize LCD
PAUSE 5                                      ' 5 ms delay for clearing display

custChar = 3                                 ' Select Custom Character 3
dotLine = %11111                             ' Black all pixels in each line
GOSUB Def_Horiz_Bar_Char                     ' Character define subroutine

line = Line0                                 ' BarGraph on Line 0.

DEBUG "Enter values (0 to 80)", CR

' -----[noparse][[/noparse] Main Routine ]-------------------------------------------------------

DO                                           ' Main loop

  DEBUG ">"
  DEBUGIN DEC value                          ' Value from Transmit windowpane
  GOSUB Bar_Graph                            ' Display as bar graph

LOOP                                         ' Repeat main loop

' -----[noparse][[/noparse] Subroutine - Bar_Graph ]---------------------------------------------

Bar_Graph:

  ' Fill from left with black bars

  value = value MAX 80                       ' Limit value - 0 to 80
  charCnt = value / 5                        ' Number of black bars
  custChar = 3                               ' Choose black custom character

  IF charCnt > 0 THEN                        ' If black bars to print then
    FOR cursor = 0 TO charCnt - 1            ' Print charCnt - 1 black bars
      GOSUB Disp_Cust_Char                   ' Print the black bar
    NEXT
  ENDIF

  ' Display Custom Character 2 with a certain number of black columns.

  cursor = charCnt                           ' Place cursor
  custChar = value // 5                      ' How many 5ths of a bar?
  ' Choose bit pattern for custom character definition
  LOOKUP custChar,
         [noparse][[/noparse]%00000, %10000, %11000, %11100, %11110],
         dotLine
  custChar = 2                               ' Set custom character to 2
  GOSUB Def_Horiz_Bar_Char                   ' Define the custom character
  GOSUB Disp_Cust_Char                       ' Display the custom character

  ' Print over everything to the right with spaces.

  IF (charCnt + 1) < 15 THEN                 ' Partial char left of char 15?
    FOR cursor = (charCnt + 1) TO 15         ' Fill to right with " "
      SEROUT LcdPin, T9600,
             [noparse][[/noparse]line + cursor, " "]
    NEXT
  ELSEIF value = 80 THEN                     ' Special case: value = 80
    SEROUT LcdPin, T9600,
           [noparse][[/noparse]line + cursor, 3]
  ELSEIF charCnt = 14 THEN                   ' Special case: 75 <= value <= 80
    SEROUT LcdPin, T9600, [noparse][[/noparse]line + 15, " "]
  ENDIF

  RETURN

' -----[noparse][[/noparse] Subroutine - Def_Horiz_Bar_Char ]------------------------------------

Def_Horiz_Bar_Char:

  SEROUT LcdPin, T9600,                      ' Define custom character
         [noparse][[/noparse]Define + custChar]
  FOR index = 0 TO 7                         ' 7 bytes, define 7 dotted lines
    SEROUT LcdPin, T9600, [noparse][[/noparse]dotLine]          ' Send it to the LCD
  NEXT

  RETURN

' -----[noparse][[/noparse] Subroutines - Disp_Cust_Char ]---------------------------------------

' This subroutine displays a custom character.  The line variable can
' be set to either Line0 or Line1, and the cursor variable can be set
' to a value between 0 and 15.  The custChar variable selects one of the
' LCD's custom characters and should be set to a value between 0 and 7.

Disp_Cust_Char:

  SEROUT LcdPin, T9600,                    ' Print custom character
         [noparse][[/noparse]line + cursor, custChar]
  RETURN

Comments

  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-02-25 05:39
    DO                                           ' Main loop
      GOSUB acquireRPM  ' your subroutine 
      ' now scale RPM into range of 0-80...
      value = RPM * 80 / RPMmax   ' where RPMmax is the maximum you want to display.
      GOSUB Bar_Graph                            ' Display as bar graph
    LOOP                                         ' Repeat main loop
    



    What is the range of RPM? The math to scale the value depend on that. For example, the above math will fail if the RPM is greater than 819 and it won't mean much if the maximum is 2.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • tgraz34tgraz34 Posts: 16
    edited 2010-02-25 05:58
    my RPM max will be 3500. Can you explain why it will fail if rpm is greater that 819.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-02-25 07:16
    80 * 3500 = 280,000
    which is greater than 65535, the largest integer that the Stamp can handle in one gulp. 16 bits.

    For your math, you can do,
    value = RPM * 4 / 175
    That is simply reducing the fraction, 80/3500 = 4/175, and 3500 * 4 is only 14000, within bounds. Or, reduce it further to,
    value = RPM / 44

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • tgraz34tgraz34 Posts: 16
    edited 2010-02-25 19:55
    would this work the same way?

    DO
      COUNT SpeedIn, Capture, Pulses
      rpm = (pulses * 12)
      value = rpm / 30
      GOSUB Bar_Graph:                                       ' Repeat main loop
    LOOP
    
    
Sign In or Register to comment.