Help with code problems

First off I'm sorry that I'm new to Arduino and someone probably has covered this, but I'm unable to find a version.

I'm trying to write a code that when finished (and expanded for 8 buttons) will on do the following

  • Button pressed
  • Send a keystroke and turn an LED on
  • Same Button pressed again
  • Send the same keystroke and switch the LED off.

I have mashed two different code I have come across together to the best of my ability, but have had no luck making it work on my Leonardo board (it compiles fine though so I guess that is a first step).

I have included my code at the end of this post and would be thankful if someone could help me out with my errors and how to get to the outcome I'm looking for.

#include <Keyboard.h>

int LED = 12;
int stateLED = LOW;
int stateButton;
int previous = LOW;

const int buttonPin = 2;
int previousButtonState = HIGH;

long time = 0;
long debounce = 200;

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

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

void loop() {
  int stateButton = digitalRead(buttonPin);
  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 == LOW)) {

      Keyboard.write('a');

    }

    digitalWrite(LED, stateLED);
    previous == stateButton;
  }
}

You are saving the current state of the pin in stateButton. You are comparing that to the value stored in previous.

Then, you save the current state of the pin in another variable called stateButton, and comparing that state to previousButtonState.

Finally, you compare previous to buttonState, but do nothing if the comparison yields true or false.

You have extra curly braces, caused, I'm sure, because the compiler complained when you tried to save the current state in a new variable called buttonState at the same scope as the other buttonState.

You need to learn when to use

type variable = xxx

and when to use

   variable = xxx

You need to learn when to use = and when to use ==. They are NOT interchangeable.

You need to decide if the previous state is to be stored in previous or previousButtonState, and get rid of the other variable.

What I find useful is to use names that look like they go together, like currPinState and prevPinState, not buttonState and previous. You are not saving the state of a switch (or the piss-poor name button) in the variable. It is the state of the pin that you are saving.

The state change detection example shows how to do what you are trying to accomplish.

Ok then I have taken what was said in the last post into account and started rewriting the code from the beginning so that I can understand it.

I have gotten to the stage of turning the LEDs on for each switch and have see people talking about debouncing the buttons.

Is this something I need to setup for each button or is it something I add into the void setup area globally?

I've attached my sketch, which is using info I have picked up from another person's project and what I'm slowly learning on my own.

sketch_3.ino (1.61 KB)

If you only care that the button was pushed, no debouncing is necessary. Trigger on the first indication and ignore any other occurrences for a sh9rt time. If you were to count button pushes for some reason, then debouncing is perhaps necessary. The state change detection mentioned by PaulS will work fine, without debouncing, for what you are doing.