'{$STAMP BS2} '*** Listing 4 of 4: BS2-Max serial communication - Max turns LED on/off '*** BS2 sends switch state & photocell reading to Max *** 'SPST switch: 'one side to +5V (VDD) 'other side to BS2 pin 1 (input) + 10K resistor (BR-BL-OR) '10K resistor to ground (VSS) 'LED: 'short pin (cathode) to ground (VSS) 'long pin (anode) to 220 ohm resistor (RED-RED-BR) '220 ohm resistor to BS2 pin 8 (output) 'PHOTOCELL (or potentiometer) '+5V (VDD) to capacitor (0.1uF, usually labeled 104) 'capacitor to photocell & 220 ohm resistor (RED-RED-BR) 'other side of photocell to ground (VSS) '220 ohm resistor to pin 10 'BS2 serial cable: 'DB9 pin 2 to BS2 pin 2 (serial out) 'DB9 pin 3 to 22K resistor (RED-RED-BR) to BS2 (serial in) 'DB9 pin 5 to BS2 ground (VSS) switchPin var in1 'set variable to input pin 1 state switchPrev var bit 'bit for keeping track of previous pin state ledPin con 8 'output pin for LED potPin con 10 'input pin for photocell potResult var word 'photocell rcTime result potPrev var word 'previous photocell result serOutPin con 2 'output pin to serial cable serInPin con 3 'input pin from serial cable serData var word 'serial data baudMode con 16468 'baudmode for 9600 'These are the baudmode values for usable baud rates for the BS2: '16468 for 9600, 16416 for 19200, 16390 for 38400 start: debug "STAMP PROGRAM STARTING...",cr label1: 'pause 50 'don't need pause since serIn waits 50ms for input if switchPin <> switchPrev then switchChange label2: high potPin 'discharge capacitor pause 1 'for 1 ms rcTime potPin,1,potResult 'measure RC charge time if potResult <> potPrev then potChange label3: serIn serInPin,baudMode,50,label1,[DEC5 serData] 'branch to label1 if 200 ms pass if serData = 65001 then onLED 'number sent from Max to turn on LED if serData = 65000 then offLED 'number sent from Max to turn off LED debug "INPUT: ",dec serData,cr 'report any other number received serOut serOutPin,baudMode,["Received: ",dec serData,cr] 'report back to Max goto label1 switchChange: switchPrev = switchPin if switchPin = 1 then switchOn debug "SWITCH: ",dec switchPin,cr serOut serOutPin,baudMode,["switch_OFF",cr] 'send switch state to Max goto label2 switchOn: debug "SWITCH: ",dec switchPin,cr serOut serOutPin,baudMode,["switch_ON",cr] 'send switch state to Max goto label2 potChange: potPrev = potResult debug ? potResult serOut serOutPin,baudMode,[DEC potResult,cr] 'send photocell state to Max goto label3 onLED: high ledPin debug "LED ON ",dec serData,cr serOut serOutPin,baudMode,["LED_ON",cr] 'report back to Max goto label1 ' offLED: low ledPin debug "LED OFF ",dec serData,cr serOut serOutPin,baudMode,["LED_OFF",cr] 'report back to Max goto label1