PDA

View Full Version : storing data and averaging...

grkblood13
02-06-2007, 03:17 AM
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:
RETURN

crunch2:
RETURN

crunch3:
RETURN

crunch4:
RETURN

crunch5:
RETURN

Mike Green
02-06-2007, 03:29 AM

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 Green
02-06-2007, 03:32 AM
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[i-1] = inl
pause 1000
next
total = 0
for i = 1 to 5
total = total + value[i-1]
next
total = total / 5
write location,total
end

Mike Green
02-06-2007, 03:35 AM
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
total = total + value
next i
write location,total/5
end

The average is in byte #0.
The values themselves begin at byte #1.

grkblood13
02-06-2007, 03:42 AM
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).

grkblood13
02-06-2007, 03:49 AM
thanks, didnt see the other replies, i think we can figure it out.

grkblood13
02-06-2007, 03:57 AM
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 Green
02-06-2007, 04:05 AM
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.