Toggle switch acting as a button

Im trying to build a button box for my pc

Im new to all this and im certainly struggling with it lol

I have a toggle switch (on/off) and Im trying to make it so that the input is one direction is a button press then when i switch it, it becomes another button press.

so say I want the "CTRL" pressed once when toggle switch is switched to on and then "CTRL" pressed again when switched back off.

Im pretty sure the solution lies within State change detection but im at a loss. the thing im trying to build only consists of 3 switches.

Any help on this would be much appreciated! Im using a Leonardo

Do you mean, push in one direction the code does one thing, push in the other direction, the code does something different?

.

Get rid of the if (buttonState == HIGH) test. Then the code will trigger on every state change, not just changes in one direction.

So, if the state of the toggle is not the same as what it was last time you executed the loop(), then treat that as a button press.

But ignore state changes that occur less than 10 milliseconds apart, as that's probably a bounce.

Ok so I got the state change to work but how do make it so that I only get one button press? right now I just get the eternal repeating text. Thank again!

cslatch:
Ok so I got the state change to work but how do make it so that I only get one button press? right now I just get the eternal repeating text. Thank again!

Always show us your current compete sketch. Use CTRL T to format the sketch. Please use code tags. Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

.

Unfortunately the computer I'm using to write the code for doesn't have internet. I was just seeing if anyone had any common lines that could stop the button presses

We can help you if you help us.

Copy your sketch to a thumb drive, post it from computer that has internet access.

.

cslatch: Unfortunately the computer I'm using to write the code for doesn't have internet. I was just seeing if anyone had any common lines that could stop the button presses

It's included in the IDE. It's in the examples. It's called "State Change" It shows how to react only once when the state of a pin changes.

If the computer has no internet access how did you install the IDE ?

.

ok so after a tremendous amount of struggle and turning my board into an uncontrollable key press spamming device. Ive managed to come up with 3 separate sketches for 3 separate actions I want the board todo but I have no idea how to merge all 3 together. "lights" and "ignition" are both toggle switches and "starter" is a momentary button.

Light Sketch

const int headlightPin = 4;
#include "Keyboard.h";
int lastheadlightState = 0;
void setup() {
  pinMode(headlightPin, INPUT);
  Keyboard.begin();
}
void loop () {
  int headlightState = digitalRead(headlightPin);
  delay(50);
  if ((headlightState != lastheadlightState)) {
    Keyboard.print("l");
  }
  lastheadlightState = headlightState;
}

Ignition Sketch

const int ignitionPin = 2;
const int headlightPin = 4;
#include "Keyboard.h";
int lastignitionState = 0;
int lastheadlightState = 0;
void setup(){
  pinMode(ignitionPin,INPUT);
  pinMode(headlightPin,INPUT);
  Keyboard.begin();
}
void loop (){
  int ignitionState=digitalRead(ignitionPin);
  int headlightState=digitalRead(headlightPin);
  delay(50);
  if((ignitionState !=lastignitionState)){
  Keyboard.print("i"); 
  }
  if((headlightState !=lastheadlightState)){
  Keyboard.print("h");  
}
lastignitionState = ignitionState;
lastheadlightState = headlightState;
}

Starter Sketch

const int starterPin = 7;
#include "Keyboard.h";
int starterState =0;
int laststarterState=0;
void setup() {
pinMode (starterPin,INPUT);
Keyboard.begin();
}

void loop() {
int starterState=digitalRead(starterPin);
delay(50);
if ((starterState ==HIGH)){
  Keyboard.print("s");
}
}

Thanks again!

"but I have no idea how to merge all 3 together."

Merging code: http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

.