Constant stream of keypresses - not sure what to do.

I've been at this for about a month, still very new at learning. I think I am beginning to understand a little more and have fixed most of the issues I've had so far. (usually when I fix 1, 2 more pop up.) Anyway, its all good fun!

A bit of background at my shit code, my current end goal is that when I press the button the led turns on and a single key is pressed. When I press it again the led turns off and that same key is pressed again.

Currently, I just get a constant stream of the key regardless of if I've pressed or not.

I am using an Arduino Leonardo. Let me know where I went wrong!

//Pushing a Button Start/Stop LED

//constants
const int buttonPin = 4;                                         // Indicates the Pin location of the button.
const int ledPin = 7;                                            // Indicates the Pin location of the LED.

//variables
int ledState = HIGH;                                             // Indicates the initial (current?) state of the LED.
int buttonState;                                                 // Indicates when the button is pressed (?)
int lastButtonState = LOW;                                       // Indicates the previous state of the button (?)

//long variables
long lastDebounceTime = 0;                                       // Indicates the last time the the button was pressed (?)
long debounceDelay = 25;                                         // Indicates the delay of time such that the button press is not accidentally registered.

void setup() {
  pinMode(buttonPin, INPUT);                                     // Indicates the button being pushed it will cause
  pinMode(ledPin, OUTPUT);                                       // this action to occur.
  digitalWrite(ledPin, ledState);                                // (?)
  digitalWrite(buttonPin, HIGH);                                 // (?)
  Keyboard.begin();                                              // Indicates that a button press will register as key input.
}

void loop() {
  int reading = digitalRead(buttonPin);                          // Indicates when the button is pressed
  if (reading != lastButtonState) {                              // and if that button press is different than the previous buttons state
    lastDebounceTime = millis();                                 // and it is outside of the delay of time to avoid accidental button registration
  } 
  if ((millis() - lastDebounceTime) > debounceDelay) {           // (?)
    if (reading != buttonState) {                                // (?)
      buttonState = reading;                                     // (?)
      if (buttonState == HIGH) {                                 // Indicates that if the button was indeed pressed that
        ledState = !ledState;                                    // the LED state should change.
      }
    }
  }
  digitalWrite(ledPin, ledState);                                // Indicates the LED should turn on or off
  lastButtonState = reading;  

reading = digitalRead(buttonPin);                                // Indicates when the button is pressed
  if (reading != lastButtonState) {                              // and if that button press is different than the previous buttons state
    lastDebounceTime = millis();                                 // and it is outside of the delay of time to avoid accidental button registration
  } 
  if ((millis() - lastDebounceTime) > debounceDelay) {           // (?)
    if (reading != buttonState) {                                // (?)
      buttonState = reading;                                     // (?)
      if (buttonState == HIGH) {                                 // Indicates that if the button was indeed pressed that
        buttonState = !buttonState;                              // (?)
      }
    }
  }
  Keyboard.write('N');                                           // Indicates the character pressed.
      delay(100);                                                // Indicates the amount time before the character can be written again.
  lastButtonState = reading;  
    }

You just gotta have two separate sets of states for the button:

  1. buttonPressed - checks whether the button is currently being pressed
  2. buttonTriggered - checks if the code that should run when button is pressed has already been executed (so that we only xecute it once instead of repeatedly while button is pressed)

So in your loop wih the button, you'll be doing something like:

...
/* When button is pressed - buttonPressed is equal to 1 */

if (buttonPressed == 1 && buttonTriggered ==0) {
  blinkLED(); // <-- run your code
  buttonTriggered = 1; // set the triggered state to 1, so this IF clause will no longer be true and the code will not be excuted again
}

/* Then when the button is released - just set both buttonPressed and buttonTriggered to 0 again */
...

few things like no keyboard library it doesn't compile.

being as nice as I can be all I see is a bunch of code that has a lot of faults but as im unsure if you are using a button on pin 4 or some type of keyboard so im not sure what the faults are.

if this is a push button then the code roughly reads

if I press a button start timer. if the button has been pushed for 25 thousand of a second. change led state.

constantly write this? Keyboard.write('N'); (don't know what that does)

I have added some notes in the code

//Pushing a Button Start/Stop LED

//constants
const int buttonPin = 4;                                         // Indicates the Pin location of the button.
const int ledPin = 7;                                            // Indicates the Pin location of the LED.

//variables
int ledState = HIGH;                                             // Indicates the initial (current?) state of the LED.
int buttonState;                                                 // Indicates when the button is pressed (?)
int lastButtonState = LOW;                                       // Indicates the previous state of the button (?)

//long variables
long lastDebounceTime = 0;                                       // Indicates the last time the the button was pressed (?)
long debounceDelay = 25;// Indicates the delay of time such that the button press is not accidentally registered.

void setup() {
  pinMode(buttonPin, INPUT);                                     // Indicates the button being pushed it will cause
  pinMode(ledPin, OUTPUT);                                       // this action to occur.
  digitalWrite(ledPin, ledState);                                // (?)
  digitalWrite(buttonPin, HIGH);                                 // (?)
  Keyboard.begin();                                              // Indicates that a button press will register as key input.
}

void loop() {
  int reading = digitalRead(buttonPin);// Indicates when the button is pressed
  if (reading != lastButtonState) {// and if that button press is different than the previous buttons state
    lastDebounceTime = millis();  // start timer
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {//timer done
    if (reading != buttonState) {//code block so it only happens once
      //this is needed i guess because the millis if is in the loop so its always timing
      buttonState = reading;//add block as the if was true
      if (buttonState == HIGH) {// Indicates that if the button was indeed pressed that
        ledState = !ledState; // the LED state should change.
      }
    }
  }
  digitalWrite(ledPin, ledState);// Indicates the LED should turn on or off
  lastButtonState = reading;// adds the block that stops the timer being reset
  // think of this reading != lastButtonState
  //reading = 1 lastbutton state =0 so it resets timer
  //mills isnt done so we bypass that
  //lastButtonState = reading just hope no on managed to release button getting
  //to this point
  //so reading =1  lastbutton state=1
  //if some one lets go off button
  // reading =0 lastbutton state=1
  // reading != lastButtonState so reset timer and repeat the code
  //only the buttonState == HIGH blocks the led being changed

  







  //next section makes no sense
  reading = digitalRead(buttonPin);
  if (reading != lastButtonState) {
    lastDebounceTime = millis();                                 // and it is outside of the delay of time to avoid accidental button registration
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {           // (?)
    if (reading != buttonState) {                                // (?)
      buttonState = reading;                                     // (?)
      if (buttonState == HIGH) {                                 // Indicates that if the button was indeed pressed that
        buttonState = !buttonState;//so if button state was high change it to low
        //wouldnt that affect the first part of the code?
      }
    }
  }
  //back to loop
  Keyboard.write('N');// this will execute every loop
  //i think this is what you are seeing as the problem.
  delay(100);// stops the program for a tenth of a second
  lastButtonState = reading;//?
}

The code in several things at a time includes an LED operated by a button.

...R

If you are using mechanical buttons, use ones that are of a "click" type to reduce the length of time where "bounce" electrical noise is generated.

The replies here are fantastic! Thank you so much - this clears up a lot of my troubles!