Help with project.
userjyjy
Posts: 30
in BASIC Stamp
How to get counter to count the number of cycles from a pushbutton instead of how long the pushbutton is being pressed. [img][/img]
Below is the project that I'm working on.
Below is the project that I'm working on.
Comments
Sometimes a flowchart or even a simple description of the steps involved will help you understand what your program needs to do. How would you count the number of times a pushbutton is pressed? Remember that in some problems, you also have to look at when a pushbutton is released. For this reason, PC keyboards send both bits of information. They send a keycode to the PC when a key is pressed and a different keycode when that key is released.
One example is that of counting milliseconds. The PAUSE takes approximately one millisecond and a few simple statements may take say 0.4ms. Those statements might decrement a counter and loop back to a PAUSE if the count is > 0. Now you have a more complex counter that's tied into a time source.
Try writing a program that displays a message, waits for 500ms using only a PAUSE 1 as the timing statement, then displays another message.
Alternatively, write a program that displays a message, waits for you to close a switch, and keeps track of the elapsed time in milliseconds using only a PAUSE 1, then displays another message.
Great News! Congrats.
"PBA 1
PBB 0
PBC 0
TC( Total Counter) 1"
This works for each button but i cant get it to loop
Here is my code
"
CounterA VAR WORD
CounterB VAR WORD
CounterC VAR WORD
counter VAR WORD
totalcounter VAR WORD
maximum VAR WORD
CodeA VAR WORD
CodeB VAR WORD
CodeC VAR WORD
CodeA = 2
CodeB = 3
CodeC = 3
maximum = 8
countera = 0
main:
DO
DO UNTIL (IN3 = 1) OR (IN4=1) OR (IN5=1)
LOOP
IF IN3 = 1 THEN
CounterA = CounterA + 1
totalcounter = totalcounter + 1
DO WHILE IN3 = 0
LOOP
ELSEIF IN4 =1 THEN
counterB= counterb + 1
totalcounter = totalcounter + 1
DO WHILE IN4=0
LOOP
ELSEIF IN5=1 THEN
counterc = counterc + 1
totalcounter = totalcounter + 1
DO WHILE IN5=1
LOOP
ENDIF
DO
DO
DEBUG CR,"PBA ",DEC CounterA, CR
DEBUG CR,"PBb ",DEC Counterb, CR
DEBUG CR,"PBC ",DEC Counterc, CR
DEBUG CR, "TC", DEC Totalcounter, CR
DO WHILE IN3 = 0 OR IN4 = 0 OR IN5=0
LOOP
LOOP
LOOP
LOOP UNTIL totalcounter =8 "
As your code is displayed now, I am too lazy to try to figure out which LOOP goes with which DO.
2. But surely there is one too many DO/LOOPs near the bottom. Get your indentation squared away and I'm sure you will see it.
Here is the new code
CodeA = 2
CodeB = 3
CodeC = 3
maximum = 8
countera = 0
counterb = 0
counterc = 0
totalcounter = 0
DO
IF(IN3=1) AND (IN4=0) AND (IN4=0) THEN
PAUSE 500
DO
IF (IN3=0 AND counterc = 0 AND counterb = 0) THEN
counterA = counterA +1
totalcounter = totalcounter + 1
ELSEIF IN3 = 0 AND counterb > 0 OR Counterc > 0 AND totalcounter >= 0 THEN
totalcounter = totalcounter + 1
ENDIF
LOOP UNTIL IN4 = 0
DEBUG CR,"PBA ",DEC CounterA, CR
DEBUG CR, "TC", DEC Totalcounter, CR
ENDIF
IF(IN4=1) AND (IN3=0) AND IN5=0 THEN
PAUSE 500
IF IN4=0 AND counterc = 0 AND countera > 0 THEN
counterb = counterb +1
totalcounter = totalcounter + 1
ELSEIF IN4 = 0 AND countera > 0 OR counterc > 0 OR totalcounter >= 0 THEN
totalcounter = totalcounter + 1
ENDIF
DEBUG CR,"PBB ",DEC CounterB, CR
DEBUG CR, "TC", DEC Totalcounter, CR
ENDIF
IF(IN5=1) AND IN4=0 AND IN3=0 THEN
PAUSE 500
IF IN5=0 AND counterb > 0 AND countera >0 THEN
counterc = counterc +1
totalcounter = totalcounter + 1
ELSEIF IN5= 0 AND counterb = 0 AND countera = 0 THEN
totalcounter = totalcounter + 1
ENDIF
DEBUG CR,"PBC ",DEC CounterC, CR
DEBUG CR, "TC", DEC Totalcounter, CR
ENDIF
LOOP
Please use the "CODE" tags so others can understand you indentation.