Help with project.

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.

Comments

  • 17 Comments sorted by Date Added Votes
  • Mike GreenMike Green Posts: 22,754
    edited October 1 Vote Up0Vote Down
    We can't do the homework for you. We would be happy to explain how various Basic Stamp features work or clarify some detail you don't understand.

    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.
  • I know that a cycle for instance would be pin = 0 back to pin = 1 back to Pin = 0.
  • I put the question so that you'd understand what I was trying to do. I've got the counter counting how long the button is being pressed but having problems getting it to count the cycle.
  • Have you looked at the COUNT command and sample programs?
    Florida, between St. Petersburg and the Gulf of Mexico

    Do not look directly into laser with remaining good eye...
  • I've been having a hard time trying to utilize it. It's not clear to me as far as how to declare it and implement it with the pin that I'm using.
  • The COUNT command is not useful for this exercise. Keep track of the number of PB cycles in variables.
    Re-inventing the wheel is not a waste of time if, when you are done, you understand why it is round.
  • How are you recommending to do that? Which function? The counter function just keep track of how long the PB =1
    The COUNT command is not useful for this exercise. Keep track of the number of PB cycles in variables.

  • something like (pseudo code)
    wait for PB to go HIGH a few millisec
    wait for PB to go LOW a few millisec
    increment a counter
    
    Re-inventing the wheel is not a waste of time if, when you are done, you understand why it is round.
  • Ok I think I see what you're saying. Set a limit for the counter then add that to another counter.
    something like (pseudo code)
    wait for PB to go HIGH a few millisec
    wait for PB to go LOW a few millisec
    increment a counter
    

  • You will need to keep track of the approximate time that goes by while you're waiting for something to happen. Most simple statements take a fraction of a millisecond to execute. With the PAUSE statement, you can create much longer delays.

    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.
  • Thanks everyone! I've completed the project. !
    Mike Green wrote: »
    You will need to keep track of the approximate time that goes by while you're waiting for something to happen. Most simple statements take a fraction of a millisecond to execute. With the PAUSE statement, you can create much longer delays.

    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.

  • userjyjy wrote: »
    Thanks everyone! I've completed the project. !
    Mike Green wrote: »
    You will need to keep track of the approximate time that goes by while you're waiting for something to happen. Most simple statements take a fraction of a millisecond to execute. With the PAUSE statement, you can create much longer delays.

    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.
    Infernal Machine
  • So I have ran into another problem! I've gotten the buttons to count individually but I need them to count all together no matter the order but only one order being the correct one. I need to have all the buttons with an individual counter but also each button push needs to be added to a total button counter. The code that I have will let me press either button and Ive debugged it to show for example if Push Button A is pressed
    "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 "


  • 1. If you post your code inside code/code, it will preserve the formatting. See the big "C" in the formatting bar just above the text box. Thus:
    DO
         some code
         more code
         LOOP
    

    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.
    Re-inventing the wheel is not a waste of time if, when you are done, you understand why it is round.
  • Ok I got a little frustrated and rewrote the code. I got the code to work as far as getting each button to add to the total counter and not adding to the individual counter if the button after it is pressed. The only problem im having now is getting push button 1 counter to not count if the other pushbuttons have been pushed first.

    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











    1. If you post your code inside code/code, it will preserve the formatting. See the big "C" in the formatting bar just above the text box. Thus:
    DO
         some code
         more code
         LOOP
    

    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.

  • Seems to me you are making it a bit more complicated than it needs to be. Not sure I have the indentation right since I have no access to the BS editor but the following main: should do what you want.
    
    DO UNTIL 
        DO UNTIL (IN3 = 1) OR (IN4=1) OR (IN5=1)' wait for a button to be pressed
    
            totalcounter = totalcounter + 1     ' a button has to be pressed to get here so increment Totalcounter
    
            IF IN3 = 1 THEN
                CounterA = CounterA + 1         ' Button A pressed so increment counterA
            ELSEIF IN4 =1 THEN
                CounterB = CounterB + 1         ' Button B pressed so increment counterB
            ELSEIF IN5=1 THEN
                CounterC = CounterC + 1         ' Button C pressed so increment counterC
    
        DO UNTIL (IN3 = 0) AND (IN4 = 0) AND (IN5 = 0)' wait for all buttons to be released
    LOOP
    
    
    In science there is no authority. There is only experiment.
    Life is unpredictable. Eat dessert first.
  • userjyjy wrote: »
    Ok I got a little frustrated and rewrote the code. I got the code to work as far as getting each button to add to the total counter and not adding to the individual counter if the button after it is pressed. The only problem im having now is getting push button 1 counter to not count if the other pushbuttons have been pushed first.

    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











    1. If you post your code inside code/code, it will preserve the formatting. See the big "C" in the formatting bar just above the text box. Thus:
    DO
         some code
         more code
         LOOP
    

    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.

    Please use the "CODE" tags so others can understand you indentation.
    Infernal Machine
Sign In or Register to comment.