Your button assignments are not in a correct syntax:
Code:
button1=pin1
button2=pin2
button3=pin3
The above code fails because the compiler has no idea what
type button1, 2 and 3 variables are.
I am going to assume you are trying to use pins 1, 2, and 3 to check for those relays being switched on/off. I am also going to assume that you want to use the Bounce library for debouncing those relays as well. Finally, I'm going to assume that when your relays are triggered, they connect input to ground. Thus, triggering HIGH to LOW. This is important.
The first thing to do is ensure that you are using the correct core and board in the Arduino IDE:
- From the Menu, select Tools -> Board -> Teensy 2.0
- From the Menu, select Tools -> USB Type -> Keyboard + Mouse + Joystick
Now onto the code. First, ensure you are using the Bounce library. You can confirm this by making sure you include it in your code like so:
Next, you must create your objects to handle the button detection. For pins 1, 2, and 3, it would look something like so:
Code:
#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.
// First argument is the pin, second argument is the time (in ms) the pin must produce a
// particular input to show a value change in the button.
Bounce button1 = Bounce(1, 10);
Bounce button2 = Bounce(2, 10);
Bounce button3 = Bounce(3, 10);
Next, comes the
setup() routine. This method is only run once at start up. This would be where you setup your pins for input and do any other 1-time tasks. Here we will make sure the pins are set to input.
Code:
#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.
// First argument is the pin, second argument is the time (in ms) the pin must produce a
// particular input to show a value change in the button.
Bounce button1 = Bounce(1, 10);
Bounce button2 = Bounce(2, 10);
Bounce button3 = Bounce(3, 10);
void setup() {
// Configure our pins
// Note, teensy has built-in pullup resistors that can
// be activated by using INPUT_PULLUP instead of INPUT
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
}
Above we are using INPUT_PULLUP so that we make sure the input pins will only read LOW when a relay is triggered. In all other cases, including disconnection of the relay from the Teensy will result in the pins reading HIGH.
Finally, you build your
loop() method. This method is run continuously. Here you would update your buttons, then check for falling and rising edges. This is where it might get tricky because it sort of depends on how you wired up your relays.
- Falling Edge detects a change from HIGH to LOW on the input pin. For a key to be pressed using this method, your relays must change input from HIGH to LOW when triggered.
- Rising Edge detects a change from LOW to HIGH on the input pin. For a key to be pressed using this method, your relays must change input from LOW to HIGH when triggered.
According to the assumptions falling edge detects key presses and rising edges detects key releases. Note that simply checking for just the falling edge may not be enough. Sure, by checking this, whenever your relays are triggered, a keyboard press is sent. But, you must also be able to release those keys. Or else, your computer will think the keys are being held down!
Thus your code might look like the following when you put it all together:
Code:
#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.
// First argument is the pin, second argument is the time (in ms) the pin must produce a
// particular input to show a value change in the button.
Bounce button1 = Bounce(1, 10);
Bounce button2 = Bounce(2, 10);
Bounce button3 = Bounce(3, 10);
void setup() {
// Configure our pins
// Note, teensy has built-in pullup resistors that can
// be activated by using INPUT_PULLUP instead of INPUT
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
}
void loop() {
// Update our buttons
button1.update();
button2.update();
button3.update();
// Check for falling edges
// If a falling edge is detected, set key on the keyboard
// accordingly.
if (button1.fallingEdge()) {
Keyboard.set_key1(KEY_1);
}
if (button2.fallingEdge()) {
Keyboard.set_key2(KEY_H);
}
if (button3.fallingEdge()) {
Keyboard.set_key3(KEY_ENTER);
}
// Check for rising edges
// If a rising edge is detected, set key on the keyboard
// to zero (key is no longer pressed).
if (button1.risingEdge()) {
Keyboard.set_key1(0);
}
if (button2.risingEdge()) {
Keyboard.set_key2(0);
}
if (button3.risingEdge()) {
Keyboard.set_key3(0);
}
// Finally, send out the Keyboard's report to the computer
Keyboard.send_now();
}
Most of the above information was taken from the Buttons example for the teensy platform, with some minor differences. I chose to use
set_key# instead of
press because internally, using
press causes the button press to be sent immediately, while
set_key# requires you to manually "send" the key presses yourself. Basically instead of sending three individual presses, we send 1 group of 3.
Also this is setup to constantly send out key presses as long as the relays are triggered. If all you wanted was a 1-time key press and release for each triggering of the relays, the following code could be used:
Code:
#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.
// First argument is the pin, second argument is the time (in ms) the pin must produce a
// particular input to show a value change in the button.
Bounce button1 = Bounce(1, 10);
Bounce button2 = Bounce(2, 10);
Bounce button3 = Bounce(3, 10);
void setup() {
// Configure our pins
// Note, teensy has built-in pullup resistors that can
// be activated by using INPUT_PULLUP instead of INPUT
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
}
void loop() {
// Update our buttons
button1.update();
button2.update();
button3.update();
// Check for falling edges
// If a falling edge is detected, set key on the keyboard
// accordingly.
if (button1.fallingEdge()) {
Keyboard.set_key1(KEY_1);
}
if (button2.fallingEdge()) {
Keyboard.set_key2(KEY_H);
}
if (button3.fallingEdge()) {
Keyboard.set_key3(KEY_ENTER);
}
// Send out the Keyboard's report to the computer
// for the presses.
Keyboard.send_now();
// Immediately send a blank report indicating that the keyboard
// has no buttons pressed
Keyboard.releaseAll();
}
Hope this helps. But, as Paul said, it really helps if you explain your what you are trying to do and the code you have as concisely and thoroughly as possible. I had to make assumptions because you didn't provide enough information on how you were connecting things to the Teensy, and what you wanted the result to be.
I make no guarantees this code works or even compiles.