> Introduction       > Technology       > Code       > Demos       > Additional Documentation     

 

CODE > PicBasicPro

DEFINE OSC 20 'increase response time to switches & Bluetooth comunication

TRISB = %00000000 'open all B ports
TRISC = %01001111 'ports C0, C1, C2, C3 = input; C6(TX) is output?; C4,C5, C7=output

y var byte
x var byte
duty var byte
counter var word

counter = 0

' initial setup of the board
blinky
for x = 0 to 3
high portc.4
pause 100
low portc.4
pause 100
next

gosub setup 'go check if switches are on first (devices snapped in)

' in the main loop the bluetooth gets bits from the phone via RX pin
' tells portc.5 to go HIGH to confirm communication
' all the blinks are then performed
' also in this we call on the LEDs to tell us when any of the switches are switched off

main:
if portc.0 == 0 then
gosub panic3
endif

if portc.1 == 0 then
gosub panic1
endif

if portc.2 == 0 then
gosub panic2
endif

' tells chip to wait 1000 milliseconds for serial
' if nothing, go to "check" function and do something with "x"
' this way, program isn't hanging due to lack of bluetooth conversation

serin2 portc.7, 84, 1000, check, [x]
SEROUT2 portc.6, 16468, ["im in!", 13,10]
if x == 114 then ' 114 is "r"
SEROUT2 portc.6, 16468, ["Hello World!", 13,10]
endif
gosub b4
gosub b3
gosub b2
gosub b1
gosub c5
gosub c4

' setup a counter so we can time the cycles approx
' when counter hits 50 then the setup code is executed

counter = counter+1
if counter >= 50 then
SEROUT2 portc.6, 16468, ["counting!", 13,10]
gosub setup
pause 100
counter = 0
endif

goto main

'///////////////////////////////////////////////////////////////////////////////////
' this is when the switches are on and happy - devices are snapped in

glow1:
for duty = 0 to 255
pwm portb.7, duty, 5
next
return

glow2:
for duty = 0 to 255
pwm portb.6, duty, 5
next
return

glow3:
for duty = 0 to 255
pwm portb.5, duty, 5
next
return

'/////////////////////////////////////////////////////////////////////////
' this is when the switches hit an off state - device is removed

panic1:
for x = 0 to 3
high portb.7
pause 100
low portb.7
pause 100
next
return

panic2:
for x = 0 to 3
high portb.6
pause 100
low portb.6
pause 100
next
return

panic3:
for x = 0 to 3
high portb.5
pause 100
low portb.5
pause 100
next
return

'////////////////////////////////////////////////////////////////////////////////////////////////////////////////
' this happens in the beginning of the code and then is revisted everytime the counter hits 50

setup:
if portc.1 = 1 then
gosub glow1
else
if portc.1 = 0 then
gosub panic1
endif
endif

if portc.2 = 1 then
gosub glow2
else
if portc.2 = 0 then
gosub panic2
endif
endif

if portc.0 = 1 then
gosub glow3
else
if portc.0 = 0 then
gosub panic3
endif
endif
return

'/////////////////////////////////////////////////////////////////
' all the serin blinks, confirming bluetooth conversation

b4:
high portb.4
pause 100
low portb.4
pause 100
return

b3:
high portb.3
pause 100
low portb.3
pause 100
return

b2:
high portb.2
pause 100
low portb.2
pause 100
return

b1:
high portb.1
pause 100
low portb.1
pause 100
return

c5:
high portc.5
pause 100
low portc.5
pause 100
return

c4:
high portc.4
pause 100
low portc.4
pause 100
return

'////////////////////////////////////////////////////////////////////////////////////////////
' come here when there is a hang in the SERIN2 (no bluetooth conversation)
' this will repeat until bluetooth resumes, to let you know it's waiting

check:

high portb.4
pause 500
low portb.4
pause 500

goto main

 

back to top