Shop OBEX P1 Docs P2 Docs Learn Events
storing data and averaging... — Parallax Forums

storing data and averaging...

grkblood13grkblood13 Posts: 31
edited 2007-02-05 20:05 in BASIC Stamp
hey, im trying to take 5 seperate samples and average them together. im not exactly sure how to save my samples and how to retrieve them. this is the coding that i have so far. im hoping to get the average of the samples and save it to the EEPROM. I could most likely figure out the mathematical part to this if some1 could help me figure out how to save and retrieve all of the sample values.

' {$STAMP BS2p}
' {$PBASIC 2.5}
reps VAR Nib
FOR reps = 1 TO 5
value VAR Byte
value=INL

PAUSE 1000
IF reps = 1 THEN GOSUB crunch1
IF reps = 2 THEN GOSUB crunch2
IF reps = 3 THEN GOSUB crunch3
IF reps = 4 THEN GOSUB crunch4
IF reps = 5 THEN GOSUB crunch5

NEXT
END

crunch1:
addr1 VAR Byte
WRITE addr1, value
RETURN

crunch2:
addr2 VAR Byte
WRITE addr2, value
RETURN

crunch3:
addr3 VAR Byte
WRITE addr3, value
RETURN

crunch4:
addr4 VAR Byte
WRITE addr4, value
RETURN

crunch5:
addr5 VAR Byte
WRITE addr5, value
RETURN

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-02-05 19:29
    How about:
    location con 0
    i var nib
    total var word
    
    total = 0
    for i = 1 to 5
      total = total + inl
      pause 1000
    next
    total = total / 5
    write location,total
    end
    
  • Mike GreenMike Green Posts: 23,101
    edited 2007-02-05 19:32
    If you actually want to save the samples:
    location con 0
    i var nib
    value var byte
    total var word
    
    for i = 1 to 5
      value[noparse][[/noparse]i-1] = inl
      pause 1000
    next
    total = 0
    for i = 1 to 5
      total = total + value[noparse][[/noparse]i-1]
    next
    total = total / 5
    write location,total
    end
    
    
  • Mike GreenMike Green Posts: 23,101
    edited 2007-02-05 19:35
    If you want to save the samples to EEPROM:
    location con 0
    total var word
    value var byte
    
    for i = 1 to 5
      write location+i,inl
      pause 1000
    next i
    
    total = 0
    for i = 1 to 5
      read location+i,value
      total = total + value
    next i
    write location,total/5
    end
    
    


    The average is in byte #0.
    The values themselves begin at byte #1.
  • grkblood13grkblood13 Posts: 31
    edited 2007-02-05 19:42
    thanks for the quick reply. but where is this saving to and how can we access this value again in the future. in the grand scheme of things what we're trying to do is make a scale that weighs and stores the weight to one of three "bins" depending on what the weight total of the bin is (ex. the next weight would be added to the lowest bin and so on until the bins were full and close to equal).
  • grkblood13grkblood13 Posts: 31
    edited 2007-02-05 19:49
    thanks, didnt see the other replies, i think we can figure it out.
  • grkblood13grkblood13 Posts: 31
    edited 2007-02-05 19:57
    we ran into a snag, how do we display a value that is stored in an address? For example, how would we read the variable for repitition number 3? and where would the location be on the eeprom? thanks
  • Mike GreenMike Green Posts: 23,101
    edited 2007-02-05 20:05
    Look at the sample program I posted and read the description of the READ and WRITE statements in the PBasic manual. There are small demo programs there as well. If you have questions afterwards, please post them.
Sign In or Register to comment.