SP3T - Keyboard.press

When the switch is in the top position I want it to "keyboard.press" say "T", when it's pushed into the middle position, I want nothing to print, and finally in the bottom position I want it to output say "B". How can I do this?

How can I do this?

That depends on how the switch is connected to the Arduino. No data == no help.

Top pin - Nothing
Middle pin - Input
Bottom pin - Ground

Assuming its an SPDT. You'll want to connect ground to the common terminal, and a separate input to each of the others.

Then you do precisely what you currently do in your code, and treat that other input line as a separate input, pressing whatever key you want it to be.

Just make sure you connect ground to the common pin, that really is the only place you could cock up.

Common pin meaning middle?

Don't know :wink: Usually, but not sure on your switch.

Get your multimeter out, and test each set with switch in each direction. You'll find the common one.

This code slightly works. When the switch is pushed from middle down to the bottom position (pin 8, I think) It press "g" and when it's moved back to the middle, it presses "g" again. Same with the up position. But when it's flicked into the middle, I don't want anything to happen. I don't know how I could do this.

bool g_toggle = false;// put this line with your globals
///////////// Pin 7 ////////////////////
int switchState_7 = 0;         
int lastswitchState_7 = 0;
int SwitchPin_7 = 7;
char pin_input_7 = 'f';
///////////// Pin 8 ////////////////////
int switchState_8 = 0;         
int lastswitchState_8 = 0;
int SwitchPin_8 = 8;
char pin_input_8 = 'g';




void setup() 
{
 pinMode(SwitchPin_7,INPUT_PULLUP);
 pinMode(SwitchPin_8,INPUT_PULLUP);
}

void loop() 
{
////////////////////// Pin 7 /////////////////////
   switchState_7 = digitalRead(SwitchPin_7);
   delay(50); // contact debounce
    if (switchState_7 != lastswitchState_7){
        Keyboard.press(pin_input_7);
        delay(50);
        Keyboard.releaseAll();
      }
   lastswitchState_7 = switchState_7;
////////////////////// Pin 8 /////////////////////
   switchState_8 = digitalRead(SwitchPin_8);
   delay(50); // contact debounce
    if (switchState_8 != lastswitchState_8 ){
        Keyboard.press(pin_input_8);
        delay(50);
        Keyboard.releaseAll();
    }
     lastswitchState_8 = switchState_8;

}

Did you figure out the wiring yet? There is no point in messing around in code until you know the hardware is behaving as you expect.

For the record your code is sending a keystroke each time your pin changes state, so it sends when going from HIGH->LOW and LOW->HIGH.

Yeah, the hardware is good, how do I do this "Low to High and High to Low" function?

jAMDup:
Yeah, the hardware is good,

So you have two inputs from your switch?

how do I do this "Low to High and High to Low" function?

You already are doing that - thats why you get the letter printed when switching on and switching off.

Yeah, two inputs. But the problem is, when the switch is flicked into the middle, it prints the last key press again.
For example

Position:

Middle to top -> F
Top to middle -> F

Middle to bottom -> G
Bottom to Middle -> G

I don't want the "strikouts" to happen

Try this, depending on how you wired up, you'll either get a press on the contact or the when you break.

void loop() 
{
////////////////////// Pin 7 /////////////////////
	switchState_7 = digitalRead(SwitchPin_7);
	delay(50); // contact debounce
	if (switchState_7 == HIGH && lastswitchState_7 == LOW)
	{
		Keyboard.press(pin_input_7);
		delay(50);
		Keyboard.releaseAll();
	}
	lastswitchState_7 = switchState_7;
////////////////////// Pin 8 /////////////////////
	switchState_8 = digitalRead(SwitchPin_8);
	delay(50); // contact debounce
	if (switchState_8 == HIGH && lastswitchState_8 == LOW )
	{
		Keyboard.press(pin_input_8);
		delay(50);
		Keyboard.releaseAll();
	}
	lastswitchState_8 = switchState_8;
}

It works on break, I like it to work on contact. But it does function properly, thanks for your help.

Then switch the HIGH and LOW round and it'll work on contact :wink: