Analog in using RCTIME

The RCTIME command is used to obtain a varying number from the charge or discharge of a capacitor in a resistor-capacitor circuit. The PWM command produces a modulated pulse on an oputput pin to simulate a varying voltage. The FREQOUT command pulses a frequency on the given pin.


' there is a variable resistor-capacitor circuit on pin 7, a speaker circuit on pin 8, 
' and an LED on pin 0, and an LED on pin 3 for this program:

pwrLED    con    0
LED2    co   3
potPin    con    7
spkrPin    con    9
    
' declare variables:
potVar    var    word    ' a word-sized variable to hold the results of the RCTIME command
byteVar    var    byte    ' a byte-sized variable that will vary proprtional to potVar
output pwrLED        'light an LED on pin 8

high pwrLED'just so we know the stamp's working

main:
    high potPin'put voltage out to the capacitor
    pause 1    'hold a millisecond (0.4 ms on the BS2-SX)
    rctime potPin, 1, potVar    'time how long the cap takes to discharge
    debug ?potVar    
    byteVar = potVar /3        'convert potvar to a smaller number
    pwm LED2, byteVar, 10        'output a varying voltage based on bytevar
    freqout spkrPin,10,byteVar*50    'make a varying sound on the speaker
    debug  ?byteVar
goto main

Schematic: