Shop OBEX P1 Docs P2 Docs Learn Events
5-Position switch coding with Impact sound sensor — Parallax Forums

5-Position switch coding with Impact sound sensor

steve418rsteve418r Posts: 5
edited 2009-12-11 22:17 in Accessories
I have the new 5-position switch and sound impact sensor. I have the switch set up in an If then code with each pin being an elseif . I'm trying to make it so that when I click the switch and it starts to do a movement pattern, with in that movement code I'd like to trigger the sound impact sensor to turn a servo quickly (without stopping the movement code). This is my code currently, I also can't seem to get the switch to recognize multiple pins to get the extra switches. Any help would be appreciated.

Comments

  • Jessica UelmenJessica Uelmen Posts: 490
    edited 2009-12-11 00:03
    Hi steve418r,

    In order to use multiple selections in your IF...ELSEIF code block, you're going to have to re-order your statements. The way these statements work is that the program will go through all of the conditional statements until it finds a condition that is true. So if you list this statement last:

    ELSEIF (IN2 = 0) AND (IN4 = 0) THEN ...
    



    It will never get there because it will have reached either of these two statements first:

    ELSEIF (IN2 = 0) THEN ...
    ELSEIF (IN4 = 0) THEN ...
    



    So just be sure to list any code that requires two conditions first.

    Also, regarding your Test subroutine - you have it nested within a DO...LOOP, so that portion of the code is going to keep repeating forever and ever. So once your code branches to that subroutine, it's only going to check if no sound is detected, and if there isn't, it's just going to keep sending those same four pulses to your servos.

    Instead, just stick with your IF...THEN conditional statements - IF this position is pressed, then see if there is a sound. If there is, then do this, if there's not then do this and RETURN. Doing it that way will ensure that your code executes as quickly as possible. The BASIC Stamp isn't capable of doing multiple things simultaneously, so your servo will have to stop turning to check if there is a sound. But you can control how long this delay will last with your code.

    Hope this helps, happy developing!

    -- Jessica

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jessica Uelmen
    Education Department
    Parallax Inc.
  • steve418rsteve418r Posts: 5
    edited 2009-12-11 00:35
    It does help a bit thank you =)
  • steve418rsteve418r Posts: 5
    edited 2009-12-11 01:16
    Has anyone have any idea how to get the multiple switches to work, meaning click the switch diagonally for up and left or clicking in the center then up. I've tried

    ELSEIF (IN2 = 0) AND (IN4 = 0) THEN 'Center AND Down  triggers
              GOSUB Forward_Pulse
    


    It doesn't seem to register as anything other than single codes for each directional
  • Jessica UelmenJessica Uelmen Posts: 490
    edited 2009-12-11 18:42
    Hi steve418r,

    Have you tried moving that to the top as your first statement? As I said before, the loop is only going to execute until it finds one condition that is true. The BASIC Stamp is not capable of reading the states of two I/O pins at the same time, so one will always be read as a logical "0" first.

    Below is some example code demonstrating how to do this:

    DO
      DEBUG CLS
      IF (IN3 = 0) AND (IN1 = 0) THEN
        GOSUB Subroutine_1
      ELSEIF (IN3 = 0) THEN
        GOSUB Subroutine_2
      ELSEIF (IN1 = 0) THEN
        GOSUB Subroutine_3
      ELSE
        PAUSE 20
      ENDIF
    LOOP
    
    Subroutine_1:
    DEBUG HOME, "In Subroutine #1"
    PAUSE 1000
    RETURN
    
    Subroutine_2:
    DEBUG HOME, "In Subroutine #2"
    PAUSE 1000
    RETURN
    
    Subroutine_3:
    DEBUG HOME, "In Subroutine #3"
    PAUSE 1000
    RETURN
    



    Now one other thing to keep in mind is human error. You will not be able to select two positions at exactly the same time, and the current code is executing extremely fast, so it will execute whatever condition it detected as "pressed" first. But, if you hold both positions and wait for the loop to repeat itself, you'll find that it will go to the correct subroutine.

    Try moving the first IF...THEN statement to be the third one so your DO...LOOP looks like this:

    DO
      DEBUG CLS
      IF (IN3 = 0) THEN
        GOSUB Subroutine_2
      ELSEIF (IN1 = 0) THEN
        GOSUB Subroutine_3
      ELSEIF (IN3 = 0) AND (IN1 = 0) THEN
        GOSUB Subroutine_1
      ELSE
        PAUSE 20
      ENDIF
    LOOP
    



    Try holding both positions down again. Will it make it to the correct subroutine now?

    The answer is no, because it's getting to the IF (IN3 = 0) THEN ... statement first. It sees that condition is true, and doesn't move on to check any other conditions that you may have listed, because it found the first one to be true.

    Back to the human error component I was talking about earlier. One way to eliminate this error is to decrease the time it takes to execute the DO...LOOP. Try adding the command "PAUSE 500" right about the statement "LOOP". This will make the loop execute slower, giving the user time to select both positions before the IF...THEN statements start checking the input states of each pin.

    Hope this helps.

    -- Jessica

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jessica Uelmen
    Education Department
    Parallax Inc.

    Post Edited (Jessica Uelmen (Parallax)) : 12/11/2009 6:47:59 PM GMT
  • steve418rsteve418r Posts: 5
    edited 2009-12-11 22:17
    Hey Jessica!

    Thanks for all the help you have be giving me!

    After pulling an all niter on the code and first now getting back to my dorm at 5pm cause of multiple other projects...I'm beat!

    What you said makes so much sense on the pins, but too little too late I presented my project at 11:30 this afternoon. I'm uploading my final code here for others looking to try the impact sound sensor and 5 position switch! My project was one of the best in show.

    Note: I was very tired making this code so its very messy and not 100% efficient.
    I'll post it in projects also with more insight on the coding I used. Now there is finally some code on the interwebs for these two sensors!

    *EDIT* Sorry put up the wrong code, I was so tired, this is the final code, Has movements set to switch position and a program to set different amounts of claps to different programs, as well as Jingle Bells as one of those clap commands. I commented it a bit to show what i did.

    It's a text doc. so just copy and paste it in.

    Post Edited (steve418r) : 12/12/2009 7:18:26 PM GMT
Sign In or Register to comment.