I have a project I'm working on, it's a USB button box. The only problem I have is the programming, I have tried keyboard.print which worked until it wouldn't stop writing it. Even with keyboard.releaseAll.
So how would I be able to make a toggle switch act as a key press. I thought putting in delay then using keyboard.release(key) would work but it didn't. So I kinda hit a dead end, any help would be nice.
@owendoesarduino, post your code describe your hardware setup so we can help.
Use the code tags button </> and paste your code into the box that opens.
Please use CTRL-T on your code before you copy & paste it.
@Delta_G, try some decaf man, you're wound too tight for this time of the day/night.
Here's the code, I'm new to the forum (should have added that) so I didn't know how to. My board is the Teensy 2.0 because of it's size so that's why the pins might be different.
const int buttonPin = 20; //sets pin 20 as button
const int ledPin = 11;
int buttonState = 0;
boolean pressed = false;
void setup() {
// put your setup code here, to run once:
pinMode(buttonPin, INPUT); //make buttonPin an input
Keyboard.begin(); //control keyboard
Keyboard.releaseAll();
}
void loop() {
buttonState = digitalRead(buttonPin);
if (!pressed && buttonState == HIGH)
{
// turn LED on: and set pressed true
pressed = true;
digitalWrite(ledPin, HIGH);
Keyboard.press('A');
}
else if (pressed && buttonState == LOW)
{
// turn LED off: and reset pressed flag
pressed = false;
digitalWrite(ledPin, LOW);
}
}
The switch is a spdt but it isn't momentary, so it stays on the pole that it is toggled to. I didn't realize this at the beginning, now I this is the main issue holding me back.
The best way to rig a momentary switch is by PWM or pulse width modulation. Instead of using a tile go to radio shack (yay they carry stuff like that again) and buy you some lil momentary switch buttons and use the PWM to Send a signal to what ever your project is. I think most arduino come with pull-up resistors on board so it's really as easy as wiring one prong to the arduinos PWM pin and a ground. Instant momentary.
I would walk away from using a toggle at first unless you have lightning reflexes since it usually toggles one way or the other and sticks which means a loss of precision with what ever your controlling. Like a game controller.
Or buy a cheap game controller and wire it your arduino they even make wireless PS2 controllers that wire straight to your arduino using 7 wires. Either way way I think PWM would be your best bet.
it says at the top he wants to use the toggle as a key press. the longer you press it or toggle it for will send a signal for as long as its pressed. Maybe it's not PWM but I know it's simple all you need is an arduino a switch and two wires. Then set the code to send signal constantly or momentarily.