Shop OBEX P1 Docs P2 Docs Learn Events
Average an ADC Output over a Time Interval (BS2) — Parallax Forums

Average an ADC Output over a Time Interval (BS2)

MtnFlyerMtnFlyer Posts: 28
edited 2009-05-18 04:07 in BASIC Stamp
Greetings all,

I'm trying to average an ADC output (8 bit) in the following way:

When signal goes above a threshold value, say %00010000, THEN
- continue sampling ADC
- start calculating a running sum of ADC values·for the next 0.1s
- divide the sum by the number of samples to get an average
- exit the loop and print the result

I'm having a hard time figuring out how to specify the time interval in PBASIC.· Thanks for the help!

Jim

Comments

  • Clive WakehamClive Wakeham Posts: 152
    edited 2009-05-18 02:28
    The only way without a RTC is to put the lot inside a loop but you need to work out how long a loop takes to do the loop etc.

    for example;

    if inA > %00010000 then
    y = 1 ' samples
    a = inA ' storage variable
    c = inA ' total variable
    for x = 0 to 100 'guess work of time
    b = inA 'temp variable
    if b = a then next elseif
    y = y + 1 ' amount of samples
    c = c + b ' add samples together
    a = b ' next check
    next
    a = c / y ' result
  • MtnFlyerMtnFlyer Posts: 28
    edited 2009-05-18 03:56
    Clive, thanks for getting back to me. I understand your code, but I'm a little confused by the "if b = a" check. Is that to make sure the value being output by the ADC changed?
  • Clive WakehamClive Wakeham Posts: 152
    edited 2009-05-18 04:07
    Yes. I thought I read your original post as you only wanted to record samples that changed otherwise if the sample did not change then the program might only record one value multiple times. (Unless thats what you want).
Sign In or Register to comment.