Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 8 of 8

Thread: Non-blocking (no wait) touch sense (touchRead) routine for Teensy 3.2

  1. #1
    Junior Member
    Join Date
    Mar 2021
    Location
    Melbourne, Australia
    Posts
    5

    Non-blocking (no wait) touch sense (touchRead) routine for Teensy 3.2

    Here is a suggestion for an alternative method to read touch inputs on the Teensy 3.2. The Teensyduino library function touchRead() waits for the result before returning, so it may be too slow or cause excess delay to other tasks in some applications. Such was the case with an application I am developing. (It's a MIDI EWI synth. The maximum tolerable latency to read all 12 touch pads is about 10ms.)

    My suggested method defines 3 new functions: touchSenseInit(pin), touchSenseDone() and touchSenseRead(). A test sketch is attached to show how (up to) 12 touch input readings can be acquired (continuously updated) without "blocking", i.e. without causing delay to other "background" tasks executing in the main loop. Note: The test sketch does not include any code to output the touch readings. This is left to the reader to implement. You could use the (USB) Serial library functions to output readings to the host PC running a terminal emulator (e.g. PuTTY -- or the Arduino "Serial Monitor").

    I vaguely remember reading somewhere that the touchRead() function's maximum reading is 60,000, but I can't find anywhere in the code where this limit is imposed. Using my scheme, a time-out can be easily imposed. This would further reduce any latency due to touch sensing. There is another post somewhere on this forum which explains how the 10us delay can also be removed from the touchRead() function.
    teensy32_touch_sense.ino

    Oops... the program I attached to my post contains an error. Here is the corrected version.
    The forum page says I can edit posts, but I can't see how. If anyone can enlighten me, I would be grateful.
    teensy32_touch_sense.ino
    Last edited by defragster; 03-18-2021 at 08:41 AM. Reason: added updated attachment

  2. #2
    Junior Member
    Join Date
    Mar 2021
    Location
    Melbourne, Australia
    Posts
    5
    Oops... the program I attached to my post contains an error. Here is the corrected version.
    The forum page says I can edit posts, but I can't see how. If anyone can enlighten me, I would be grateful.
    teensy32_touch_sense.ino

  3. #3
    The edit function is at the bottom of your post.

  4. #4
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,735
    Quote Originally Posted by billfisher View Post
    The edit function is at the bottom of your post.
    Edit option goes ~2 hours after posting.

  5. #5
    Junior Member
    Join Date
    Mar 2021
    Location
    Melbourne, Australia
    Posts
    5
    Ah-ha! That would explain why I could not edit the post the day after. Thanks for the tip.

  6. #6
    Senior Member pictographer's Avatar
    Join Date
    May 2013
    Location
    San Jose, CA
    Posts
    700
    Have you looked at https://forum.pjrc.com/threads/13987...chread+timeout ?

    I think there's a need for a library on GitHub to support non-blocking touch sense. Posting code on the forum is great for working out issues, but not as effective as putting the code into an open source code repository. I mention GitHub because lots of other Teensy stuff is there.

    Would be good to add debounce functionality, too. Bounce2 doesn't support capacitive touch.

  7. #7
    Junior Member
    Join Date
    Mar 2021
    Location
    Melbourne, Australia
    Posts
    5
    Thanks for the suggestion... I will consider putting something up on GitHub. The library could include a couple more functions... one to return the "on/off" state of a given touch switch and another to detect button "hits" (transitions from "not touched" to "touched", with debounce).

  8. #8
    Quote Originally Posted by defragster View Post
    Edit option goes ~2 hours after posting.
    Thank you for telling me, I didn't know this feature was only available for two hours.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •