Help with a SPST toggle

Hi everyone,

I am very new to programming and I just can't find the info for turning a simple SPST toggle switch into a keyboard input at both the ON and OFF positions without locking up the keyboard.

I wrote a code that works with the switches but needs TWO TOGGLES in order to change the state and send the Keyboard.write.

I read the StateChangeDetection and tried to implement it but that's when my code went to bits...

Please can anyone help? I have probably over complicated this but it is driving me wild now being so close!!!

Thanks in advance for any help offered!
Col

#include <Keyboard.h>

int redLED=13;
int switch1=16;
int switch2=11;
int switch3=10;
int switch4=9;
int switch5=8;
int switch6=7;
int switch7=6;
int switch8=5;
int switch9=4;
int switch10=3;
int switch11=2;

int del1=70;

int switch1Old=0;
int switch1New;
int switch1State=1;

int switch2Old=0;
int switch2New;
int switch2State=1;

int switch3Old=0;
int switch3New;
int switch3State=1;

int switch4Old=0;
int switch4New;
int switch4State=1;

int switch5Old=0;
int switch5New;
int switch5State=1;

int switch6Old=0;
int switch6New;
int switch6State=1;

int switch7Old=0;
int switch7New;
int switch7State=1;

int switch8Old=0;
int switch8New;
int switch8State=1;

int switch9Old=0;
int switch9New;
int switch9State=1;

int switch10Old=0;
int switch10New;
int switch10State=1;

int switch11Old=0;
int switch11New;
int switch11State=1;


void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);

pinMode(redLED,OUTPUT);
pinMode(switch1,INPUT_PULLUP);
pinMode(switch2,INPUT_PULLUP);
pinMode(switch3,INPUT_PULLUP);
pinMode(switch4,INPUT_PULLUP);
pinMode(switch5,INPUT_PULLUP);
pinMode(switch6,INPUT_PULLUP);
pinMode(switch7,INPUT_PULLUP);
pinMode(switch8,INPUT_PULLUP);
pinMode(switch9,INPUT_PULLUP);
pinMode(switch10,INPUT_PULLUP);
pinMode(switch11,INPUT_PULLUP);

Keyboard.begin();

}

void loop() {

switch1New=digitalRead(switch1);
if(switch1Old==1 && switch1New==0){
  if(switch1State==1){
    digitalWrite(redLED,HIGH);
    Keyboard.write("d");
    switch1State=0;}
  else{digitalWrite(redLED,LOW);
  Keyboard.write("d");
    switch1State=1;}}
switch1Old=switch1New;


switch2New=digitalRead(switch2);
if(switch2Old==1 && switch2New==0){
  if(switch2State==1){
    digitalWrite(redLED,HIGH);
    Keyboard.write("s");
    switch2State=0;}
  else{digitalWrite(redLED,LOW);
  Keyboard.write("s");
    switch2State=1;}}
switch2Old=switch2New;


switch3New=digitalRead(switch3);
if(switch3Old==1 && switch3New==0){
  if(switch3State==1){
    digitalWrite(redLED,HIGH);
    Keyboard.write("a");
    switch3State=0;}
  else{digitalWrite(redLED,LOW);
  Keyboard.write("a");
    switch3State=1;}}
switch3Old=switch3New;


switch4New=digitalRead(switch4);
if(switch4Old==1 && switch4New==0){
  if(switch4State==1){
    digitalWrite(redLED,HIGH);
    Keyboard.write("x");
    switch4State=0;}
  else{digitalWrite(redLED,LOW);
  Keyboard.write("x");
    switch4State=1;}}
switch4Old=switch4New;


switch5New=digitalRead(switch5);
if(switch5Old==1 && switch5New==0){
  if(switch5State==1){
    digitalWrite(redLED,HIGH);
    Keyboard.write("z");
    switch5State=0;}
  else{digitalWrite(redLED,LOW);
  Keyboard.write("z");
    switch5State=1;}}
switch5Old=switch5New;

switch6New=digitalRead(switch6);
if(switch6Old==1 && switch6New==0){
  if(switch6State==1){
    digitalWrite(redLED,HIGH);
    Keyboard.write("c");
    switch4State=0;}
  else{digitalWrite(redLED,LOW);
  Keyboard.write("c");
    switch6State=1;}}
switch6Old=switch6New;

switch7New=digitalRead(switch7);
if(switch7Old==1 && switch7New==0){
  if(switch7State==1){
    digitalWrite(redLED,HIGH);
    Keyboard.write("t");
    switch7State=0;}
  else{digitalWrite(redLED,LOW);
  Keyboard.write("t");
    switch7State=1;}}
switch7Old=switch7New;

switch8New=digitalRead(switch8);
if(switch8Old==1 && switch8New==0){
  if(switch8State==1){
    digitalWrite(redLED,HIGH);
    Keyboard.write("y");
    switch8State=0;}
  else{digitalWrite(redLED,LOW);
  Keyboard.write("y");
    switch8State=1;}}
switch8Old=switch8New;

switch9New=digitalRead(switch9);
if(switch9Old==1 && switch9New==0){
  if(switch9State==1){
    digitalWrite(redLED,HIGH);
    Keyboard.write("u");
    switch9State=0;}
  else{digitalWrite(redLED,LOW);
  Keyboard.write("u");
    switch9State=1;}}
switch9Old=switch9New;

switch10New=digitalRead(switch10);
if(switch10Old==1 && switch10New==0){
  if(switch10State==1){
    digitalWrite(redLED,HIGH);
    Keyboard.write("r");
    switch10State=0;}
  else{digitalWrite(redLED,LOW);
  Keyboard.write("r");
    switch10State=1;}}
switch10Old=switch10New;

switch11New=digitalRead(switch11);
if(switch11Old==1 && switch11New==0){
  if(switch11State==1){
    digitalWrite(redLED,HIGH);
    Keyboard.write("f");
    switch11State=0;}
  else{digitalWrite(redLED,LOW);
  Keyboard.write("f");
    switch11State=1;}}
switch11Old=switch11New;

delay(del1);

}

check out the Toggle library from @dlloyd. it will make your life easier

For readability, please format your sketch use Ctrl T or CMD T

Place { and } on separate lines by themselves.

What does this mean?

A toggle-switch is a switch let's say you start at switch is in off position
first press => switch ON
second press => switch is OFF
third press => switch is ON
fourth press => switch is OFF

and your code shall translate into
regardless of switch beeing in position ON or OFF
whenever switch gets pressed do a

Keyboard.write();

?

is this correct?

best regards Stefan

Which "Keyboard.h" library are you using (link).

Because the "official" library:
"Keyboard - Arduino Reference
"https://docs.arduino.cc/built-in-examples/usb/KeyboardSerial

Using the library example, it does not compile correctly either in my IDE or in the simulator:
"keyboard - Wokwi ESP32, STM32, Arduino Simulator

I believe there are problems in this library.

Hi Stefan,

I have a physical single pole toggle switch.

I need each position to write a keystroke.

I.e.
Starting with switch off:

First press ==> switch is ON (send a Keyboard.write "A")
Second press ==> switch is OFF (send a Keyboard.write "A")
Third press==> Switch is ON (send a Keyboard.write "A")
Fourth press ==> switch is OFF (send a Keyboard.write "A")

and so on and so on.

Cheers

Hi ruilviana, the keyboard library works but is occasionally giving me different keys than expected, I can live with that, it is purely the toggling I can not make work correctly.

Thanks, I had a quick look and will see if I can make sense of it for my application....

Hopefully that is better formatted.

#include <Keyboard.h>

int redLED = 13;
int switch1 = 16;
int switch2 = 11;
int switch3 = 10;
int switch4 = 9;
int switch5 = 8;
int switch6 = 7;
int switch7 = 6;
int switch8 = 5;
int switch9 = 4;
int switch10 = 3;
int switch11 = 2;

int del1 = 70;

int switch1Old = 0;
int switch1New;
int switch1State = 1;

int switch2Old = 0;
int switch2New;
int switch2State = 1;

int switch3Old = 0;
int switch3New;
int switch3State = 1;

int switch4Old = 0;
int switch4New;
int switch4State = 1;

int switch5Old = 0;
int switch5New;
int switch5State = 1;

int switch6Old = 0;
int switch6New;
int switch6State = 1;

int switch7Old = 0;
int switch7New;
int switch7State = 1;

int switch8Old = 0;
int switch8New;
int switch8State = 1;

int switch9Old = 0;
int switch9New;
int switch9State = 1;

int switch10Old = 0;
int switch10New;
int switch10State = 1;

int switch11Old = 0;
int switch11New;
int switch11State = 1;


void setup() 
{
  // put your setup code here, to run once:
  Serial.begin(115200);

  pinMode(redLED, OUTPUT);
  pinMode(switch1, INPUT_PULLUP);
  pinMode(switch2, INPUT_PULLUP);
  pinMode(switch3, INPUT_PULLUP);
  pinMode(switch4, INPUT_PULLUP);
  pinMode(switch5, INPUT_PULLUP);
  pinMode(switch6, INPUT_PULLUP);
  pinMode(switch7, INPUT_PULLUP);
  pinMode(switch8, INPUT_PULLUP);
  pinMode(switch9, INPUT_PULLUP);
  pinMode(switch10, INPUT_PULLUP);
  pinMode(switch11, INPUT_PULLUP);

  Keyboard.begin();
}

void loop() 
{

  switch1New = digitalRead(switch1);
  if (switch1Old == 1 && switch1New == 0) 
  {
    if (switch1State == 1) 
    {
      digitalWrite(redLED, HIGH);
      Keyboard.write("d");
      switch1State = 0;
    } 
    else 
    {
      digitalWrite(redLED, LOW);
      Keyboard.write("d");
      switch1State = 1;
    }
  }
  switch1Old = switch1New;


  switch2New = digitalRead(switch2);
  if (switch2Old == 1 && switch2New == 0) {
    if (switch2State == 1) 
    {
      digitalWrite(redLED, HIGH);
      Keyboard.write("s");
      switch2State = 0;
    }
     else 
    {
      digitalWrite(redLED, LOW);
      Keyboard.write("s");
      switch2State = 1;
    }
  }
  switch2Old = switch2New;


  switch3New = digitalRead(switch3);
  if (switch3Old == 1 && switch3New == 0) 
  {
    if (switch3State == 1) 
    {
      digitalWrite(redLED, HIGH);
      Keyboard.write("a");
      switch3State = 0;
    } 
    else 
    {
      digitalWrite(redLED, LOW);
      Keyboard.write("a");
      switch3State = 1;
    }
  }
  switch3Old = switch3New;


  switch4New = digitalRead(switch4);
  if (switch4Old == 1 && switch4New == 0) 
  {
    if (switch4State == 1) 
    {
      digitalWrite(redLED, HIGH);
      Keyboard.write("x");
      switch4State = 0;
    } 
    else 
    {
      digitalWrite(redLED, LOW);
      Keyboard.write("x");
      switch4State = 1;
    }
  }
  switch4Old = switch4New;


  switch5New = digitalRead(switch5);
  if (switch5Old == 1 && switch5New == 0) 
  {
    if (switch5State == 1) 
    {
      digitalWrite(redLED, HIGH);
      Keyboard.write("z");
      switch5State = 0;
    } 
    else 
    {
      digitalWrite(redLED, LOW);
      Keyboard.write("z");
      switch5State = 1;
    }
  }
  switch5Old = switch5New;


  switch6New = digitalRead(switch6);
  if (switch6Old == 1 && switch6New == 0) 
  {
    if (switch6State == 1) 
    {
      digitalWrite(redLED, HIGH);
      Keyboard.write("c");
      switch4State = 0;
    } 
    else 
    {
      digitalWrite(redLED, LOW);
      Keyboard.write("c");
      switch6State = 1;
    }
  }
  switch6Old = switch6New;


  switch7New = digitalRead(switch7);
  if (switch7Old == 1 && switch7New == 0) 
  {
    if (switch7State == 1) 
    {
      digitalWrite(redLED, HIGH);
      Keyboard.write("t");
      switch7State = 0;
    } 
    else 
    {
      digitalWrite(redLED, LOW);
      Keyboard.write("t");
      switch7State = 1;
    }
  }
  switch7Old = switch7New;


  switch8New = digitalRead(switch8);
  if (switch8Old == 1 && switch8New == 0) 
  {
    if (switch8State == 1) 
    {
      digitalWrite(redLED, HIGH);
      Keyboard.write("y");
      switch8State = 0;
    } 
    else 
    {
      digitalWrite(redLED, LOW);
      Keyboard.write("y");
      switch8State = 1;
    }
  }
  switch8Old = switch8New;


  switch9New = digitalRead(switch9);
  if (switch9Old == 1 && switch9New == 0) 
  {
    if (switch9State == 1) {
      digitalWrite(redLED, HIGH);
      Keyboard.write("u");
      switch9State = 0;
    } 
    else 
    {
      digitalWrite(redLED, LOW);
      Keyboard.write("u");
      switch9State = 1;
    }
  }
  switch9Old = switch9New;


  switch10New = digitalRead(switch10);
  if (switch10Old == 1 && switch10New == 0) 
  {
    if (switch10State == 1) 
    {
      digitalWrite(redLED, HIGH);
      Keyboard.write("r");
      switch10State = 0;
    } 
    else 
    {
      digitalWrite(redLED, LOW);
      Keyboard.write("r");
      switch10State = 1;
    }
  }
  switch10Old = switch10New;


  switch11New = digitalRead(switch11);
  if (switch11Old == 1 && switch11New == 0) 
  {
    if (switch11State == 1) 
    {
      digitalWrite(redLED, HIGH);
      Keyboard.write("f");
      switch11State = 0;
    }
     else 
    {
      digitalWrite(redLED, LOW);
      Keyboard.write("f");
      switch11State = 1;
    }
  }
  switch11Old = switch11New;

  delay(del1);
}

More documentation

Your code does check for

if   (switch11Old == 1 && switch11New == 0)

This is only half of what the code should check.
And this is the reason why you always need two changes (button-presses).

The code should also check for the opposite values

if (  (switch11Old == 0 && switch11New == 1)

This would look like

if (  (switch11Old == 1 && switch11New == 0)  || (switch11Old == 0 && switch11New == 1) ) {

And as there are only two values "0" and "1"
this can be coded as the two values are un-equal

The logic operator for unequal is "!="
so it looks like this
check if switch11Old has a different value than switch11New
instead of different value you can say unequal values

check if switch11Old unequal to switch11New

  if ( switch11Old != switch11New ) {

You have written 11 times the exact same code. The only difference is the variable-names.
This cries for using arrays.
You can imagine an array as a big box that has a number of smaller boxes inside.
The smaller boxes are numbered 0....10.
Attention the numbers start at 0. So 0 to 10 are 11 small boxes

With arrays your code boils down to

  switchNew[Index] = digitalRead(switchPin[Index]);
  if ( switchOld[Index]  != switchNew[Index]  ) {
    if (switchState[Index]  == 1) {
      digitalWrite(redLED, HIGH);
      Keyboard.write(myKey[Index] );
      switchState[Index]  = 0;
    }
    else {
      digitalWrite(redLED, LOW);
      Keyboard.write(myKey[Index] );      
      switchState[Index]  = 1;
    }
  }
  switchOld[Index]  = switchNew[Index] ;

where variable Index runs from 0 up to 10

You can imagine an array similar to your variables only the number is not inbetween like in
switch11Old = switch11New;

The number is at the end and in edgy brackets

  switchOld[10]  = switchNew[10] ;

switchOld[0] = switchNew[0] ;
switchOld[1] = switchNew[1] ;
switchOld[2] = switchNew[2] ;
switchOld[3] = switchNew[3] ;
....
switchOld[10] = switchNew[10] ;
...

Though you don't use hard coded numbers you use a variable that runs from
0 to 10

For running the variable Index from 0 to 10 you use a for-loop

best regards Stefan

This means every variable that you "repeated" with a number 1..11 is declared as an array

int switch[11] =  {16,11,10,9,8,7,6,5,4,3,2};

int switchOld[11];

etc. etc. etc.

best regards Stefan

Stefan,

Thank you so much, I have now managed to get my toggle to "toggle" as it should thanks to you!!! I don't quite fully understand the arrays principle you have described (it's 04:50 and I can't get me head around the new terms, so I modified my code and unfortunately wrote it out 11 times again (I'm sorry!))
But I will work on learning what you have said and make it more efficient, this is my very first Arduino project and the beginning of many more that will require multiple toggles so it is certainly in my benefit to learn the principles you have explained.

Once again thank you so much for your help!
Colin