Need help with an example (mediabuttons)

Status
Not open for further replies.

MapTwo

New member
Hi, new guy here with a Teensy 3.2 board

I'm using the media buttons example (in: File - Example - teensy - USB_keyboard). I've simplified the code for my use which is only the first three buttons.

After making sure the sketch works for my project (controlling my music on my android phone via USB OTG hub) I realize that i cannot fit extra buttons in my small enclosure due to assembly method. I thought: Why not use the touch pins? I've tried some innocent changes in the code and only met terrible failure lol.
Could i have some help changing the inputs from buttons to touchread? So far I know i need two pins for each buttons with a resistor between pins for the touch sensing. The online examples i can find about touchread functions are rarely about buttons and more about sliders and arrays and piano keys. I'd just like to be able to have 3 simple touch buttons. Is that possible for someone with a good logic and zero coding skills?

My only code modification to the example other than shortening it is giving it a stupid-high bounce value to prevent the phone from registering multiple keypresses (i had cheap sticky buttons lined up for this because they looked and felt smooth:cool:)
The board has nothing soldered to it yet and my only preference to used pins is the outside row of through holes (i'm bad at pad soldering so far but will try if i must use pin 25, 32 or 33 for some reason)

I attach the simplified code so you guys know what i'm trying to do. I can find the right resistor value on my own i think but the code part eludes me. How would you replace buttons with touch in this?

thanks in advance for any time spent on this.
 

Attachments

  • MediaButtonsMAPTWOEDIT.ino
    2.1 KB · Views: 66
Could i have some help changing the inputs from buttons to touchread? So far I know i need two pins for each buttons with a resistor between pins for the touch sensing.

Only one pin per touchRead() input is needed. Small valued resistors are recommended to protect the Teensy, but you can start without any additional hardware to see how things work. There's no touchRead pin mode. Just call touchRead(pin). Not all pins work. Check the reference card.

I suggest the following step-by-step approach:

Call touchRead(0) in your main loop and Serial.println() the value to the console then delay(250) or so. You'll see that as you touch or even get very close to pin 0 on the Teensy, the number goes up.

Attach a wire. Notice the minimum value is now higher than before. Try touching the wire or grabbing the wire. Notice that the more of your hand surrounds the wire, the higher the value. Also notice, the wire does not need to be stripped and for protecting your Teesny, it's better if the part you touch is insulated. Think static zap!

You'll need to experiment to find the threshold between a glancing touch and a real touch. Likewise, you will need to 'debounce' the readings so a very brief but hard touch gets ignored.

The thing to understand is that everything has capacitance. Some things store charge more easily than others. Water in your finger stores charge much more readily than air or plastic.

You can also use touchRead to make a soil moisture sensor or a water level sensor. Cool, huh?
 
Now that's something to work with! I'm gonna experiment with this and report with progress soon!

Thanks a million!:cool:
 
ok, so by some weird fashion, my pins 0 and 1 have a default value of 500 and a working threshold of 800 but for pin 23 i need a threshold of 1000. Otherwise the next track button just auto-presses on it's own.

Is there a way to setup more than one threshold? I know that this won't work as it's redeclaring "threshold":

int threshold(0) = 800;
int threshold(1) = 800;
int threshold(23) = 1000;

here's the updated code with threshold redeclaration.
pitch in if you have ideas. thank you!;)

Code:
/* Buttons to USB Keyboard Example - Special Media Player Keys

   You must select Keyboard from the "Tools > USB Type" menu

   This example code is in the public domain.
*/

#include <Bounce.h>

Bounce button0 = Bounce(0, 1000);
Bounce button1 = Bounce(1, 1000); 
Bounce button2 = Bounce(23, 1000);
                                 

void setup() {
  Serial.begin(9600);
}

void loop() {
  button0.update();
  button1.update();
  button2.update();
  
  Serial.println(touchRead(23));
  delay(250);
  int threshold(0) = 800;
  int threshold(1) = 800;     // no idea how to add more threshold values 
  int threshold(23) = 1000;   // to fix pin 23 auto pressing the next track button.
  int touch0 = touchRead(0);
  int touch1 = touchRead(1);
  int touch2 = touchRead(23);
  
  if (touch0 > threshold) {
    Keyboard.set_media(KEY_MEDIA_PREV_TRACK);
    Keyboard.send_now();	
    delay(300);   
    Keyboard.set_media(0);
    Keyboard.send_now();	
  }
  if (touch1 > threshold) {
    Keyboard.set_media(KEY_MEDIA_PLAY_PAUSE);
    Keyboard.send_now();
    delay(300); 
    Keyboard.set_media(0);
    Keyboard.send_now();
  }
  if (touch2 > threshold) {
    Keyboard.set_media(KEY_MEDIA_NEXT_TRACK);
    Keyboard.send_now();
    delay(300); 
    Keyboard.set_media(0);
    Keyboard.send_now();
  }

}
 
found something... if I tell the Teensy this:

int touch2 = (touchRead(23) - 200);

it will compensate for the difference between the 23 pin and the two other ones.
Did I do this right?

thanks

Code:
/* Buttons to USB Keyboard Example - Special Media Player Keys

   You must select Keyboard from the "Tools > USB Type" menu

   This example code is in the public domain.
*/

#include <Bounce.h>

Bounce button0 = Bounce(0, 1000);
Bounce button1 = Bounce(1, 1000);  
Bounce button2 = Bounce(23, 1000); 
                               

void setup() {
  Serial.begin(9600);
}

void loop() {
  button0.update();
  button1.update();
  button2.update();
  
  Serial.println(touchRead(23));
  delay(250);
  int threshold = 800;
  int touch0 = touchRead(0);
  int touch1 = touchRead(1);
  int touch2 = (touchRead(23) - 200);
  
  if (touch0 > threshold) {
    Keyboard.set_media(KEY_MEDIA_PREV_TRACK);
    Keyboard.send_now();	
    delay(300);  // 
    Keyboard.set_media(0);
    Keyboard.send_now();	
  }
  if (touch1 > threshold) {
    Keyboard.set_media(KEY_MEDIA_PLAY_PAUSE);
    Keyboard.send_now();
    delay(300); 
    Keyboard.set_media(0);
    Keyboard.send_now();
  }
  if (touch2 > threshold) {
    Keyboard.set_media(KEY_MEDIA_NEXT_TRACK);
    Keyboard.send_now();
    delay(300); 
    Keyboard.set_media(0);
    Keyboard.send_now();
  }

}
 
Last edited:
Status
Not open for further replies.
Back
Top