Program about switch
boblai2010
Posts: 18
i want to set a basic stamp program for a switch , it is one switch to one mode, i have four mode,that will has four switch to four different mode.how can i set this program,Can somebody help me,Thank
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
' {$STAMP BS2}
' {$PBASIC 2.5}
DIRA=0
DIRB=15
DIRC=15
DIRD=15
MAINProgram:
loopcounter VAR Byte
FOR loopCounter = 0 TO 99
DEBUG HOME, "loopCounter =", DEC loopCounter
IF IN0=0 THEN· MODE1
IF IN1=0 THEN· MODE2
IF IN2=0 THEN· MODE3
IF IN4=0 THEN· MODE4
PAUSE 1000
NEXT
SLEEP 100
GOTO mainprogram
MODE1:
cmConstant CON 2257
cmDistance VAR Byte
time······ VAR Word
DO
· PULSOUT 15, 5
· PULSIN 15, 1,time
· cmDistance = cmConstant**time
· SEROUT 14, 84, [noparse][[/noparse]22, 12] ' Initialize LCD
· PAUSE 5
· SEROUT 14, 84, [noparse][[/noparse]"Distance Mode" ,CR, DEC3 cmDistance,"cm" ]
· PAUSE 100
· IF IN1=0 THEN· MODE2
· IF IN2=0 THEN· MODE3
· IF IN4=0 THEN· MODE4
LOOP
MODE2:
angle VAR Byte
x VAR Word
y VAR Word
DO
PULSIN 6, 1, x
PULSIN 7, 1, y
x = (x MIN 1921 MAX 3164) - 1921** 13369 - 126
y = (y MIN 1921 MAX 3164) - 1921 ** 13369 - 126
angle = -x ATN y
angle = angle*/363
IF angle>180 THEN angle=360-angle
IF angle=0 THEN FREQOUT 4,10000,2
SEROUT 14, 84, [noparse][[/noparse]22, 12] ' Initialize LCD
PAUSE 5
SEROUT 14, 84, [noparse][[/noparse]"Degree Mode" ,CR, "angle = " ,CLREOL,DEC angle,"deg" ]
PAUSE 100
IF IN0=0 THEN· MODE1
IF IN2=0 THEN· MODE3
IF IN4=0 THEN· MODE4
LOOP
MODE3:
lux VAR Word
h VAR Nib
z VAR Word
cnt VAR Word
y1 VAR Byte
y2 VAR Word
S0 PIN 8 'Sensitivity selection pins
S1 PIN 9
In PIN 10 'TSL230R output pin
DO
'
Program Start
LOW S0 'Set sensitivity pins (S0,S1) to x10
HIGH S1
'
Main program loop
h=$0014
y1 = cnt */ h
y2 = y1 * cnt */ h */ h
FOR z = 0 TO 2000
COUNT In, 100, cnt 'COUNT on P2 FOR 100 ms, store value in cnt
lux = cnt*/$39A-36-y2
DEBUG HOME , DEC5 lux,CR 'Display value of cnt as a decima
SEROUT 14, 84, [noparse][[/noparse]22, 12] ' Initialize LCD
PAUSE 5
SEROUT 14, 84, [noparse][[/noparse]"Lux Mode" ,CR,"Lux = " ,CLREOL,DEC lux,"lux" ]
IF IN0=0 THEN· MODE1
IF IN1=0 THEN· MODE2
IF IN4=0 THEN· MODE4
NEXT
LOOP
MODE4:
ShtData PIN 11 ' bi-directional data
Clock PIN 13
ShtTemp CON %00011 ' read temperature
ShtHumi CON %00101 ' read humidity
ShtStatW CON %00110 ' status register write
ShtStatR CON %00111 ' status register read
ShtReset CON %11110 ' soft reset
Ack CON 0
NoAck CON 1
No CON 0
Yes CON 1
ioByte VAR Byte ' data from/to SHT11
ackBit VAR Bit ' ack/nak from/to SHT11
toDelay VAR Byte ' timeout delay timer
timeOut VAR Bit ' timeout status
soT VAR Word ' temp counts from SHT11
tC VAR Word ' temp - Celcius
status VAR Byte
'Initialize:
GOSUB SHT_Connection_Reset ' reset device connection
PAUSE 250 ' let DEBUG window open
Main:
DO
SEROUT 14, 84, [noparse][[/noparse]22, 12] ' Initialize LCD
PAUSE 5
SEROUT 14, 84, [noparse][[/noparse]"Temp Mode" ,CR,"tC = " ,CLREOL, DEC tC,· "C"]
GOSUB SHT_Measure_Temp
PAUSE 1000
IF IN0=0 THEN· MODE1
IF IN1=0 THEN· MODE2
IF IN2=0 THEN· MODE3
LOOP
SHT_Connection_Reset:
SHIFTOUT ShtData, Clock, LSBFIRST, [noparse][[/noparse]$FFF\9]
SHT_Start:
INPUT ShtData ' let pull-up take high
LOW Clock
HIGH Clock
LOW ShtData
LOW Clock
HIGH Clock
INPUT ShtData
LOW Clock
RETURN
SHT_Measure_Temp:
GOSUB SHT_Start ' alert device
ioByte = ShtTemp ' temperature command
GOSUB SHT_Write_Byte ' send command
GOSUB SHT_Wait ' wait for measurement
ackBit = Ack ' another read follows
GOSUB SHT_Read_Byte ' get MSB
soT.HIGHBYTE = ioByte
ackBit = NoAck ' last read
GOSUB SHT_Read_Byte ' get LSB
soT.LOWBYTE = ioByte
tC = soT ** $1999 - 400 ' convert to tenths C
RETURN
SHT_Write_Status:
GOSUB SHT_Start ' alert device
ioByte = ShtStatW
GOSUB SHT_Write_Byte ' send command
ioByte = status
GOSUB SHT_Write_Byte
RETURN
SHT_Read_Status:
GOSUB SHT_Start ' alert device
ioByte = ShtStatW ' write to status reg cmd
GOSUB SHT_Read_Byte ' send command
ackBit = NoAck ' only one byte to read
GOSUB SHT_Read_Byte
RETURN
SHT_Write_Byte:
SHIFTOUT ShtData, Clock, MSBFIRST, [noparse][[/noparse]ioByte] ' send byte
SHIFTIN ShtData, Clock, LSBPRE, [noparse][[/noparse]ackBit\1] ' get ack bit
RETURN
SHT_Read_Byte:
SHIFTIN ShtData, Clock, MSBPRE, [noparse][[/noparse]ioByte] ' get byte
SHIFTOUT ShtData, Clock, LSBFIRST, [noparse][[/noparse]ackBit\1] ' send ack bit
INPUT ShtData ' release data line
RETURN
SHT_Wait:
INPUT ShtData ' data line is input
timeOut = No ' assume no timeout
FOR toDelay = 1 TO 250 ' wait ~1/4 second
IF (ShtData = 0) THEN EXIT
PAUSE 1
NEXT
IF (toDelay = 250) THEN timeOut = Yes ' loop completed = timeout
RETURN
SHT_Soft_Reset:
GOSUB SHT_Connection_Reset ' reset the connection
ioByte = ShtReset ' reset command
ackBit = NoAck ' only one byte to send
GOSUB SHT_Write_Byte ' send it
PAUSE 11 ' wait at least 11 ms
RETURN
Heater_On:
status = %00000100 ' heater bit = On
GOSUB SHT_Write_Status
RETURN
Heater_Off:
status = %00000000 ' heater bit = Off
GOSUB SHT_Write_Status
RETURN
i only want to press one button display one mode ,i can't do it
if some where in your code you need to select a different mode the just rescan the select mode
Scan_Mode:
FOR loopCounter = 0 TO 99
IF IN0=0 THEN· MODE1
IF IN1=0 THEN· MODE2
IF IN2=0 THEN· MODE3
IF IN4=0 THEN· MODE4
NEXT
PAUSE 1000
RETURN
Then any time you need to select a different mode· then use GOSUB Scan_Mode
I hope this help
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·Now wanting to learn Spin· Thanks for any··that you may have and all of your time finding them
·
·
·
·
Sam