Menu

glcd gauge using MAP or Scale

2018-02-21
2018-03-04
  • stan cartwright

    stan cartwright - 2018-02-21

    This project was used to test the MAP or scale function in Contributors.
    I map readad to pulseout range for a servo actuator.
    I then map pulseout to degrees and display as a dial.
    It's a pretty servo actuator tester. poor video. https://www.youtube.com/watch?v=riaFvlKl1kE&feature=youtu.be

    ;scale potentiometer readad to 45-315 deg for glcd and
    ;scale to .75 to 2.25 ms for sevo actuator
    #chip mega328p, 16
    #include <UNO_mega328p.h >
    #option explicit
    #include <glcd.h>
    #INCLUDE <TRIG2PLACES.H>
    ;
    #define AD_REF_SOURCE AD_REF_256
    #define ADSpeed lowSpeed
    ;---- glcd set up
    #define GLCD_TYPE GLCD_TYPE_ILI9341
    'Pin mappings for SPI - this GLCD driver supports Hardware SPI and Software SPI
    #define GLCD_DC       DIGITAL_8         ' Data command line
    #define GLCD_CS       DIGITAL_10        ' Chip select line
    #define GLCD_RESET    DIGITAL_9         ' Reset line, you could just tie high
    #define GLCD_DI       DIGITAL_12        ' Data in | MISO    - Not used therefore not really required
    #define GLCD_DO       DIGITAL_11        ' Data out | MOSI
    #define GLCD_SCK      DIGITAL_13        ' Clock Line
    ;
    #define ILI9341_HardwareSPI    ' remove/comment out if you want to use software SPI.
    #define GLCD_EXTENDEDFONTSET1
    GLCDfntDefaultsize = 2
    GLCDCLS ili9341_black
    GLCDBackground = ili9341_blue
    GLCDRotate (3)
    
    ;---- glcd done,start program
    dim radius,xcentre,ycentre,xstart,ystart,xend,yend,pulse_out as byte
    dim angle,needlecolour,newval,oldval as word
    dir portc.0 in ;readad pin
    dir portd.7 out ;to servo
    ;
    radius=170
    xcentre=160
    ycentre=120
    oldval=180 ;centre
    newval=180 ;of gauge
    ;
    ;---- draw the gauge
    filledcircle (xcentre,ycentre,79,ili9341_blue)
    circle (xcentre,ycentre,80,ili9341_white)
    filledcircle (xcentre,ycentre,9,ili9341_red)
    circle (xcentre,ycentre,10,ili9341_white)
    filledBox 104,180,216,200,ili9341_black ;rub out circle base
    line 108,179,212,179,ili9341_white
    needlecolour=ili9341_yellow
    draw_needle
    ;---- start of main program
    Do
      pulse_out=scale(ReadAD(portc.0),0,255,75,225) ;scale readad to pulse_out
      GLCDprint 140,154,str(pulse_out)+" ",ili9341_white
      newval = Scale(pulse_out, 75, 225, 45, 314) ;scale pulse_out to gauge
      if newval <> oldval  then
        erase_needle
        draw_needle
        oldval=newval
      end if
      pulseout portd.7,pulse_out 10us ;out to servo
       wait 5 ms ;best add this cos it's so fast
    Loop
    ;end of main program..that's it
    End
    ;Anobiums scale from to..looks complicateded...style
    function scale ( in l_map as word, in l_fromLow as integer, in l_fromHigh as integer, in l_toLow as integer, in l_toHigh  as integer) as integer
    
      dim l_syscalc as integer
      dim l_syscalcF as long
      l_syscalcf = 0
      repeat (l_toHigh   - l_toLow)
          l_syscalcf = l_syscalcf + ( l_map - l_fromLow )
      end Repeat
      l_syscalc = (  l_fromHigh - l_fromLow )
      scale = (l_syscalcf / l_syscalc) + l_toLow
    
    end function
    ;
    sub erase_needle
      angle=oldval
      needlecolour=ILI9341_blue
      needle ;erase needle
    end sub
    ;
    sub draw_needle
      angle=newval
      needlecolour=ILI9341_yellow
      needle ;draw needle
    end sub
    ;
    sub needle ;draws needle in needlecolour
      xstart=xcentre-30*sin(angle)/255
      ystart=ycentre+30*cos(angle)/255
      xend = xstart - radius  * sin (angle)/255
      yend = ystart + radius  * cos (angle)/255
      line (xstart,ystart,xend,yend,needlecolour)
    end sub
    
     

    Last edit: stan cartwright 2018-02-21
  • Anobium

    Anobium - 2018-02-21

    Nice.

    Have we resolved the ADC read issue? How did you resolve?

    I will move Scale to the distribution soon - once I have resolved a little issue I have. The final Scale code may look simplier but if it does not perform faster then the existing code - I will retain the existing method. Speed is of the essence in this conversion.

     
  • stan cartwright

    stan cartwright - 2018-02-21

    Resolved considering the 5V pin is not 5V but 4.67 on a dvm.
    How did I get by without "Scale"...sums for each time, ha!
    Whatever it's final name, it's a useful function.
    The display and pulseout was so easy..no brain needed.
    There is a slight error in my prog that 150 10 us should be vertical line mid display but it's not quite. maybe scaling twice instead of both scales from ad.
    just demoing gcb. well impressed with math handling speed.
    glcd graphics are very good to.
    It's a couldn't do that with picaxe program. :(
    ps the idea was the servo follows the display but different servos have different range

     

    Last edit: stan cartwright 2018-02-21
  • Anobium

    Anobium - 2018-02-27

    This is the final code that i will include in the next release.

    It is 3 times faster and now includes an optional calibrate parameter to enable a calibration offset to be applied.

    Enjoj]y

     

    Last edit: Anobium 2018-02-28
  • stan cartwright

    stan cartwright - 2018-03-04

    Not tested yet. Nice one.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.