Switch state dilemma

Hi guys,

i'm trying to build up a button box for my simulator and ran across a major problem (at least for my skill set). Here is what i want to do:
Have 3 Buttons which act like this-> pressing will give a signal (button push), releasing the same button will also give the same or a different button push.
Plus having several other buttons acting like normal buttons do (you have to push them to get a signal and that's it).
Preferably it would be nice to use the joystick.h library, since i already have a Arduino as a keyboard (for my other button box).
I already found this example here on the forum, but i'm really struggeling to have more than one key/button and to "translate" it to the joystick library.
Maybe someone would be kind enough to help me out with this.
Thanks in advance for all inputs and help!

#include <Keyboard.h>

const byte switchPin = 8;
byte oldSwitchState = HIGH;  // assume switch open because of pull-up resistor
const unsigned long debounceTime = 10;  // milliseconds

void setup ()
  {
  Serial.begin (9600);
  pinMode (8, INPUT_PULLUP);
  }  // end of setup

void loop ()
  {
  Keyboard.begin();
  // see if switch is open or closed
  byte switchState = digitalRead (switchPin);
  
  // has it changed since last time?
  if (switchState != oldSwitchState)
    {
    oldSwitchState =  switchState;  // remember for next time 
    delay (debounceTime);   // debounce
    if (switchState == LOW)
       {
       Keyboard.press('A');
       delay(100);
       Keyboard.releaseAll();
       }  // end if switchState is LOW
    else
       {
       Keyboard.press('B');
       delay(100);
       Keyboard.releaseAll();
       }  // end if switchState is HIGH
    }  // end of state change
     
  // other code here ...
   
  } 

Please insert the code using code tags

unclear what you're trying to do.
looks like your trying to synthesize keyboard presses using momentary switches connected to I/O pins

No it's not lol....i want to build a joystick 10 buttons,of which 3 need to have a "special" function, which gpes like this:

Button push -> print "a"
Button release-> print "b" or same as push button

To detect button press and button release, study the State Change Detection example built in to the Arduino IDE.

Found a solution

include <Keyboard.h>

const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 7;       // the pin that the LED is attached to
int buttonState = 1;         // current state of the button, HIGH as we have active low with INPUT_PULLUP
int lastButtonState = 1;     // previous state of the button

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  Keyboard.begin();
}

void loop() {
  buttonState = digitalRead(buttonPin);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {

    if (buttonState == LOW) {  // if the current state is pressed as we use INPUT_PULLUP
      // wend from off to on:
      Keyboard.write('d');
            Keyboard.release('d');

      digitalWrite(ledPin, HIGH);

    } else {
      // if the current state is LOW then the button
      // wend from on to off:
            Keyboard.write('D');

      Keyboard.release('D');
      
      digitalWrite(ledPin, LOW);
    }
    // Delay a little bit to avoid bouncing
    //delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.