Beginner needs help with button logic

Hello!

I've managed to put together a simple, crude program for Arduino that sends my PC keystrokes via five buttons. I've tested it and it works. I also added a very crude control for LEDs to be illuminated corresponding to each press, until another is selected. I'm sure there is a much more effective way to do that and actually haven't yet soldered the LEDs in place so it's untested.

This, if it works would be quite sufficient for my needs, a 5-button footswitch that sends out letters and shows which one was pressed last.

BUT there is one issue. Should I accidentally press the same button again, it messes up the logic at the PC end, instead of selecting a function, receiving the same command (letter) switches all five off. I can't affect the way it works on the PC so I would like to add a logic that repeated keypresses of the SAME button will be ignored, UNTIL another button has at some point been pressed. I've been able to find out how to do that with timers, but there can be no timer after which the button is again accepted, but it needs to be infinite, so even after a week pressing the same button again does absolutely nothing, BUT at the same time it allows me to press any buttons as rapidly as possible and send out the letters.

and... ?

Sorry pressed send by accident too early :slight_smile:

Here is my program:

#include <Keyboard.h>                               //Sisällytetään näppäimistö-kirjasto
#include <Mouse.h>                                  //Sisällytetään hiiri-kirjasto
#include <Arduino.h>                                //Sisällytetään arduinon käskyjä LEDeille

int pinA = 2;                                       //Annetaan muuttujanimet pinneille
int pinB = 3;
int pinC = 4;
int pinD = 5;
int pinE = 6;

void setup() {

pinMode(pinA, INPUT_PULLUP);                        //Määritellään sisäiset vastukset
pinMode(pinB, INPUT_PULLUP);                        //sekä asetetaan kytkintila pinneille
pinMode(pinC, INPUT_PULLUP);
pinMode(pinD, INPUT_PULLUP);
pinMode(pinE, INPUT_PULLUP);
pinMode(7, OUTPUT);                                 // asetetaan LED output pinnit
pinMode(8, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);

// muistiin: Button 1 = pin 2..jne..Button 5 = pin 6.
// PIn 7 = punainen LED (solo) VALKEA lanka
// PIn 8 = vihreä LED (metal) PUNAINEN lanka
// PIn 10 = sininen LED (clean) Vihreä lanka
// PIn 11 = keltainen LED vasen (rock) valkea lanka
// PIn 12 = keltainen LED oikea (lead) punainen

}

void loop() {

  if (digitalRead(pinA) == LOW)                     //Tsekataan onko Pin 2 kytkintä painettu (vihreäF) 
  {
    Keyboard.write('A');                            //Sending the "A" character
    digitalWrite(8, LOW);                           // Laitetaan muut ledit pois päältä
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    digitalWrite(7, HIGH);                          // Laitetaan päälle LED pinnissä 7
    delay(500);
  }

  if (digitalRead(pinB) == LOW)                     //Tsekataan onko Pin 3 kytkintä painettu (valkea G)
  {
    Keyboard.write('B');                            //Sending the "B" character
    digitalWrite(7, LOW);                           // Laitetaan muut ledit pois päältä
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    digitalWrite(8, HIGH);                          // Laitetaan päälle LED pinnissä 8
    delay(500);
  }

  if (digitalRead(pinC) == LOW)                     //Checking if the third switch has been pressed
  {
    Keyboard.write('C');                            //Sending the "C" character
    digitalWrite(7, LOW);                           // Laitetaan muut ledit pois päältä
    digitalWrite(8, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    digitalWrite(10, HIGH);                          // Laitetaan päälle LED pinnissä 10
    delay(500);
  }

  if (digitalRead(pinD) == LOW)                     //Checking if the fourth switch has been pressed
  {
    Keyboard.write('D');                            //Sending the "D" character
    digitalWrite(7, LOW);                           // Laitetaan muut ledit pois päältä
    digitalWrite(8, LOW);
    digitalWrite(10, LOW);
    digitalWrite(12, LOW);
    digitalWrite(11, HIGH);                          // Laitetaan päälle LED pinnissä 11
    delay(500);
  }

  if (digitalRead(pinE) == LOW)                     //Checking if the fifth switch has been pressed
  {
   Keyboard.write('E');                            //Sending the "E" character
    digitalWrite(7, LOW);                           // Laitetaan muut ledit pois päältä
    digitalWrite(8, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, HIGH);                          // Laitetaan päälle LED pinnissä 12
    delay(500);                                    
  }

Hello, and welcome.

Learn about arrays.

Give your variables meaningful names.

#include <Keyboard.h>                               //Sisällytetään näppäimistö-kirjasto
#include <Mouse.h>                                  //Sisällytetään hiiri-kirjasto
#include <Arduino.h>                                //Sisällytetään arduinon käskyjä LEDeille

const int buttonCount = 5;
const int buttonPin[buttonCount] = {2, 3, 4, 5, 6};                              //Annetaan muuttujanimet pinneille
const int ledPin[buttonCount] = {
  // muistiin: Button 1 = pin 2..jne..Button 5 = pin 6.
  7, //punainen LED (solo) VALKEA lanka
  8, //vihreä LED (metal) PUNAINEN lanka
  10, //sininen LED (clean) Vihreä lanka
  11, //keltainen LED vasen (rock) valkea lanka
  12 //keltainen LED oikea (lead) punainen
};
const char sendKey[buttonCount] = {'A', 'B', 'C', 'D', 'E'};

int lastButton = -1;

void setup() {

  for (int b=0, b<buttonCount, b++) {
    pinMode(buttonPin[p], INPUT_PULLUP);                        //Määritellään sisäiset vastukset
    pinMode(ledPin[p], OUTPUT);                                 // asetetaan LED output pinnit
  }
}

void loop() {

  for (int b=0, b<buttonCount, b++) {
    if (digitalRead(buttonPin[p]) == LOW and lastButton != p ) {                    //Tsekataan onko Pin 2 kytkintä painettu (vihreäF) 
      Keyboard.write(sendKey[p]);                            //Sending the character
      if (lastButton >= 0) digitalWrite(ledPin[lastButton], LOW);                           // Laitetaan muut ledit pois päältä
      digitalWrite(ledPin[p], HIGH);                          // Laitetaan päälle LED pinnissä 7
      lastButton = p;
      delay(50); // debounce
    }
  }
}
1 Like

Hello deeaa from the other side of the Baltic Sea :slight_smile:

Welcome to the world's best Arduino forum ever.

Do you have experience with programming in C++?

Which Arduino are you using for your project?

My recommendation is to use a structured array.

This structured array contains all information about the pins and a service to debounce the pins and a service to detect state changes.
The results of the status change detection can be further processed to transmit letters as specified.

Have a nice day and enjoy coding in C++.

1 Like

I did do a few basic courses in C++ waaaaay back in my 20s during the 1990s....haven't coded anything since, forgotten most of it. But that basic idea of how code works has certainly helped me to take some examples and figure out how to create at least something.

The thing is, I don't want to spend way too much time&effort on this, because I don't really know if I will ever need Arduino for anything else besides this project for making this controller. Possibly improved versions of, but that's about it :slight_smile: I normally use MIDI and for that there are lots of controllers. This one music project however proved to be next to impossible to program MIDI for, but very easy on the keyboard, so it dawned on me I could probably use Arduino for this.

Very cool that this forum is very helpful indeed!

And oh, an Arduino Micro. Bought that one, because it works directly as a HID device in Windows, and it was indeed easy enough to program the basic commands.

I had looked at the structured array functions, but the logic seemed a little too complex to decipher with various example codes.

Thanks a LOT! I'll try this. Now that I see the code it makes a lot more sense!!

No problem, please ask about any part that is confusing or unexpected.

It's untested code, so it may not work at the first attempt.

One thing the code may not handle well is if more than one button is pressed simultaneously, so if that's important, more thought will be needed.

Ok, there is something wrong with it, compilation erro 'p' was not declared in this scope.

#include <Keyboard.h>                               
#include <Mouse.h>                                  
#include <Arduino.h>                               

const int buttonCount = 5;
const int buttonPin[buttonCount] = {2, 3, 4, 5, 6};
const int ledPin[buttonCount] = {7, 8, 10, 11, 12};
const char sendKey[buttonCount] = {'A', 'B', 'C', 'D', 'E'};

int lastButton = -1;

void setup() {

  for (int b=0; b<buttonCount; b++) {
    pinMode(buttonPin[p], INPUT_PULLUP);           
    pinMode(ledPin[p], OUTPUT);                              
  }
}

void loop() {

  for (int b=0; b<buttonCount; b++) {
    if (digitalRead(buttonPin[p]) == LOW and lastButton != p ) {                 
      Keyboard.write(sendKey[p]);                            
      if (lastButton >= 0) digitalWrite(ledPin[lastButton], LOW);      
      digitalWrite(ledPin[p], HIGH);                          
      lastButton = p;
      delay(50); // debounce
    }
  }
}

Pay attention to variable names. This is why meaningful names are so much better than single letters.

You seem to have switched from b to p in here.

Thanks!!!!

My bad!!!

Heh, no prob, we got it sorted in no time. Incredible to me that you got it almost perfect just like that with no testing even, and it works perfectly now!

I'd post pics of the whole thing but this seems to not allow pictures easily. Anyway thanks again!

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