Industrial Controls Edge Triggering
Earl Foster
Posts: 185
I am working through the Industrial Controls 1.1 book program 2.4 on page 40-41.· The count never changes like it is suppose too when I push the main push button.· I have looked over the code several times and can not find anything wrong.· Can someone help please?· Code attached.
'Program 2.4 Switch Edge Detection
'Count and display the number of closures of PB1
'Reset total count with a closure of PB2
' {$STAMP BS2}
' {$PBASIC 2.5}
'---Setup StampPlot
PAUSE 500
DEBUG "!TITL Counting Challenge", CR· 'Titles the StampPlot screen
DEBUG "!TMAX 50", CR····················· 'Sets the plot time (seconds)
DEBUG "!PNTS 300", CR···················· 'Sets the number of data points
DEBUG "!AMAX 20", CR····················· 'Sets the vertical axis (counts)
DEBUG "!MAXR", CR·························· 'Reset after reaching max data points
'---Declare variables
INPUT 1
INPUT 2
pb1 VAR IN1
pb2 VAR IN2
flag1 VAR Bit······················ 'Flag for PB1
flag2 VAR Bit······················ 'Flag for PB2
counts VAR Word·················'word variable to hold count
'---Initialize variables
flag1 = 0
flag2 = 0
counts = 0
main:
· PAUSE 50
· DEBUG "!USRS Total Count = ", DEC counts, CR···· 'Display total counts
· DEBUG DEC counts, CR···························· 'Show counts on analog trace
· DEBUG IBIN pb1, BIN pb2, CR
· IF pb1 = 1 THEN count_it························· 'If pressed, count and display
· flag1 = 0·············································· ·'If not pressed, reset flag to 0
· IF IN2 = 0 THEN Clear_it·························· 'If pb2 pressed, clear counts to 0
· flag2 = 0
GOTO main
Count_it:
·· IF (pb1 = 0) OR (flag1 = 0) THEN main·········· 'If no longer pressed or the
································································ 'flag is set, skip
·· counts = counts +1····································'increment counts
·· flag1 = 1··················································'once action executes, set flag to 1
GOTO main
clear_it:
·· IF (IN2 = 1) OR (flag2 = 1) THEN main
·· counts = 0
·· flag2 = 1
·· DEBUG "Counter Cleared. total Count = ", DEC counts, CR
GOTO main
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"Don't ask yourself what the world needs - ask yourself what makes you come alive, and then go do it." - Harold Thruman Whitman
'Program 2.4 Switch Edge Detection
'Count and display the number of closures of PB1
'Reset total count with a closure of PB2
' {$STAMP BS2}
' {$PBASIC 2.5}
'---Setup StampPlot
PAUSE 500
DEBUG "!TITL Counting Challenge", CR· 'Titles the StampPlot screen
DEBUG "!TMAX 50", CR····················· 'Sets the plot time (seconds)
DEBUG "!PNTS 300", CR···················· 'Sets the number of data points
DEBUG "!AMAX 20", CR····················· 'Sets the vertical axis (counts)
DEBUG "!MAXR", CR·························· 'Reset after reaching max data points
'---Declare variables
INPUT 1
INPUT 2
pb1 VAR IN1
pb2 VAR IN2
flag1 VAR Bit······················ 'Flag for PB1
flag2 VAR Bit······················ 'Flag for PB2
counts VAR Word·················'word variable to hold count
'---Initialize variables
flag1 = 0
flag2 = 0
counts = 0
main:
· PAUSE 50
· DEBUG "!USRS Total Count = ", DEC counts, CR···· 'Display total counts
· DEBUG DEC counts, CR···························· 'Show counts on analog trace
· DEBUG IBIN pb1, BIN pb2, CR
· IF pb1 = 1 THEN count_it························· 'If pressed, count and display
· flag1 = 0·············································· ·'If not pressed, reset flag to 0
· IF IN2 = 0 THEN Clear_it·························· 'If pb2 pressed, clear counts to 0
· flag2 = 0
GOTO main
Count_it:
·· IF (pb1 = 0) OR (flag1 = 0) THEN main·········· 'If no longer pressed or the
································································ 'flag is set, skip
·· counts = counts +1····································'increment counts
·· flag1 = 1··················································'once action executes, set flag to 1
GOTO main
clear_it:
·· IF (IN2 = 1) OR (flag2 = 1) THEN main
·· counts = 0
·· flag2 = 1
·· DEBUG "Counter Cleared. total Count = ", DEC counts, CR
GOTO main
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"Don't ask yourself what the world needs - ask yourself what makes you come alive, and then go do it." - Harold Thruman Whitman
Comments
-Martin
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
StampPlot - GUI and Plotting, and XBee Wireless Adapters
Southern Illinois University Carbondale, Electronic Systems Technologies
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"Don't ask yourself what the world needs - ask yourself what makes you come alive, and then go do it." - Harold Thruman Whitman
IF (pb1 = 0) OR (flag1 = 0) THEN main
to
IF (pb1 = 0) OR (flag1 = 1) THEN main
It doesn't make sense the 1st way since it will be 0 initially and never allow execution to continue.
-Martin
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
StampPlot - GUI and Plotting, and XBee Wireless Adapters
Southern Illinois University Carbondale, Electronic Systems Technologies