« Basic Stamp 2 code to switch lamp on and off | Main | Microcontroller Madness: Class Pix 9/25/03 »

September 25, 2003

Basic Stamp 2 code for communicating with MaxMSP

'{$STAMP BS2}

'BASIC STAMP 2 FIRST PROJECT - COMMUNICATION WITH MAX/MSP

'WHAT SHOULD HAPPEN:
'When a serial message is received on pin 2, the light is turned on or off.
'When the switch is opened or closed, a serial message is sent out on pin 3.

'NOTE: after downloading this program, remove the programming cable from the
'Mac serial port, and plug in the custom serial cable to communicate with Max/MSP.
'Also, you will have to quit out of MacBS2 (or Virtual PC) before you run Max/MSP.

'DECLARE INS & OUTS
input 0 'switch
output 1 'relay
input 2 'BS2 serial receive, mac TXD (transmit)
output 3 'BS2 serial transmit, mac RXD (recieve)

'DECLARE VARIABLES
prevSwitchState var bit
serButtonByte var byte 'serial input from Mac - 1 for button down, 2 for button up

'INITIALIZE VARIABLES
prevSwitchState = 0 'start with switch off

'MAIN LOOP

TOP:
serIn 2,16468,50,CHECKSWITCH,[serButtonByte] 'branch to CHECKSWITCH if 50 ms pass
if serButtonByte = 2 then LIGHTON
if serButtonByte = 1 then LIGHTOFF

CHECKSWITCH:
if in0 <> prevSwitchState then ONOROFF 'branch if switch state changed
goto TOP

LIGHTON:
high 1 'turn on light
'debug "Light ON",cr
serOut 3,16468,["light_ON_"] 'report light on
goto TOP

LIGHTOFF:
low 1 'turn off light
'debug "Light OFF",cr
serOut 3,16468,["light_OFF"] 'report light off
goto TOP

ONOROFF:
if in0 = 1 then SWITCHON 'if 1 then switch on
if in0 = 0 then SWITCHOFF 'if 0 then switch off
goto TOP

SWITCHON:
prevSwitchState = 1
'debug "Switch ON",cr
serOut 3,16468,["switch_ON"] 'report switch on
goto TOP

SWITCHOFF:
prevSwitchState = 0
'debug "Switch OFF",cr
serOut 3,16468,["switchOFF"] 'report switch off
goto TOP

Posted by at September 25, 2003 08:45 PM

Comments

Could someone post what flavor of PicBasic (2.0 or 2.5) is being used for the posted code? I'm using Bsic Editor v2.0 (PicBasic v2.5) and there seems to be some syntax differences that allows to compile but doesn't work properly.

Thanks!

Posted by: danielle at December 12, 2003 10:30 AM

Post a comment

Thanks for signing in, . Now you can comment. (sign out)

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)


Remember me?