code is here =====
1
'==========================================================================' IR motion sensor hack // analg input interpreted as digital
' Caluculates percentage of motion on a given time
' and sends it through xport to database
' Leah W. + Mouna A based on Tigoe analog in code.
'==========================================================================
DEFINE osc 4 'set the clock
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
' variables and constants for the serial port:
sensorVar var WORD 'raw value from the analog sensor
sendVar var word 'interpreted value for sensor (digital //counter)
tx var portc.4 ' to PC
rx var portc.5
xportTx var portc.6 ' to xport
xportRx var portc.7
inv9600 con 16468 ' baudmode for serin2 and serout2: 9600 8-N-1 inverted
non9600 con 84 ' baudmode for serin2 and serout2: 9600 8-N-1 non-inverted
lf con 10 'send linefeed
i var byte 'generic variable
'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
'=============================================================================
' blink an LED on startup + send hello to PC:
strat:
high portb.7
pause 1000
low portb.7
Serout2 tx, inv9600, ["hello world", 13, lf]
Pause 1000 ' Wait a second at startup
'=============================================================================
main:
sendVar = 0 'reset variable as counter
i = 0
' ===========Will take data from the sensor every 0.1 sec for 5 secs
for i = 0 to 50
adcin 0, sensorVar
if sensorVar = 0 then 'each time the sensor detects movement
sendVar = sendVar + 1 'increment the counter variable
endif
pause 100
Next i
sendVar = sendVar*2 ' multiply the counter (currently on 50 to get %)
' ===========Version to send to a PC
serout2 tx, inv9600, ["sendVar = ", DEC sendVar, lf]
' ===========Version to send to xport and database
' Check to make sure you send 2 bits of data:
' TEMORARY solutions until this can be done in php to unify to 2 digits only
SELECT CASE sendVar
CASE is <= 9
serout2 xportTx, non9600, ["GET /test/ir.php?ir=10", lf, lf]
CASE is >= 100
serout2 xportTx, non9600, ["GET /test/ir.php?ir=99", lf, lf]
CASE ELSE
serout2 xportTx, non9600, ["GET /test/ir.php?ir=", dec sendVar, lf, lf]
END SELECT
'GENERAL SEND DATA LINE IS:
'serout2 xportTx, non9600, ["GET /test/ir.php?ir=", dec sendVar, lf, lf]
pause 1000
goto main
2
'=============================================================================
' Photocell sensor // analg input sent
' through xport to database
' check issue of resistance to photocell in order to get values
' Leah W. + Mouna A based on Tigoe analog in code.
'=============================================================================
DEFINE OSC 4
DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 10
adcVar VAR WORD ' Create variable to store result
sendVar VAR Byte ' variable to /adcVar in 4
TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %10000010 ' Set up ADCON1
photopin var porta.1
xportTx var portd.1 ' transmit pin
xportRx var portd.0 ' receive pin
non9600 con 84 ' baudmode for serin2 and serout2: 9600 8-N-1 non-inverted
inv9600 con 16468
lf con 10 ' constant for ascii linefeed character
dataByte var byte ' used when we just want one byte
Pause 500 ' Wait .5 second
'====================================================================
main:
ADCIN 1, adcVar ' Read channel 1
sendVar = adcVar/4 'can be adjusted according to what is needed
'serout2 xporttx, non9600, ["GET /test/photo.php?photo=", DEC sendVar, lf. lf]
SELECT CASE sendVar
CASE is <= 9
serout2 xportTx, non9600, ["GET /test/photo.php?photo=10", lf, lf]
CASE is >= 100
serout2 xportTx, non9600, ["GET /test/photo.php?photo=99", lf, lf]
CASE ELSE
serout2 xportTx, non9600, ["GET /test/photo.php?photo=", dec sendVar, lf, lf]
END SELECT
Pause 5000
GoTo main
3
'=============================================================================
' Network Center //
' Reads sensor data from mySql database & sends it serially to output pics
' Leah W. + Mouna A.
'=============================================================================
DEFINE osc 4 'set the clock
xportTx var portc.4 ' transmit pin
xportRx var portc.5 ' receive pin
Tx var portc.6 ' to PC
Rx var portc.7
non9600 con 84 ' baudmode for serin2 and serout2: 9600 8-N-1 non-inverted
inv9600 con 16468
lf con 10 ' constant for ascii linefeed character
sensVar1 var byte 'input variable from ir
sensVar2 var byte 'inut variable from emf
'these 2 ports will tranfer data to their respective pics
TRISB = %00000000 ' Set PORTB to all output
TRISD = %00000000 ' Set PORTD to all output
Pause 500 ' Wait .5 second
'====================================================================
main:
Pause 100
'hello world to the PC for debugging
serout2 Tx, inv9600, ["hello baby!",10,13]
GOTO getsensor1
goto main
'==============IR SENSOR READ > stepper =======================================
getsensor1:
serout2 xportTx, non9600, ["GET /test/ir.php", lf, lf]
'wait for the text which immediately precedes the necessary info and then grab the rest
serin2 xportRx, non9600, 2000, getsensor1, [ wait("IR="), dec2 sensVar1 ]
Serout2 tx, inv9600, ["sensVar1 IR = ", dec sensVar1, lf, 13 ]
'this 10 value is to compensate for lack of accuracy in data transfer.
'if i get only one of the 2 digits, i am <10 and therefore lost data
'10 is = to 0 when sent out by sensors already
if sensVar1 >=10 then
portd = sensVar1
endif
pause 100
'==============EMF SENSOR READ > servo =======================================
getsensor2:
serout2 xportTx, non9600, ["GET /test/emf.php", lf, lf]
'wait for the text which immediately precedes the necessary info and then grab the rest
serin2 xportRx, non9600, 2000, getsensor2, [ wait("EMF="), dec2 sensVar2 ]
'keep2:
Serout2 tx, inv9600, ["sensVar2 emf=", dec sensVar2, lf, 13 ]
if sensVar2 >=10 then
portb = sensVar2
endif
pause 100
goto main
4
'===========================================================================
'STEPPER'
i var byte
speed var byte
inputvar var byte
Tx var portc.6 ' to PC
Rx var portc.7
non9600 con 84 ' baudmode for serin2 and serout2: 9600 8-N-1 non-inverted
inv9600 con 16468
lf con 10 ' constant for ascii linefeed character
'==========================================================
start:
steps VAR WORD
stepArray VAR BYTE(4)
clear
TRISD = %11110000
PORTD = %00000000
TRISB = %11111111
'PORTC = 0
Pause 1000
stepArray[0] = %00001010
stepArray[1] = %00000110
stepArray[2] =%00000101
stepArray[3] = %00001001
speed = 2
'=========================================================
main:
'our stepper is 3.2 degrees
for i=0 to 3
steps = steps + 1
portD = stepArray[steps //4]
pause 2
next i
'stepper can be pause to keep position
pause speed*10
'this forumla can be adjusted to accomodate value in and
'amplitude of result
inputvar = portb
inputvar = 100-inputvar
speed = (inputvar - 9)*(inputvar - 9)
'debugg
Serout2 tx, inv9600, ["speed", dec speed, lf, 13 ]
GoTo main
5
'=============================================================================
' Servo motor control // analg input interpreted as digital
' Move servo at constant 5 degrees at VARIABLE speed
' HERE: Analog Pot variate Pulse speed
' Leah W. + Mouna A based on Tigoe analog in code.
'=============================================================================
i var byte
refresh var word
pulseRange var word
pulseWidth var word
amp var word 'incremented amplitude
middle var word 'middle of clock face position
servoPin var portd.1
TRISB = %11111111 ' Set PORTB to all input
'====START==================================================================
start:
minPulse CON 50
maxPulse CON 250
middle = 170
amp = 50
refresh = 1
' set up a constant with the time between pulses:
refresh = 1 'will define the speed. basically, servo seems to like 1 to 30
' set an initial pulsewidth:
pulseWidth = middle
pulseRange = maxPulse - minPulse
'pause 500
for i=0 to i=500
low servoPin
Pulsout servopin, pulseWidth
pause refresh
next i
'====MAIN====================================================================
main:
for i = 0 to 15 'set the amplitude of angle mvt : approx 1 = 1 degree
'take the output pin low so we can pulse it high
Low servoPin
' pulse the pin
PulsOut servoPin, pulseWidth
' pause for as long as needed:
Pause refresh
pulseWidth = pulseWidth + 1
if pulsewidth >= maxPulse then
pulsewidth = middle
endif
next i
for i = 0 to 30
Low servoPin
PulsOut servoPin, pulseWidth
Pause refresh
pulseWidth = pulseWidth -1
if pulsewidth >= maxPulse then
pulsewidth = middle
endif
next i
for i = 0 to 15 'set the amplitude of angle mvt : approx 1 = 1 degree
'take the output pin low so we can pulse it high
Low servoPin
' pulse the pin
PulsOut servoPin, pulseWidth
' pause for as long as needed:
Pause refresh
pulseWidth = pulseWidth + 1
'the current ring slides constantly so we need to check if it reached the max left position
' and reset to middle when this happens
if pulsewidth >= maxPulse then
pulsewidth = middle
endif
next i
refresh = portb/2
GoTo main |