How to make a switch momentary

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.

You've got something wrong with your code. Since you didn't want to share that with us you're kinda on your own. Good luck.

We can help you, would you help us?,148850.0.html


@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.

American Airlines will do that to ya.

But they're something special in the air!

But they're something special in the air!

They must be something special in the air. :wink:

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


void loop() {

buttonState = digitalRead(buttonPin);

  if (!pressed && buttonState == HIGH) 
    // turn LED on: and set pressed true
    pressed = true;    
    digitalWrite(ledPin, HIGH);'A');
  else if (pressed && buttonState == LOW) 
    // turn LED off: and reset pressed flag
    pressed = false;
    digitalWrite(ledPin, LOW); 

To monitor switches, you might want to look at the change in state example that came with the IDE.

How is your switch wired?
Show us a good image of your wiring.


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.

Forgot the photo.

Is this the way you have the switch connected?

Here is a discussion about switches:

You could just flip the switch on/off to get a momentary action.

Added OP image.


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.

PWM is for output. We're talking about an input. Can you explain how PWM figures into this?

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.

@LarryD My plan was to have to toggle switch be 2 mappings, so I have it set up as:

-------- arduino input
ground *-------- arduino input

I tested it, this works except I need it to stop the input after about 100 milliseconds or average key press duration.