2 Way On-On switch

So I found some random bits of code for making a button box, but I'm trying to use a two way on-on switch rather than a button. As a result, when the switch is in either on position, it continually outputs the value rather than just giving one single value. Was wondering if there was any way around and to just send one signal to the keyboard instead of repeated signals. This is what I got so far:

#include "Keyboard.h"

void setup() {
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);

Keyboard.begin();

}

void loop()
{
if(digitalRead(9) == LOW)
{
Keyboard.print("1");
delay(500);
Keyboard.releaseAll();
}
if(digitalRead(10) == LOW)
{
Keyboard.print("2");
delay(500);
Keyboard.releaseAll();
}
}

Thanks, Dan

Detect when the input becomes LOW rather than when it is LOW

See state change detection tutorial

and USE CODE TAGS !

you need an extra variable to remember what the last reading was from the pin.

this way you can compair it to the new reading to detect if there was a change

#include "Keyboard.h"

int old9;
int old10;
void setup() {
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
 
  Keyboard.begin();

}

void loop() 
{ 
  int r;
  r = digitalRead(9);
  if(r!= old9) {old9=r;
  if(r==LOW){
     Keyboard.print("1");
      delay(500);
      Keyboard.releaseAll(); }
   }
   
  
  r = digitalRead(10);
  if(r!= old10) {old10=r;
  if(r==LOW){
      Keyboard.print("2");
      delay(500);
      Keyboard.releaseAll();}    
    }
}

if you want to be more sure sometimes i wait for a few consecutive readings of the same type to ignore any bounce or interference in your switch but it's more coding. usually the simple answer above is sufficient

Thankyou <3 That code works great and I wasn’t sure where I could find the tutorials but the link to the state change tutorial is perfect. Maybe now I can actually use code tags because I actually understand what’s going on… kinda. Might take some more time and reading lol.