The Sensing Beds
// Networked Objects, Spring 2003
the beds    the benches    schematics and code     resources

BX1a: RF & Senors

'Code for transmitter
'BX1a
dim inputBuffer(1 To 13) As Byte
dim outputBuffer(1 To 10) As Byte 

dim serInPinRF As Byte
dim serOutPinRF As Byte 

dim dat as byte 
dim datTwo as byte 
dim dataBuffer as byte

'define constants for area cases
 const	PAD1_ON			as byte = 10
 const	PAD1_OFF		as byte = 20
 const	PAD2_ON			as byte = 30
 const	PAD2_OFF		as byte = 40
 const	PAD3_ON			as byte = 50
 const	PAD3_OFF		as byte = 60

Sub Main() 
	call FlashMe(5)
	serInPinRF = 12
	serOutPinRF = 11

	call defineCom3(serInPinRF, serOutPinRF, bx1000_1000) 
	call openQueue(inputBuffer, 13)
	call openQueue(outputBuffer, 10)

	' open COM3:
	call openCom(3, 4800, inputBuffer, outputBuffer)

   dataBuffer=100

	do
		call delay(1.0)
		if getPin(13) = 1 then
			databuffer = PAD1_ON	
		else
			databuffer = PAD1_OFF
		end if
		call putQueue (outputBuffer, dataBuffer, 1)
        debug.print cstr(databuffer)

		call delay(1.0)
		if getPin(14) = 1 then
			databuffer = PAD2_ON	
		else
			databuffer = PAD2_OFF
		end if
		call putQueue (outputBuffer, dataBuffer, 1)
        debug.print cstr(databuffer)

		call delay(1.0)
		if getPin(15) = 1 then
			databuffer = PAD3_ON	
		else
			databuffer = PAD3_OFF
		end if
		call putQueue (outputBuffer, dataBuffer, 1)
        debug.print cstr(databuffer)

   	loop
end sub 

BX1: RF, Cobox & Firecracker
'BX1 CODE:
 '1) receives RF data from BX1a
 '2) sends RF data to BX2 through CoBox
 '3) receives data from BX2 through CoBox
 '4) sends BX2 data out through Firecracker

 '---- INITIALIZE ---------------
 'define board layout constants
 ' COM3 pins
 const 	serInPin 		as byte = 12
 const 	serOutPin 		as byte = 11

 ' Firecracker pins
 const	pin_DTR			as byte = 14
 const 	pin_RTS			as byte = 13

 dim 		databyte 			as byte 'used to be an integer cobox data
 dim		inData				as byte 'RF data
 dim 		gotabyte 			as boolean

  'define buffers for serial communication
 dim 		outputBuffer1(1 To 50) 	as byte 
 dim 		inputBuffer1(1 To 20) 		as byte
 dim 		outputBuffer3(1 To 50) 	as byte 
 dim 		inputBuffer3(1 To 20) 		as byte

 'define constants for area cases
 const	PAD1_ON			as byte = 10
 const	PAD1_OFF		as byte = 20
 const	PAD2_ON			as byte = 30
 const	PAD2_OFF		as byte = 40
 const	PAD3_ON			as byte = 50
 const	PAD3_OFF		as byte = 60

  'define constants for sensor data
 const	SENSOR1_ON			as byte = 10
 const	SENSOR1_OFF			as byte = 20
 const	SENSOR2_ON			as byte = 30
 const	SENSOR2_OFF			as byte = 40
 const	SENSOR3_ON			as byte = 50
 const	SENSOR3_OFF			as byte = 60

'----------------------------------------------
 Public Sub Main()
	call flashme(10)

	' set up COM3
	' COM1 already defined in BX OS
	call defineCom3(serInPin, serOutPin, bx1000_1000)

  	' open COM1
  	call openQueue(outputBuffer1, 50)
  	call openQueue(inputBuffer1, 20)
  	call openCom(1, 4800, inputBuffer1, outputBuffer1)

	' open COM3
  	call openQueue(outputBuffer3, 50)
  	call openQueue(inputBuffer3, 20)
  	call openCom(3, 9600, inputBuffer3, outputBuffer3)

	' set Firecracker to standby
	call FStandby()

	do		
		'check for serial data from CoBox
		call serialCheck()

		'check for serial data from RF
		call RFCheck()

      loop
 end sub

 ' ------------------------------------------------
 ' This checks for serial communication from the CoBox
 ' and calls the correct Firecracker command
  sub serialCheck()

	' Find out if anything is in the queue.
	gotaByte = statusQueue(inputBuffer3)
	'debug.print Cstr(gotaByte)
	' If there is data in the input buffer,
	' get the first byte of it:

	if (gotaByte = true) then
	' there's data available: read it here
		'debug.print "gotabyte!"; Cstr(dataByte)
		call getQueue(inputBuffer3, dataByte, 1)

		' cases for area 1 
		if (dataByte = PAD1_ON) then
			call send_A1_ON()
		end if

		if (dataByte = PAD1_OFF) then
			call send_A1_OFF()
		end if

		' cases for area 2
		if (dataByte = PAD2_ON) then
			call send_A2_ON()
		end if

		if (dataByte = PAD2_OFF) then
			call send_A2_OFF()
		end if

		' cases for area 3
		if (dataByte = PAD3_ON) then
			call send_A2_ON()
		end if

		if (dataByte = PAD3_OFF) then
			call send_A2_OFF()
		end if
	end if
 end sub

 ' -----------------------------------
 ' This checks for serial communication from RF
 ' and outputs the data through the CoBox
 sub RFCheck()
	gotaByte = statusQueue(inputBuffer1)

	' Find out if anything is in the queue.
	if gotabyte=true then
      		call getQueue(inputBuffer1, inData, 1)
			if inData=10 then
				call putPin(15,1)
				call putQueue(outputBuffer3, inData, 1)
			end if

			if inData=20 then
				call putPin(15,0)
			call putQueue(outputBuffer3, inData, 1)
			end if

			if inData=30 then
				call putPin(16,1)
			call putQueue(outputBuffer3, inData, 1)
			end if

			if inData=40 then
				call putPin(16,0)
			call putQueue(outputBuffer3, inData, 1)
			end if

			if inData=50 then
				call putPin(17,1)
			call putQueue(outputBuffer3, inData, 1)
			end if

			if inData=60 then
				call putPin(17,0)
			call putQueue(outputBuffer3, inData, 1)
			end if
	end if
	
'debuging tool
'	if getPin(18)=1 then
'		inData=10
'	else
'		inData=20
'	end if
'	debug.print Cstr(inData)
'	call putQueue(outputBuffer3, inData, 1)
 end sub

 ' ----- FIRECRACKER METHODS ---------
 '--------------------------------------------------------------------------------
 Sub sendHeader() 'header = 11010101 10101010 
   call send_1()
   call send_1()
   call send_0()
   call send_1()
   call send_0()
   call send_1()
   call send_0()
   call send_1()

   call send_1()
   call send_0()
   call send_1()
   call send_0()
   call send_1()
   call send_0()
   call send_1()
   call send_0()
 end sub

'--------------------------------------------------------------------------------
 Sub sendFooter() '10101101
   call send_1()
   call send_0()
   call send_1()
   call send_0()
   call send_1()
   call send_1()
   call send_0()
   call send_1()
 end sub

'--------------------------------------------------------------------------------
 Sub send_A1_ON() 'A1 ON = 01100000   00000000
   call sendHeader()

   'house code + key code 
   call send_0()
   call send_1()
   call send_1()
   call send_0()
   call send_0()
   call send_0()
   call send_0()
   call send_0()

   'function code
   call send_0()
   call send_0()
   call send_0()
   call send_0()
   call send_0()
   call send_0()
   call send_0()
   call send_0()

   call sendFooter()
 end sub

'--------------------------------------------------------------------------------
 Sub send_A1_OFF() 'A1 OFF = 01100000 00100000

   call sendHeader()

   'house code + key code 
   call send_0()
   call send_1()
   call send_1()
   call send_0()
   call send_0()
   call send_0()
   call send_0()
   call send_0()

   'function code
   call send_0()
   call send_0()
   call send_1()
   call send_0()
   call send_0()
   call send_0()
   call send_0()
   call send_0()

   call sendFooter()
 end sub

 ' -----------------------------------------
 Sub send_A2_ON() 'A2 ON 01100000 00010000

   call sendHeader()

   'house code + key code 
   call send_0()
   call send_1()
   call send_1()
   call send_0()
   call send_0()
   call send_0()
   call send_0()
   call send_0()

   'function code
   call send_0()
   call send_0()
   call send_0()
   call send_1()
   call send_0()
   call send_0()
   call send_0()
   call send_0()

   call sendFooter()
 end sub

'--------------------------------------------------------------------------------
 Sub send_A2_OFF() 'A2 OFF = 01100000 00110000 

   call sendHeader()

   'house code + key code 
   call send_0()
   call send_0()
   call send_1()
   call send_1()
   call send_0()
   call send_0()
   call send_0()
   call send_0()

   'function code
   call send_0()
   call send_0()
   call send_1()
   call send_0()
   call send_0()
   call send_0()
   call send_0()
   call send_0()

   call sendFooter()
 end sub

 ' -----------------------------------------
 Sub send_A3_ON() 'A3 ON 01100000 00001000 

   call sendHeader()

   'house code + key code 
   call send_0()
   call send_1()
   call send_1()
   call send_0()
   call send_0()
   call send_0()
   call send_0()
   call send_0()

   'function code
   call send_0()
   call send_0()
   call send_0()
   call send_0()
   call send_1()
   call send_0()
   call send_0()
   call send_0()

   call sendFooter()
 end sub

'--------------------------------------------------------------------------------
 Sub send_A3_OFF() 'A3 OFF 01100000 00101000 

   call sendHeader()

   'house code + key code 
   call send_0()
   call send_0()
   call send_1()
   call send_1()
   call send_0()
   call send_0()
   call send_0()
   call send_0()

   'function code
   call send_0()
   call send_0()
   call send_1()
   call send_0()
   call send_1()
   call send_0()
   call send_0()
   call send_0()
   call sendFooter()
 end sub
'--------------------------------------------------------------------------------
 Sub FStandby()
    call putPin(pin_DTR, 1)
    call putPin(pin_RTS, 1)
 end sub
'--------------------------------------------------------------------------------
Sub FReset()
    call putPin(pin_DTR, 0)
    call putPin(pin_RTS, 0)
    call waitABit()
    call FStandby()
    call waitABit()
End Sub
 '--------------------------------------------------------------------------------
 Sub send_1()
    call putPin(pin_DTR, 0)
    call waitABit()
    call putPin(pin_DTR, 1)
    call waitABit()
 end sub
'--------------------------------------------------------------------------------
 Sub send_0()
   call putPin(pin_RTS, 0)
   call waitABit()
   call putPin(pin_RTS, 1)
   call waitABit()
 end sub
'--------------------------------------------------------------------------------
Sub waitABit()
   call delay(0.0005)
end sub


BX2: Firecracker, Sensors & CoBox

'BX2 CODE:
 '1) receives sensor data through switches
 '2) sends sensor data to BX2 through CoBox
 '3) receives data from BX2 through CoBox
 '4) sends BX2 data out through Firecracker

 '---- INITIALIZE ---------------
 'define board layout constants
 ' COM3 pins
 const 	serInPin 			as byte = 12
 const 	serOutPin 			as byte = 11

 ' Firecracker pins
 const	pin_DTR			as byte = 8
 const 	pin_RTS			as byte = 9

 dim 		databyte 			as byte
 dim		inData			as byte
 dim 		gotabyte 			as boolean
 
 'define buffers for serial communication
 dim 		outputBuffer1(1 To 50) 	as byte 
 dim 		inputBuffer1(1 To 20) 		as byte
 dim 		outputBuffer3(1 To 50) 	as byte 
 dim 		inputBuffer3(1 To 20) 		as byte

 'define constants for area cases
 const	PAD1_ON			as byte = 10
 const	PAD1_OFF			as byte = 20
 const	PAD2_ON			as byte = 30
 const	PAD2_OFF			as byte = 40
 const	PAD3_ON			as byte = 50
 const	PAD3_OFF			as byte = 60
 
 'define constants for sensor data
 const	SENSOR1_ON			as byte = 10
 const	SENSOR1_OFF			as byte = 20
 const	SENSOR2_ON			as byte = 30
 const	SENSOR2_OFF			as byte = 40
 const	SENSOR3_ON			as byte = 50
 const	SENSOR3_OFF			as byte = 60

'----------------------------------------------

 Public Sub Main()
	
	'CoBoxes connect automatically, so there's no need
	'for a connect string variable 

	call flashme(10)
	call delay(1.0)

	' set up COM3
	call defineCom3(serInPin, serOutPin, bx1000_1000)

	' open COM3
  	call openQueue(outputBuffer3, 50)
  	call openQueue(inputBuffer3, 20)
  	call openCom(3, 9600, inputBuffer3, outputBuffer3)
	
	' set Firecracker to standby
	call FStandby()

	do		
		'check for serial data from CoBox
		call serialCheck()

		'check for input from cushion switches, since we
		'decided to save money by not buying another RF kit
		call switchCheck()
      loop
 end sub

 ' -----------------------------------
 ' This checks for serial communication from the CoBox
 ' and calls the correct Firecracker command
 sub serialCheck()

	' Find out if anything is in the queue.
	gotaByte = statusQueue(inputBuffer3)

	' If there is data in the input buffer,
	' get the first byte of it:
	if (gotaByte = true) then
	' there's data available: read it here
		call getQueue(inputBuffer3, dataByte, 1)
		debug.print "I hear"; cstr(databyte)

		' cases for area 1 
		if (dataByte = 10) then
			call send_B1_ON()
		end if

		if (dataByte = 20) then
			call send_B1_OFF()
		end if

		' cases for area 2
		if (dataByte = PAD2_ON) then
			call send_B2_ON()
		end if

		if (dataByte = PAD2_OFF) then
			call send_B2_OFF()
		end if

		' cases for area 3
		if (dataByte = PAD3_ON) then
			call send_B2_ON()
		end if

		if (dataByte = PAD3_OFF) then
			call send_B2_OFF()
		end if
	end if
 end sub

 ' -----------------------------------
 ' This checks for serial communication from RF
 ' and outputs the data through the CoBox
 sub switchCheck()

	'AREA 1
	if getPin(13) = 1 then
		inData = SENSOR1_ON
	end if

	if getPin(13) = 0 then
		inData = SENSOR1_OFF
	end if

	call putQueue(outputBuffer3, inData, 1)
	delay (0.1)
	
	'AREA 2
	if getPin(14) = 1 then
		inData = SENSOR2_ON
	end if
	if getPin(14) = 0 then
		inData = SENSOR2_OFF		
	end if

	call putQueue(outputBuffer3, inData, 1)
	delay (0.1)
				
	'AREA 3
	if getPin(15) = 1 then
		inData = SENSOR3_ON
	end if
	if getPin(15) = 0 then	
		inData = SENSOR3_OFF
	end if

	call putQueue(outputBuffer3, inData, 1)
	delay (0.1)

 end sub

 ' ----- FIRECRACKER METHODS ---------
 '--------------------------------------------------------------------------------

 Sub sendHeader() 'header = 11010101 10101010 
   call send_1()
   call send_1()
   call send_0()
   call send_1()
   call send_0()
   call send_1()
   call send_0()
   call send_1()

   call send_1()
   call send_0()
   call send_1()
   call send_0()
   call send_1()
   call send_0()
   call send_1()
   call send_0()
 end sub

'--------------------------------------------------------------------------------
 
 Sub sendFooter() '10101101
   call send_1()
   call send_0()
   call send_1()
   call send_0()
   call send_1()
   call send_1()
   call send_0()
   call send_1()
 end sub

'--------------------------------------------------------------------------------
 
 Sub send_B1_ON() 'B1 ON = 01110000    00000000

   call sendHeader()

   'house code + key code 
   call send_0()
   call send_1()
   call send_1()
   call send_1()
   call send_0()
   call send_0()
   call send_0()
   call send_0()

   'function code
   call send_0()
   call send_0()
   call send_0()
   call send_0()
   call send_0()
   call send_0()
   call send_0()
   call send_0()

   call sendFooter()
 end sub

'--------------------------------------------------------------------------------
 Sub send_B1_OFF() 'B1 OFF = 01110000   00100000

   call sendHeader()

   'house code + key code 
   call send_0()
   call send_1()
   call send_1()
   call send_1()
   call send_0()
   call send_0()
   call send_0()
   call send_0()

   'function code
   call send_0()
   call send_0()
   call send_1()
   call send_0()
   call send_0()
   call send_0()
   call send_0()
   call send_0()

   call sendFooter()
 end sub

 ' -----------------------------------------
 Sub send_B2_ON() 'B2 ON 01110000   00010000


   call sendHeader()

   'house code + key code 
   call send_0()
   call send_1()
   call send_1()
   call send_1()
   call send_0()
   call send_0()
   call send_0()
   call send_0()

   'function code
   call send_0()
   call send_0()
   call send_0()
   call send_1()
   call send_0()
   call send_0()
   call send_0()
   call send_0()

   call sendFooter()
 end sub

'--------------------------------------------------------------------------------
 Sub send_B2_OFF() 'B2 OFF  01110000   00110000

   call sendHeader()

   'house code + key code 
   call send_0()
   call send_1()
   call send_1()
   call send_1()
   call send_0()
   call send_0()
   call send_0()
   call send_0()

   'function code
   call send_0()
   call send_0()
   call send_1()
   call send_1()
   call send_0()
   call send_0()
   call send_0()
   call send_0()

   call sendFooter()
 end sub

 ' -----------------------------------------
 Sub send_B3_ON() 'B3 ON 01110000   00001000

   call sendHeader()

   'house code + key code 
   call send_0()
   call send_1()
   call send_1()
   call send_1()
   call send_0()
   call send_0()
   call send_0()
   call send_0()

   'function code
   call send_0()
   call send_0()
   call send_0()
   call send_0()
   call send_1()
   call send_0()
   call send_0()
   call send_0()

   call sendFooter()
 end sub

'--------------------------------------------------------------------------------
 Sub send_B3_OFF() 'B3 OFF 01110000   00101000 
   call sendHeader()

   'house code + key code 
   call send_0()
   call send_1()
   call send_1()
   call send_1()
   call send_0()
   call send_0()
   call send_0()
   call send_0()

   'function code
   call send_0()
   call send_0()
   call send_1()
   call send_0()
   call send_1()
   call send_0()
   call send_0()
   call send_0()

   call sendFooter()
 end sub


'--------------------------------------------------------------------------------

 Sub FStandby()
    call putPin(pin_DTR, 1)
    call putPin(pin_RTS, 1)
 end sub

'--------------------------------------------------------------------------------
 
 Sub FReset()
    call putPin(pin_DTR, 0)
    call putPin(pin_RTS, 0)
    call waitABit()
    call FStandby()
    call waitABit()
 End Sub

'--------------------------------------------------------------------------------

 Sub send_1()
    call putPin(pin_DTR, 0)
    call waitABit()
    call putPin(pin_DTR, 1)
    call waitABit()
 end sub

'--------------------------------------------------------------------------------

 Sub send_0()
   call putPin(pin_RTS, 0)
   call waitABit()
   call putPin(pin_RTS, 1)
   call waitABit()
 end sub

'--------------------------------------------------------------------------------

 Sub waitABit()
   call delay(0.0005)
 end sub

'----------------------------------------------------------------------------------

 sub flashme(byVal flashx as byte) 
 'create a function called "flashme", with one passed
 'byte variable (passed by its value) called "flashX"

	debug.print "restart"
     dim x as byte 'create a byte variable (just for
	 'inside this flashme function) called X 

             for x = 0 to flashx 'flash pin 25 on and off 
			 'flashX times...for example, if flashX = 5, 
			 'it will flash 5 times
               call putpin(25, 0)
               delay(0.1)
               call putpin(25, 1)
               delay(0.1)
             next

       call putpin(26, 0) 'once everything is finished,
	   'turn pin 26 (the green 'go' pin on)
 end sub

>> next


Schematics (pdf download)

BX1a

 

BX1

 

BX2