Using non momentary switches with teensy for pc button box

Status
Not open for further replies.

faber

Member
Hi everyone, I am a newb to the teensy and am building my first teensy based project which is a simple button box for a PC. I have gotten the teensy loaded with the Keyboard/mouse/joystick sketch and managed to add a few more buttons by addind new iterations to each block of code in the sketch. But I am now trying to figure out how I can use toggle switches that are non-momentary to send momentary signals. Is there a way to do this in code rather than using hardware relays? I basically just want to have a few dpdt on-on-on switches to control a flight sim which expects a momentary. Any help would be much appreciated. Here is the code I am currently using:
/* Buttons to USB Joystick Example

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

This example code is in the public domain.
*/

#include <Bounce.h>

// Create Bounce objects for each button. The Bounce object
// automatically deals with contact chatter or "bounce", and
// it makes detecting changes very simple.
Bounce button0 = Bounce(0, 10);
Bounce button1 = Bounce(1, 10); // 10 = 10 ms debounce time
Bounce button2 = Bounce(2, 10); // which is appropriate for
Bounce button3 = Bounce(3, 10); // most mechanical pushbuttons
Bounce button4 = Bounce(4, 10);
Bounce button5 = Bounce(5, 10);
Bounce button6 = Bounce(6, 10);
Bounce button7 = Bounce(7, 10);
Bounce button8 = Bounce(8, 10);
Bounce button9 = Bounce(9, 10);
Bounce button10 = Bounce(10, 10);
Bounce button11 = Bounce(11, 10);

void setup() {
// Configure the pins for input mode with pullup resistors.
// The pushbuttons connect from each pin to ground. When
// the button is pressed, the pin reads LOW because the button
// shorts it to ground. When released, the pin reads HIGH
// because the pullup resistor connects to +5 volts inside
// the chip. LOW for "on", and HIGH for "off" may seem
// backwards, but using the on-chip pullup resistors is very
// convenient. The scheme is called "active low", and it's
// very commonly used in electronics... so much that the chip
// has built-in pullup resistors!
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP); // Teensy++ LED, may need 1k resistor pullup
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);

// Please be aware the X, Y, Z, Zr and Slider axes will have default
// settings, if you only use the buttons. This can give the appearance
// of the buttons interfering with the axes, if your PC software shows
// different default assumed values before your first button press.
// More details here:
// https://forum.pjrc.com/threads/29320-Teensy-3-1-Button-problems?p=80275#post80275
}

void loop() {
// Update all the buttons. There should not be any long
// delays in loop(), so this runs repetitively at a rate
// faster than the buttons could be pressed and released.
button0.update();
button1.update();
button2.update();
button3.update();
button4.update();
button5.update();
button6.update();
button7.update();
button8.update();
button9.update();
button10.update();
button11.update();

// Check each button for "falling" edge.
// Update the Joystick buttons only upon changes.
// falling = high (not pressed - voltage from pullup resistor)
// to low (pressed - button connects pin to ground)
if (button0.fallingEdge()) {
Joystick.button(1, 1);
}
if (button1.fallingEdge()) {
Joystick.button(2, 1);
}
if (button2.fallingEdge()) {
Joystick.button(3, 1);
}
if (button3.fallingEdge()) {
Joystick.button(4, 1);
}
if (button4.fallingEdge()) {
Joystick.button(5, 1);
}
if (button5.fallingEdge()) {
Joystick.button(6, 1);
}
if (button6.fallingEdge()) {
Joystick.button(7, 1);
}
if (button7.fallingEdge()) {
Joystick.button(8, 1);
}
if (button8.fallingEdge()) {
Joystick.button(9, 1);
}
if (button9.fallingEdge()) {
Joystick.button(10, 1);
}
if (button10.fallingEdge()) {
Joystick.button(11, 1);
}
if (button11.fallingEdge()) {
Joystick.button(12, 1);
}
// Check each button for "rising" edge
// Update the Joystick buttons only upon changes.
// rising = low (pressed - button connects pin to ground)
// to high (not pressed - voltage from pullup resistor)
if (button0.risingEdge()) {
Joystick.button(1, 0);
}
if (button1.risingEdge()) {
Joystick.button(2, 0);
}
if (button2.risingEdge()) {
Joystick.button(3, 0);
}
if (button3.risingEdge()) {
Joystick.button(4, 0);
}
if (button4.risingEdge()) {
Joystick.button(5, 0);
}
if (button5.risingEdge()) {
Joystick.button(6, 0);
}
if (button6.risingEdge()) {
Joystick.button(7, 0);
}
if (button7.risingEdge()) {
Joystick.button(8, 0);
}
if (button8.risingEdge()) {
Joystick.button(9, 0);
}
if (button9.risingEdge()) {
Joystick.button(10, 0);
}
if (button10.risingEdge()) {
Joystick.button(11, 0);
}
if (button11.risingEdge()) {
Joystick.button(12, 0);
}
}
 
Is there a way to maybe have some code running that checks whether a pin is shorted to ground, and then just sends a 10 ms pulse when it detects a short, and another 10ms pulse when it detects no short?
 
I am new to arduino and teensy programming so I just need a little chunk of code I can insert into my project to allow specific pins to perform the toggle work. For instance I have 20 momentary spst switches that will just act like buttons, but I have a few rotary switches that I want to use and some dpdt toggles that latch in the on position. The latching switches act like a button that is being held down. I just want those to send a momentary button press to the pc. I know it will require a loop but I am not good at programming yet. I am sure this is probably simple, but I am having a hard time getting it. Any help is much appreciated. I will send a $10 starbucks card to the first person that can solve this and help me understand :)
 
Perhaps this library will attach to your buttons and momentary switches and give you the desired feedback:: pjrc.com/teensy/td_libs_Bounce.html

It looks like that was tried above - not sure what failed?

I'm not sure either. When I short the pins to ground and hold them there they send constant "pressed" signals to the pc until i stop. I just want them to send a quick press for certain pins regardless of how long they are held down.
 
I'm not sure either. When I short the pins to ground and hold them there they send constant "pressed" signals to the pc until i stop. I just want them to send a quick press for certain pins regardless of how long they are held down.

That code will always return the current value as it does for Normal buttons. Using these methods would allow you to detect a change in state on those switches:

Code:
  // The risingEdge method is true for one scan after the de-bounced input goes from off-to-on.
	bool risingEdge();
  // The fallingEdge  method it true for one scan after the de-bounced input goes from on-to-off. 
	bool fallingEdge();
 
That code will always return the current value as it does for Normal buttons. Using these methods would allow you to detect a change in state on those switches:

Code:
  // The risingEdge method is true for one scan after the de-bounced input goes from off-to-on.
	bool risingEdge();
  // The fallingEdge  method it true for one scan after the de-bounced input goes from on-to-off. 
	bool fallingEdge();

Thank you for helping me! Please forgive my lack of knowledge but where would I put that code to make it work? I tried pasting it into my project but it still does the same as before.
 
I just want them to send a quick press for certain pins regardless of how long they are held down.

To accomplish this, put both Joystick.button(1, 1) and Joystick.button(1, 0) in the same "if" check for falling edge.

Hopefully that will be obvious with a little hindsight. Joystick.button(1, 1) sends communication to your PC that the button has just been pressed. Joystick.button(1, 0) tells your PC the button has just been released. Since you want a very short press to happen the moment the physical button is pressed (the falling edge), you would do both of these in the same "if" check that runs when the falling edge happens. Then just do nothing for the rising edge "if".

Some PC software might expect a minimum time. If so, just add something like delay(20). There are more complex ways to add delay but keep scanning for events, but for now while you're learning keep things simple.
 
Thank you for the explanation. I really appreciate the help. I will give it a shot and report back once i get back home.
 
Thank you for the explanation. I really appreciate the help. I will give it a shot and report back once i get back home.
I think that worked!!! I put the following line in for the falling "if" check like you suggested:
Code:
if (button0.fallingEdge()) {
Joystick.button(1, 1); delay(40); Joystick.button(1, 0);
}

I changed the 20 to a 40 because it flashed so quickly when i shorted the pin to ground, but it flashed momentarily in the joystick control panel even though I held it shorted.

Where am I sending the coffee? Your explanation really helped me understand what was going on. Thanks a ton! I am stoked to finish my little project and start learning more. :D
 
Status
Not open for further replies.
Back
Top