buttons combined

Hi,

I am trying to figure out a way to make 2 buttons work together in one code. I have no code so far since I don't know how I could do it.
By that I mean;
If button A is pressed, type letter "A"
If button B is pressed, type letter "B"
If both buttons A and B are pressed, type letter "C"
Is there a way to make this possible?

I am using an Arduino micro for the ones who would like to know.

Best regards,

Devon

Guess you have to explain what it is you actually mean by "typing" a letter? :roll_eyes:

mgysgtdevon:
I am using an Arduino micro for the ones who would like to know.

Somewhat odd choice but ...

i mean by using
#include <Keyboard.h>
keyboard.print(“a”)

i use a micro so i could use it as a keyboard

I assume that the 2 buttons you refer to are connected to pins on the Arduino.

Have you looked at, tried and understood the Button example in the IDE ? Taking an input HIGH causes an LED to light but it could equally do something else

Hi,
Can you tell us your electronics, programming, arduino, hardware experience?

Have you written a press button code?
How will you have your button connected to the controller.

Have you checked if your controller will work with keyboard function?

https://www.arduino.cc/reference/en/language/functions/usb/keyboard/
Look at "notes and warnings"

Tom... :slight_smile:

Hi all,

i have 2 basic pushbuttons connected to ground and digital pin 10 and digital pin 11.
the code i basicly use for pushbuttons is:
(i got this code from a friend of mine)

#include <Keyboard.h>

const byte switchPinA = 10;
const byte switchPinB = 11;
byte oldSwitchStateA = HIGH;  // assume switch OFF because of pull-up resistor
byte oldSwitchStateB = HIGH;  // assume switch OFF because of pull-up resistor
const unsigned long debounceTime = 10;  // milliseconds



void setup() {
  Serial.begin(9600);
  pinMode (switchPinA, INPUT_PULLUP);
  pinMode (switchPinB, INPUT_PULLUP);
}

void loop() 
{
  byte switchStateA = digitalRead (switchPinA);

  if (switchStateA != oldSwitchStateA)
  {
    oldSwitchStateA =  switchStateA;
    delay (debounceTime);
    if (switchStateA == LOW)
    {
      Keyboard.press('a');
      delay(10);
      Keyboard.release('a');
    }
  }


  byte switchStateB = digitalRead (switchPinB);

  if (switchStateB != oldSwitchStateB)
  {
    oldSwitchStateB =  switchStateB;
    delay (debounceTime);
    if (switchStateB == LOW)
    {
      Keyboard.press('b');
      delay(10);
      Keyboard.release('b');
    }
  }
}

but now i would like to combine 2 buttons to send out a different keyboard.press when both buttons are pressed

with the help of someone i managed to get it working,

the idea was that when button1 pressed, A happens, then if button1 is still pressed when button2 becomes pressed C goes not B.
with “A” goes i mean it sends out a keyboard.print(“A”)

by using a boolean flag called “oneButtonPressed” to signify that we’re waiting for a second press after either button is pressed.

here is the new code

#include <Keyboard.h>

const byte switchPinA = 10;
const byte switchPinB = 11;
byte oldSwitchStateA = HIGH;  // assume switch OFF because of pull-up resistor
byte oldSwitchStateB = HIGH;  // assume switch OFF because of pull-up resistor
const unsigned long debounceTime = 10;  // milliseconds
bool oneButtonPressed = false;

void setup()
{
  Serial.begin(9600);
  pinMode (switchPinA, INPUT_PULLUP);
  pinMode (switchPinB, INPUT_PULLUP);
  Serial.println("Press a button or two...");
}

void loop()
{
  byte switchStateA = digitalRead (switchPinA);

  if (switchStateA != oldSwitchStateA)
  {
    oldSwitchStateA =  switchStateA;
    delay (debounceTime);
    if (switchStateA == LOW) //pressed
    {
      if (oneButtonPressed)
      {
        oneButtonPressed = false;
        Serial.println("c");
      }
      else
      {
        Serial.println("a");
        oneButtonPressed = true;
      }
    }// new press of A
   
    if (switchStateA == HIGH) //release of A so cancel
    {
      oneButtonPressed = false;
    }
  }


  byte switchStateB = digitalRead (switchPinB);

  if (switchStateB != oldSwitchStateB)
  {
    oldSwitchStateB =  switchStateB;
    delay (debounceTime);
    if (switchStateB == LOW)
    {

      if (oneButtonPressed)
      {
        oneButtonPressed = false;
        Serial.println("c");
      }
      else
      {
        Serial.println("b");
        oneButtonPressed = true;
      }
    }// new press of B
   
    if (switchStateB == HIGH) //release of B so cancel
    {
      oneButtonPressed = false;
    }
  }
}