Using multiple button presses

#include <Bounce2.h>
#include <Keyboard.h>

/*
 USB Keyboard
 For the Arduino Leonardo and Micro.
 Sends a defined text string when a button is pressed.
 The circuit: pushbutton attached from pin x to GND
 Copyright (c) 2017 Rok Rodič alias Hacker007 alias GreenEyedExplorer, http://www.rodic.si, The MIT License (MIT)
 See: http://www.arduino.cc/en/Tutorial/KeyboardMessage
 */

#include "Keyboard.h"
#include "Bounce2.h"

const int buttonPin1 = 7;
const int buttonPin2 = 5;
const int buttonPin3 = 3;
const int buttonPin4 = 8;
int previousButtonState1 = HIGH;
int previousButtonState2 = HIGH;
int previousButtonState3 = HIGH;
int previousButtonState4 = HIGH;

Bounce debouncer1 = Bounce();
Bounce debouncer2 = Bounce();
Bounce debouncer3 = Bounce();
Bounce debouncer4 = Bounce();

void setup() {
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);
  pinMode(buttonPin4, INPUT_PULLUP);
  debouncer1.attach(buttonPin1); debouncer1.interval(50);
  debouncer2.attach(buttonPin2); debouncer2.interval(50);
  debouncer3.attach(buttonPin3); debouncer3.interval(50);
  debouncer4.attach(buttonPin3); debouncer4.interval(50);
  Keyboard.begin();
}

void loop() {
  debouncer1.update();
  debouncer2.update();
  debouncer3.update();
  debouncer4.update();
  //int val1 = debouncer1.read();
  int val2 = debouncer2.read();
  int val3 = debouncer3.read();
  int val4 = debouncer4.read();
  
  if (debouncer1.fell()) { Keyboard.print(" "); } 
  //if (debouncer2.fell()) { Keyboard.press(KEY_UP_ARROW); delay(1); Keyboard.release(KEY_UP_ARROW); }
  //if (debouncer3.fell()) { Keyboard.press(KEY_DOWN_ARROW); delay(1); Keyboard.release(KEY_DOWN_ARROW); }
  //if (debouncer4.fell()) { Keyboard.press(KEY_LEFT_ALT); delay(1); }
  //if (debouncer4.fell()) { Keyboard.write('s'); delay(1); Keyboard.releaseAll}
  if (val2==LOW) { Keyboard.press(KEY_UP_ARROW); delay(10); Keyboard.release(KEY_UP_ARROW); }
  if (val3==LOW) { Keyboard.press(KEY_DOWN_ARROW); delay(10); Keyboard.release(KEY_DOWN_ARROW); }
  if (val4==LOW) { Keyboard.press(KEY_LEFT_ALT); delay(10); }
  if (val4==LOW) { Keyboard.write('s'); delay(10); Keyboard.releaseAll }
  

  delay(25); //25=4rows 35=
}

Hi there, I am trying to add something to a code someone has uploaded on youtube. I have no previous experience so having a bit of trouble here. Below is the original code which works just fine. I am using a music software for live gigs and I have a software with a button labelled "show song". I am designing a 4 button keyboard emulation using an arduino Leonardo. The pedal will have :

  1. space bar assignged to Play/pause
  2. arrow up assigned for scrolling up the song list
  3. arrow down for scrolling down the song list
  4. Alt-s assigned to "show songs" which then looads the next song in the software.

As you can see from the code the first 3 buttons:

const int buttonPin1 = 7;
const int buttonPin2 = 5;
const int buttonPin3 = 3;

all work fine.

I need to add code for the following pin. As you can see I have started but got a bit lost when it got to the loop. Can someone please check and tell me what I need to do please.

Basically all refernces to button 4 are things that I have added but unsure if I am doing it correctly. Also I could not figure out how to code for Alt-s at the same time so I have not entered anything thus far as all my trials have resulted in errors.

Many thanks for any help.

regards

Jim

At first glance except it looking like it's written by a monkey it look fine. But does it work? Can you at least print a 's'?

My untested attempt to make it less monkey-written:

#include "Keyboard.h"
#include "Bounce2.h"

const byte ButtonPins[] = {7, 5, 3, 8};
const byte NrButtons = sizeof(ButtonPins);
const byte KeyPresses[NrButtons] = {  '\0', 
                                      '\0', 
                                      '\0', 
                                      KEY_LEFT_ALT};
const byte KeyWrites[NrButtons] = { ' ',
                                    KEY_UP_ARROW,
                                    KEY_DOWN_ARROW,
                                    's'};

Bounce buttons[NrButtons];

void setup(){
  for(byte i = 0; i < NrButtons; i++){
    buttons[i].attach(ButtonPins[i], INPUT_PULLUP);
  }
  
  Keyboard.begin();
}

void loop(){
  for(byte i = 0; i < NrButtons; i++){
    buttons[i].update();
    
    if(buttons[i].fell()){
      if(KeyPresses[i] != '\0'){
        Keyboard.press(KeyPresses[i]);
        delay(10);
      }
      if(KeyWrites[i] != '\0'){
        Keyboard.write(KeyWrites[i]);
      }
      Keyboard.releaseAll();
    }
  }
}

kaborex:
I am using a music software for live gigs and I have a software with a button labelled "show song".

Have you considered using MIDI? It's supported by all music software I know, and its entire purpose is to control the software with a physical device.
This means you don't have to use keyboard shortcuts, and it doesn't rely on the window being in focus. (Imagine getting a popup warning during a live performance, and your pedals no longer work, because the warning window is now in focus!)

What software are you using?

Pieter

Hi all. Thanks for the quick replies. I have searched further on youtube and found another code which I managed to edit and get it to work. Long time musician but midi always scared me ... lol

Written by a monkey LOL

I will try the above ammended code as well and feedback.

This is what I came up with:

Hi all.  Quick update.  I managed to do it in a more simplified script.  This video was inspirational to get me started and I am very thankful for all your replies to my queries. See code below whci I modified from another video off youtube.  In the last few lines Keyboard.press is used for triggering (KEY_LEFT_ALT) because Keyboard.press is used when other keys will be used in conjunction with say for example Ctrl key etc ... in this case I needed to press two keys.  The Alt and S key.  So  I used Keyboard.press for the Alt key and used  Keyboard.write for the S key.  I had to use Keyboard.release after to stop both keys.


}

//---------------------------------------------------------
//                           Loop
//---------------------------------------------------------

void loop() {
  
  
 Keyboard.begin();         //begin keyboard 
 if (digitalRead(7) == 0)  // if button 7 is pushed
  {
    Keyboard.write((char)0x20);  // press space bar
    delay(200);           // delay so you don't get 20 spacebar presses
  }
  else if (digitalRead(5) == 0){  // if button 5 is pressed
    Keyboard.write(KEY_DOWN_ARROW); // press Down arrow key
    delay(200);
}
  else if (digitalRead(3) == 0){  //if button 3 is pressed
    Keyboard.write(KEY_UP_ARROW);         // press up arrow key
    delay(200);
  }

  else if (digitalRead(8) == 0){  //if button 8 is pressed
    Keyboard.press(KEY_LEFT_ALT);delay(200);        // press up alt key
    Keyboard.write ('S'); delay(200); 
    Keyboard.release ('S'); 
    Keyboard.release (KEY_LEFT_ALT);   //Press s
  }

  Keyboard.end();                 //stops keyboard
}

Delta_G:
Dude, that's like saying, "I'm a longtime musician but I only play in C or Am because the sharps and flats scare me." First off, midi ins't scary. Secondly, if you never try to learn then it only holds you back.

Couldn't have said it better myself :slight_smile:

MIDI Controller Library

Try it: example

Well I blame you guys for this. That is now my next project. Honestly never ventured into midi but Sounds like a good next project.

Will keep you posted.

The pedal I was working on now works fully.

I have added more buttons. I now have 7 as the scrolling through songs was too slow and added two more buttons for page up/down and also one additional button to call up a menu. Works great. Final code is as follows (some spelling errors that I could not be bothered to change):

/*
 * Arduino Keyboard Emulation
 * learnelectronics
 * 13 FEB 2017
 * 
 * www.youtube.com/c/learnelectronics
 */

#include <Keyboard.h>    // This is a "built-in" library no need to install

//---------------------------------------------------------
//                           Setup
//---------------------------------------------------------



void setup() {
pinMode(7,INPUT_PULLUP);  // sets pin 7to input & pulls it high w/ internal resistor
pinMode(5,INPUT_PULLUP);  // sets pin 5 to input & pulls it high w/ internal resistor
pinMode(3,INPUT_PULLUP);  // sets pin 3 to input & pulls it high w/ internal resistor
pinMode(8,INPUT_PULLUP);  // sets pin 8 to input & pulls it high w/ internal resistor
pinMode(9,INPUT_PULLUP);  // sets pin 9 to input & pulls it high w/ internal resistor
pinMode(10,INPUT_PULLUP);  // sets pin 10 to input & pulls it high w/ internal resistor
pinMode(11,INPUT_PULLUP);  // sets pin 11 to input & pulls it high w/ internal resistor

Serial.begin(9600);       // begin serial comms for debugging

}

//---------------------------------------------------------
//                           Loop
//---------------------------------------------------------

void loop() {
  
  
 Keyboard.begin();         //begin keyboard 
 if (digitalRead(10) == 0)  // if buton 10 is pushed
  {
    Keyboard.write(KEY_PAGE_DOWN);  // press pagedown
    delay(200);           // delay so you don't get 20 pagedown presses
  }
 else if (digitalRead(9) == 0)  // if buton 9 is pushed
  {
    Keyboard.write(KEY_PAGE_UP);  // press page up
    delay(200);           // delay so you don't get 20 pageup presses
  }
 else if (digitalRead(11) == 0)  // if buton 11 is pushed
  {
    Keyboard.write(KEY_F5);  // press F5
    delay(200);           // delay so you don't get 20 F5
  }
 else if (digitalRead(7) == 0)  // if buton 7 is pushed
  {
    Keyboard.write((char)0x20);  // press space bar
    delay(200);           // delay so you don't get 20 spacebar presses
  }
  else if (digitalRead(5) == 0){  // if button 5 is pressed
    Keyboard.write(KEY_DOWN_ARROW); // press Down arrow key
    delay(200);
}
  else if (digitalRead(3) == 0){  //if button 3 is pressed
    Keyboard.write(KEY_UP_ARROW);         // press up arrow key
    delay(200);
  }

  else if (digitalRead(8) == 0){  //if button 8 is pressed
    Keyboard.press(KEY_LEFT_ALT);delay(200);        // press up alt key
    Keyboard.write ('S'); delay(200); 
    Keyboard.release ('S'); 
    Keyboard.release (KEY_LEFT_ALT);   //Press s
  }

  Keyboard.end();                 //stops keybord
}

Although Midi 2.0 might be a little bit more scary xD