Toggle switch act like a button?

I have a simple task that I just can't wrap my head around. I have a simple toggle switch that I want to act as a button. I want to turn the toggle switch on and it send a pulse (not constant HIGH) and when I turn the toggle off it sends another pulse. So basically like a keyboard keystroke, everytime I turn the toggle on or off it's sends a single keystroke or pulse.

If you can point me in the right direction that would be great, I'm not sure if any additional hardware would be needed or this could be done with just the arduino code but I've looked everywhere and can't find an answer.

BTW this video below is the closest answer I have found and it works in minecraft but how do I get it in the Real World?

Thanks in advance,

Kevin

cputampa:
I have a simple task that I just can't wrap my head around. I have a simple toggle switch that I want to act as a button. I want to turn the toggle switch on and it send a pulse (not constant HIGH) and when I turn the toggle off it sends another pulse. So basically like a keyboard keystroke, everytime I turn the toggle on or off it's sends a single keystroke or pulse.

You basically cannot have a toggle switch operate like a momentary push button switch without some other external circuitry added. And while that is possible, it is kind of wasteful way to do it because you can perform that kind of action in your program sketch. You just create a flag variable that you set when your program detects a change of state on the input pin that the toggle switch is wired to.

If you can point me in the right direction that would be great, I'm not sure if any additional hardware would be needed or this could be done with just the arduino code but I've looked everywhere and can't find an answer.

Software is the way to go, but you first have to learn and understand C/C++ coding structure and how it's operators work. Perhaps someone here will show you a basic method. Often you will not be able to 'find' a given software function that you require, but rather you have to create it in code by turning your requirement into a working C/C++ function, that's what learning to program is all about.

BTW this video below is the closest answer I have found and it works in minecraft but how do I get it in the Real World?

Cute video and I guess it does visual describe the function you wish to create and use in a sketch, but not much use in helping you to figure out how to code it in C/C++ language is it?
Lefty

http://www.youtube.com/watch?v=O6rDntYgUkU

Thanks in advance,

Kevin

This is how you do it if you don't mind using delay() for the output pulse timing.

const int switchPin = 5;
const int outputPin = 6;

void setup() {
  pinMode(switchPin, INPUT_PULLUP);
  pinMode(outputPin, OUTPUT);
}


void loop() {
  static boolean oldSwitchState = digitalRead(switchPin);
  boolean newSwitchState = digitalRead(switchPin);
  if (newSwitchState != oldSwitchState) {
    // Switch has changed state.  Remember the new state
    oldSwitchState = newSwitchState;
    digitalWrite(outputPin, HIGH);
    delay(100);  // Pulse length
    digitalWrite(outputPin, LOW);
  }
}

code is untested.

edit* moving it into setup and loop to make it clearer

boolean toggleState; // hold current state of switch
boolean lastToggleState;  // hold last state to sense when switch is changed
long toggleTimer = millis();  // // debounce timer

void setup(){
// setup stuff here
}

void loop(){

toggleState = digitalRead(toggleSwitchPin); 

if (millis() - toggleTimer > 100){  // debounce switch 100ms timer
   if (toggleState && !lastToggleState) {  // if switch is on but was previously off
     lastToggleState = true;  // switch is now on
     toggleTimer = millis();  // reset timer
     // do stuff here
   }

   if (!toggleState && lastToggleState){  // if switch is off but was previously on
     lastToggleState = false; // switch is now off
     toggleTimer = millis(); // reset timer
     // do stuff here
    }
  }
}

If you want the same stuff to happen in each case then make a separate function and just call to the function in each case.

There are more efficient ways to do this as well but this is one of the easiest to understand in my opinion.

Thanks Guys this works perfect. It's exactly what I was looking for.

Hi John,
I'm hoping you can help me. your code pulses on the on and off of the toggle switch, is there a way of only pulses for the on part?
(apologies for bringing an old post but i can't find a solution for what i'm after)