Project help for a different button box

Ok, so I know some basics with button boxes (how to make button presses register as keystrokes using a Row and Column setup) mainly by reading other peoples codes and posts.

I know that using Toggle Switches is more effort than what it's worth for my level of understanding, but is there a way so that I can make Button 1 Send keystroke "x" to the computer and turn an LED on and keep it on until the same button is pressed again and "x" is resent and the LED turned off?

What I'm trying to do is make something like this (well I would like it to really happen with toggle switches as well to make a button box this, but that could be a touch too hard).

I'm open to ideas on how I could go about doing this as so far all I have is a Leonardo board, some basic switches and a head full of ideas. So anyone able to help bring what i want to do into life (or at least is willing to help teach me/point me in the right direction) would make my life easier

Edit I forgot to say that I'm hoping to get is about 8 momentary switches (limited to this, otherwise I would end up trying to do 30 odd buttons) and I would like each to have it's own LED.

How many buttons do you want to have? Are they momentary switches? Scanning a button matrix for presses is fairly easy. Will there be an LED for each button?

Ah sorry for not including that stuff, as I said I have a lot of different ideas in my head.

I'm looking at about 8 momentary switches (just easier this way I think) and I would like each to have it's own LED.

I have limited myself to just these 8 atm otherwise I'll try making a overly complex project with over 30 odd buttons.

Okay, so you'll have 8 inputs for buttons and 8 outputs for LEDs. The Leo has 20 digital pins so you could just use 1 pin per I/O and not worry about I/O expanders or button matrices at all.

Ok then am I looking at something like the below image then for the wiring (Yes I know the board is wrong, I can't figure out to upload my drawing)?

A resistor to reduce the current to an amount needed for the LED, I'm guessing that this is needed.

And I'm guessing that the code will do something along of a "Button press" outputting a keystroke while triggering an IF statement covering " IF 'ON' turn 'OFF' " and " IF 'OFF' turn 'ON' " (I'll figure out how to code this at a later stage)

Exactly. Although it's generally better to have the high side of the led connected and switch the pin to low to turn it on. Mcu pins are better at sinking current than they are at sourcing it. Not a big issue though.

Ok then because my coding ability is next to none and I just mashed two codes together, can you look over the following code?

#include <Keyboard.h>

int pushButton = 2;
int LED = 13;
int stateLED = LOW;
int stateButton;
int previous = LOW;

const int buttonPin = 2;
int previousButtonState = HIGH;

long time = 0;
long debounce = 200;

void setup() {
  pinMode(pushButton, INPUT);
  pinMode(LED, OUTPUT);

  pinMode(buttonPin, INPUT);
  Keyboard.begin();
}

void loop() {
  int stateButton = digitalRead(pushButton);  
  if(stateButton == HIGH && previous == LOW && millis() - time > debounce) { //if button is pressed and LED is off
    if(stateLED == HIGH){
      stateLED = LOW; 
    } else {
       stateLED = HIGH; 
    }
    time = millis();
  }
  {
  int buttonSate = digitalRead(buttonPin);
  if ((buttonSate !=previousButtonState)
  && (stateButton == HIGH)) {

    Keyboard.write('a');
 
  }
  
  digitalWrite(LED, stateLED);
  previous == stateButton;
}
}

It is compiling in sketch, but because I haven't visited Jaycar (where I can get parts from) yet I haven't been able to bring it into life.

Can you see any major holes, errors or or even points that I can clean it up?

Thank you again for your help

Right I'm sure I have made a mistake in my code above, as once I upload the sketch it starts pressing 'a' before I even press the button.

Does that mean that I have to just swap my HIGH and LOW around for that part of the script or do I have an error bigger error in my code?

#include <Keyboard.h>

#define pushButton = 2;
#define LED = 13;
int stateLED = LOW;

long debounce = 200;

void setup() {
  pinMode(pushButton, INPUT_PULLUP);
  pinMode(LED, OUTPUT);


  Keyboard.begin();
}

void loop() {
  int stateButton = digitalRead(pushButton);  
  if(stateButton == HIGH) { //button is pressed
    Keyboard.write('a');
    if(stateLED == LOW){
      digitalWrite(LED, HIGH);
    } else {
       digitalWrite(LED, LOW); 
    }
    delay(debounce);
  }
}

I would start there. Change the logic high/low based on how you've got it wired.

SamIAm93:

#include <Keyboard.h>

#define pushButton = 2;
#define LED = 13;
int stateLED = LOW;

long debounce = 200;

void setup() {
 pinMode(pushButton, INPUT_PULLUP);
 pinMode(LED, OUTPUT);

Keyboard.begin();
}

void loop() {
 int stateButton = digitalRead(pushButton);  
 if(stateButton == HIGH) { //button is pressed
   Keyboard.write('a');
   if(stateLED == LOW){
     digitalWrite(LED, HIGH);
   } else {
      digitalWrite(LED, LOW);
   }
   delay(debounce);
 }
}




I would start there. Change the logic high/low based on how you've got it wired.

Wow thank you for the help, I started diving deep into the internet and started coming across very old projects people were using and ended up stumbling into codes so much more complex that your one.

So that I understand everything right, I'm going to feed power (+5v) into the wiring at some point. With this code do I run it in via the button or via the LED or does it not matter?

Lastly to expand this into more buttons I should just have to define all of my different Buttons, LEDs and Keystrokes?

Once again I'm sorry that I still don't fully understand everything and maybe asking simple questions

You want to wire the button between ground and the input pin. The code I linked has the pull up resistor enabled so it will read high while the button is not pushed, and low when the button is pushed.

When you add more buttons it's probably easiest to store all the button pin and LED pin numbers, and LED states in arrays. Then you can iterate through them fairly easily. I'll post some example code when I get home.

In an Array huh

Well I haven't even looked into that before So I would be happy to learn, as I was just going to expanded your codes for all the things like the bellow code.

#include <Keyboard.h>

//Switches in the box
const int pushButton1 = 02;
const int pushButton2 = 03;
const int pushButton3 = 04;
const int pushButton4 = 05;
const int pushButton5 = 06;
const int pushButton6 = 07;
const int pushButton7 = 8;
const int pushButton8 = 9;

//LEDs linked to the buttons
const int LED1 = 10;
const int LED2 = 11;
const int LED3 = 12;
const int LED4 = 13;
const int LED5 = A0;
const int LED6 = A1;
const int LED7 = A2;
const int LED8 = A3;
int stateLED = LOW;

long debounce = 200;

void setup() {
  pinMode(pushButton1, INPUT_PULLUP);
  pinMode(pushButton2, INPUT_PULLUP); 
  pinMode(pushButton3, INPUT_PULLUP); 
  pinMode(pushButton4, INPUT_PULLUP); 
  pinMode(pushButton5, INPUT_PULLUP); 
  pinMode(pushButton6, INPUT_PULLUP); 
  pinMode(pushButton7, INPUT_PULLUP); 
  pinMode(pushButton8, INPUT_PULLUP);
  
  //Setting up the LEDs as outputs
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(LED8, OUTPUT);


  Keyboard.begin();
}

void loop() 
{
  int stateButton = digitalRead(pushButton1);  
  if(stateButton == HIGH) { //button is pressed
    Keyboard.write('a');
    if(stateLED == LOW){
      digitalWrite(LED1, HIGH);
    } else {
       digitalWrite(LED1, LOW); 
    }
    delay(debounce);
  }

  stateButton = digitalRead(pushButton2);  
  if(stateButton == HIGH) { //button is pressed
    Keyboard.write('b');
    if(stateLED == LOW){
      digitalWrite(LED2, HIGH);
    } else {
       digitalWrite(LED2, LOW); 
    }
    delay(debounce);
  }

  stateButton = digitalRead(pushButton3);  
  if(stateButton == HIGH) { //button is pressed
    Keyboard.write('c');
    if(stateLED == LOW){
      digitalWrite(LED3, HIGH);
    } else {
       digitalWrite(LED3, LOW); 
    }
    delay(debounce);
  }
}

But if this way will be cleaner in the end I'm happy to learn

It's definitely possible to do it like that but it gets a bit redundant. This is what I came up with.

#include <Keyboard.h>

int buttonPins[] = {2,3,4,5,6,7,8,9};
char charPrint[] = {'a','b','c','d','e','f','g','h'};
int ledPins[] = {10,11,12,13,A0,A1,A2,A3};
boolean ledStates[] = {0,0,0,0,0,0,0,0};

long debounce = 200;

void setup() {

  for(int i=0;i<8;i++) {
    pinMode(buttonPins[i], INPUT_PULLUP);
  }
  for(int i=0;i<8;i++) {
    pinMode(ledPins[i], OUTPUT);
  }
  Keyboard.begin();
}

void loop() {
  
  for(int i=0;i<8;i++) {
    if(digitalRead(buttonPins[i]) == LOW) {  //button is pressed
      Keyboard.write(charPrint[i]);          //write corresponding character
      if(ledStates[i] == 0) {                
        digitalWrite(ledPins[i], HIGH);      //turn LED on
        ledStates[i] = 1;
      }
      else {
        digitalWrite(ledPins[i], LOW);      //turn LED off
        ledStates[i] = 0;
      }
      delay(debounce);
    }
  }
}

The for loops basically just iterate through the arrays that I initialized at the top. Let me know if anything's unclear.

SamIAm93:
It's definitely possible to do it like that but it gets a bit redundant. This is what I came up with.

#include <Keyboard.h>

int buttonPins[] = {2,3,4,5,6,7,8,9};
char charPrint[] = {'a','b','c','d','e','f','g','h'};
int ledPins[] = {10,11,12,13,A0,A1,A2,A3};
boolean ledStates[] = {0,0,0,0,0,0,0,0};

long debounce = 200;

void setup() {

for(int i=0;i<8;i++) {
    pinMode(buttonPins[i], INPUT_PULLUP);
  }
  for(int i=0;i<8;i++) {
    pinMode(ledPins[i], OUTPUT);
  }
  Keyboard.begin();
}

void loop() {
 
  for(int i=0;i<8;i++) {
    if(digitalRead(buttonPins[i]) == LOW) {  //button is pressed
      Keyboard.write(charPrint[i]);          //write corresponding character
      if(ledStates[i] == 0) {               
        digitalWrite(ledPins[i], HIGH);      //turn LED on
        ledStates[i] = 1;
      }
      else {
        digitalWrite(ledPins[i], LOW);      //turn LED off
        ledStates[i] = 0;
      }
      delay(debounce);
    }
  }
}




The for loops basically just iterate through the arrays that I initialized at the top. Let me know if anything's unclear.

Wow that is one neat script.
So this would be the completed script as has been defined at the tops as the relevant buttons, LEDs and keys. So I shouldn't need to modify the script further, as it should pull the info for button '3' as keystroke 'c' and LED 12.
Do I need to do anything special with the wiring up of this one or do I just wire it as it writes, but with a resistor between the +5v and the button?

The button should be wired between its pin and GND. The pull up resistor(built in) will hold the pin high until the button is pressed, pulling it low. You shouldn't need to add a resistor to the buttons, just the leds.