Button Press/Release do an action

Hi everyone!

I started programming in arduino a little while ago and I now have a problem that I can not overcome:

I wanted some function that when i turn on a button the arduino press the letter "D" and waited until the button was turned off and typed "D" again. So, i want that the arduino write two times the letter "D": when i turn on the buton and when i turn off the button.

I started using this code:

#include <Keyboard.h>

void setup() {
  // make pin 2 an input and turn on the 
  // pullup resistor so it goes high unless
  // connected to ground:
  pinMode(2, INPUT_PULLUP);
  // initialize control over the keyboard:
  Keyboard.begin();
}

void loop() {
  while (digitalRead(2) == HIGH) {
    // do nothing until pin 2 goes low
  }
  //delay(10);
  // new document:
  Keyboard.press('d');
  delay(100);
  Keyboard.release('d');
  // wait for new window to open:
  delay(1000);

}

But the arduino writes a lots of "D" and i didn´t figure out how to avoid the repetition of the "D" letter

Thanks a lot from Portugal

Look at the state change detection example. It does exactly what you want.

Hi!

Thanks for your answer!

I tried to adapt for a Keyboard.Press but without success :frowning:

I did this:

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 7;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

#include <Keyboard.h>


void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
       Keyboard.press('d');
  delay(100);
  Keyboard.release('d');
    } else {
      // if the current state is LOW then the button
      // wend from on to off:
       Keyboard.press('d');
  delay(100);
  Keyboard.release('d');
    }
    // Delay a little bit to avoid bouncing
    //delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

}

Removed some comments and counter

#include <Keyboard.h>

const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 7;       // the pin that the LED is attached to
int buttonState = 1;         // current state of the button, HIGH as we have active low with INPUT_PULLUP
int lastButtonState = 1;     // previous state of the button

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  Keyboard.begin();
}

void loop() {
  buttonState = digitalRead(buttonPin);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {

    if (buttonState == LOW) {  // if the current state is pressed as we use INPUT_PULLUP
      // wend from off to on:
      Keyboard.press('d');
      digitalWrite(ledPin, HIGH);

    } else {
      // if the current state is LOW then the button
      // wend from on to off:
      Keyboard.release('d');
      digitalWrite(ledPin, LOW);
    }
    // Delay a little bit to avoid bouncing
    //delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
}

Hi!

Thanks a LOT to everyone! I changed lit bit and works fine now:

#include <Keyboard.h>

const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 7;       // the pin that the LED is attached to
int buttonState = 1;         // current state of the button, HIGH as we have active low with INPUT_PULLUP
int lastButtonState = 1;     // previous state of the button

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  Keyboard.begin();
}

void loop() {
  buttonState = digitalRead(buttonPin);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {

    if (buttonState == LOW) {  // if the current state is pressed as we use INPUT_PULLUP
      // wend from off to on:
      Keyboard.write('d');
            Keyboard.release('d');

      digitalWrite(ledPin, HIGH);

    } else {
      // if the current state is LOW then the button
      // wend from on to off:
            Keyboard.write('D');

      Keyboard.release('D');
      
      digitalWrite(ledPin, LOW);
    }
    // Delay a little bit to avoid bouncing
    //delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
}

When tha button is turn on the arduino writes one "d" and when you turn off writes one "D".

Thanks again!