Shop OBEX P1 Docs P2 Docs Learn Events
Using a switch with the SEROUT command — Parallax Forums

Using a switch with the SEROUT command

TonyATonyA Posts: 226
edited 2006-02-27 14:22 in BASIC Stamp
Hi,

I am wondering if anyone could help me with some code. I would like to use a switch with the serout command like

Switch = On THEN
SEROUT something

However, I would like to only have the SEROUT event occur once if the switch remains in the ON position. So in other words I can only produce another SEROUT event if the switch is switched back to the off position and then back to the on position.

The SEROUT event in my application is for sending MIDI notes. So I am trying to have a switch (the piezo Vibra Tab) only produce one note when pressed, if pressure applied to the piezo is held it will only produce that one note until pressure is released and than reapplied. Like a midi drum pad.

Thanks for any help with this.

Tony A.

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-02-26 18:02
    Tracy Allen has frequently shares a cool trick with the XOR operator that lets you know when a switch has changed states and what its current value is.· I used that trick in StampWorks Experiment 14; the code is attached.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • TonyATonyA Posts: 226
    edited 2006-02-26 19:21
    Hi Jon,

    Thanks for that code. I'll have to figure this out. I'm not sure yet how to apply it to what I'm doing. (Sometimes these things take longer to sink in these days).

    I was wondering though, if there might be another way to do this.

    Suppose I had a simple on/off switch. If the switch is turned on only one midi note (one SEROUT event) would be produced, then stop even though the switch is still in the on position. Another (2nd) SEROUT event could only be produced if the switch is turned off and then on again. Is there a way to do this with the SEROUT command instead of monitoring the state of the switch?

    Thanks again, I appreciate it.

    Tony A.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-02-26 19:39
    Nope -- you've got to track the switch if that's the element you're setting as the control point.· Of course you can brute-force it, but then you're limited to just one switch, and in a MIDI system I think you're probably going to want more.

    Brute_Force:
    · DO : LOOP WHILE (TheButton = Pressed)·········· ·' force release
    · DO : LOOP UNTIL (TheButton = Pressed)·········· ·' wait for clean 0 -> 1 press
    · SEROUT Sio, Baud, [noparse][[/noparse]NoteOn, theNote, velocity]
    · DO : LOOP WHILE (TheButton = Pressed)········· · ' force release
    · DO : LOOP UNTIL (TheButton = Pressed)·········· ·' wait for clean 0 -> 1 press
    · SEROUT Sio, Baud, [noparse][[/noparse]NoteOff, theNote, velocity]
    · GOTO Brute_Force

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax

    Post Edited (Jon Williams (Parallax)) : 2/26/2006 7:43:59 PM GMT
  • TonyATonyA Posts: 226
    edited 2006-02-26 21:16
    Jon,

    I'm starting to understand now. You need the condition of "off plus on" to trigger.

    Is that what this is doing?
    IF (xBtns.LOWBIT(idx) = 1) THEN

    Thanks,
    Tony
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-02-26 21:21
    Exactly. The only time a bit will be set in xBits is when it made a transition from 0 to 1 (last scan was 0, current scan is 1).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • TonyATonyA Posts: 226
    edited 2006-02-27 02:00
    Jon,

    I was wondering about a couple of lines in the code you posted above. In the MAIN section there is "FOR idx = 0 TO 3" for the 4 input pins. In the "Get_Buttons" (sub routine) section I see "FOR idx = 1 TO 5" <--- I'm just wondering what that is for.

    Thanks again,
    Tony A
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-02-27 03:58
    Switch debouncing to make sure the contacts stay closed during the entire scan before they're considered valid;·this is explained in StampWorks.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • TonyATonyA Posts: 226
    edited 2006-02-27 13:59
    Thanks again.

    I am looking up Stamp Works Exp 14, I see Exp 15 but without the switch state detection code in it, only Debounce. Is there an explaination of the code for Debounce plus Switch state detection?

    Tony A

    Post Edited (TonyA) : 2/27/2006 2:10:41 PM GMT
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-02-27 14:19
    Page 96 of StampWorks 2.0 describes the basic debouncing loop -- essentially, that loop ensures that the switch is closed during the entire scan; if any of the buttons lifts (bounces) at all during the scan, then the use of the AND operator causes that button to be invalidated for that scan. Page 97 goes into the transition detection.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • TonyATonyA Posts: 226
    edited 2006-02-27 14:22
    Ok, Thank you.

    Tony A
Sign In or Register to comment.