'FlexSpace, v 0.0000001
'03/05/05
'Saranont Limpananont
DEFINE OSC 4
DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS
log var word
start var word
form var word
sleepingcount var word
transformcount var word
dbstate var word
'set switch
input portd.2 'switch A
input portd.3 'switch B
input portc.4 'switch C
input portc.3 'switch Login
output portb.3 'wake up LED
XportTX var portd.0
XportRX var portd.1
' data coming in the serial port:
dataByte var byte
non9600 con 84 ' baudmode for serin2 and serout2: 9600 8-N-1 non-inverted
lf con 10 ' constant for ascii linefeed character
cr con 13 ' constant for ascii carriage return character
clear ' clear all variables
'variables for ADC:
ADCvar VAR WORD ' Create variable to store result
TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %10000010 ' Set PORTA analog and right justify result
'TRISB = %00000000 ' set all the pins of PORTB to output
pause 8000
login:
if (portc.3 = 1)then
serout2 portc.6, 16468, ["login FlexSpace", 13, 10]
serout2 XportTX, non9600, ["login FlexSpace", 10]
pause 2000
serout2 portc.6, 16468, ["set_info FlexSpace [version 0.00001] ", 13, 10]
serout2 XportTX, non9600, ["set_info FlexSpace [version 0.00001]", 10]
log = 1
endif
while log <> 1
goto login
wend
pause 2000
goto main
'+++++++++Main Program++++++++++
main:
start = 0 'restart state before checking each input in switchState
pause 500
'check switchs
GOSUB spaceState
'check timing in each activity
gosub timeState
if (sleepingcount = 1500) and (portc.7 <>0)then 'this part has not finished yet.
adcin 0, ADCvar
serin2 XportRx, non9600, [WAIT("4"), databyte]
pause 200
serout2 XportTx, non9600, ["Wake Up!", 10]
goto wakeup
endif
gosub logout
goto main
'++++++++++SubProgram+++++++++
spaceState:
if (portd.2 = 1) and (portd.3 = 1)and (portc.4 <>1)then 'Switch A and Switch B
if start = 0 then
if form <> 1 then 'If the current form is Dining, do not repeat the statement
serout2 portc.6, 16468, ["send all Flex is transformed to be a Living Space", 13, 10]
serout2 XportTX, non9600, ["send all Flex is transformed to be a Living Space", 10]
pause 500
serout2 portc.6, 16468, ["send all Come and join me, Let's hang out!", 13, 10]
serout2 XportTX, non9600, ["send all Come and join me, Let's hang out!", 10]
if portb.2 =1 then
low portb.2
endif
if portb.3 =1 then
low portb.3
endif
if portb.4 =1 then
low portb.4
endif
pause 500
High portb.1
start = 1
form = 1 'set the form to be Living space
endif
endif
endif
if (portd.2 = 1) and (portd.3 <> 1) and (portc.4 <>1)then 'switch A only
if start = 0 then
if form <> 2 then 'If the current form is Working, do not repeat the statement
serout2 portc.6, 16468, ["send all Flex is transformed to be a Working Space", 13, 10]
serout2 XportTX, non9600, ["send all Flex is transformed to be a Working Space", 10]
pause 500
serout2 portc.6, 16468, ["send all Do not disturb please, I am Working.", 13, 10]
serout2 XportTX, non9600, ["send all Do not disturb please, I am Working.", 10]
if portb.1 =1 then
low portb.1
endif
if portb.3 =1 then
low portb.3
endif
if portb.4 =1 then
low portb.4
endif
pause 500
High portb.2
start = 1
form = 2 'set the number of form to be Working space
endif
endif
endif
if(portd.2 <> 1) and(portd.3 = 1) and (portc.4 <>1)then 'switch B only
if start = 0 then
if form <> 3 then 'If the current form is Sleeping, do not repeat the statement
serout2 portc.6, 16468, ["send all Flex is transformed to be a Sleeping Space", 13, 10]
serout2 XportTX, non9600, ["send all Flex is transformed to be a Sleeping Space", 10]
pause 500
serout2 portc.6, 16468, ["send all I am sleepy.", 13, 10]
serout2 XportTX, non9600, ["send all I am sleepy.", 10]
if portb.1 =1 then
low portb.1
endif
if portb.2 =1 then
low portb.2
endif
if portb.4 =1 then
low portb.4
endif
pause 500
High portb.3
pause 500
start = 1
form = 3 'set the number of form to be Sleeping space
sleepingcount = 0
endif
endif
endif
if (portd.2 = 1)and(portd.3 <> 1) and(portc.4 = 1)then 'switch C and A
if start = 0 then
if form <> 4 then 'If the current form is Relaxing, do not repeat the statement
serout2 portc.6, 16468, ["send all Flex is transformed to be a Lounging Space.", 13, 10]
serout2 XportTX, non9600, ["send all Flex is transformed to be a Lounging Space.", 10]
pause 500
serout2 portc.6, 16468, ["send all It's time to relax.", 13, 10]
serout2 XportTX, non9600, ["send all It's time to relax.", 10]
if portb.1 =1 then
low portb.1
endif
if portb.2 =1 then
low portb.2
endif
if portb.3 =1 then
low portb.3
endif
pause 500
High portb.4
pause 500
start = 1
form = 4 'set the number of form to be Sleeping space
endif
endif
endif
'Transforming
if (portd.2 <> 1) and (portd.3 <> 1) and (portc.4 <>1)then 'no switch
if start = 0 then
if form <> 5 then
serout2 portc.6, 16468, ["send all Transforming....", 13, 10]
serout2 XportTX, non9600, ["send all Transforming....", 10]
pause 500
start = 1
form = 5 '5 = transforming
transformcount = 0
endif
endif
endif
return
timeState:
'Sleep
if form = 3 then
sleepingcount = sleepingcount + 1
endif
if sleepingcount = 5000 then
pause 1000
serout2 portc.6, 16468, ["send all zzzzzz", 13, 10]
serout2 XportTX, non9600, ["send all zzzzzz", 10]
Pause 500
serout2 portc.6, 16468, ["set_info send me '4' to wake me up ^__^ ", 13, 10]
serout2 XportTX, non9600, ["set_info press me '4' to wake me up ^__^ ", 10]
endif
'transforming....
if form = 5 then
transformcount = transformcount + 1
endif
if transformcount = 5000 then
pause 500
serout2 portc.6, 16468, ["send all I am still transforming", 13, 10]
serout2 XportTX, non9600, ["send all I am still transforming", 10]
endif
if transformcount = 8000 then
pause 500
serout2 portc.6, 16468, ["send all I am still transforming", 13, 10]
serout2 XportTX, non9600, ["send all I am still transforming", 10]
endif
if transformcount = 9000 then
pause 500
serout2 portc.6, 16468, ["send all FlexSpace might be broken already", 13, 10]
serout2 XportTX, non9600, ["send all FlexSpace might be broken already", 10]
Pause 500
serout2 portc.6, 16468, ["send all help!", 13, 10]
serout2 XportTX, non9600, ["send all help!", 10]
goto wakeup
endif
return
'Wakeup
wakeup:
while (portd.2=1) or (portd.3=1) or (portc.4=1)
goto main
wend
high portb.7
pause 300
low portb.7
Pause 200
goto wakeup
logout:
if (portc.3 = 0)then
if log <> 2 then
serout2 portc.6, 16468, ["logout", 13, 10]
serout2 XportTX, non9600, ["logout", 10]
log = 2
endif
endif
while log = 2
pause 2000
goto login
wend
return
|