Skip to content

Research output

Audio feedback code

This test uses the PinButton library to detect different types of button presses (such as single clicks, double clicks, and long clicks) on a digital input pin. When a long click or a double click is detected, the code turns on a buzzer connected to a digital output pin for a set duration, producing a different sound for each press.

Fritzing wiring diagram

Fritzing image


Code

#include <PinButton.h>
#define BUZZZER_PIN  5

// Create a new button object, listening on pin 4.
PinButton button(4);


void setup() {

}

void loop() {
  button.update();

    if (button.isLongClick()) {
    tone(BUZZZER_PIN, 622, 200);
  }else if (button.isDoubleClick()) {
    tone(BUZZZER_PIN, 222, 200);
  }
}

The code includes the PinButton.h library, which provides functions for detecting different types of button presses (such as single clicks, double clicks, and long clicks) on a digital input pin. A constant BUZZZER_PIN is defined with a value of 5, which is the digital pin that the buzzer is connected to. Then a new PinButton object is created called button, which listens for button presses on digital pin 5.

In the loop() function, the update() method of the button object is called to check for button presses. If a long click is detected, the buzzer is turned on for 200 milliseconds with a frequency of 622 Hz. If a double click is detected, the buzzer is turned on for 200 milliseconds with a frequency of 222 Hz.

This code allows you to detect different types of button presses on digital pin 4 and to use the buzzer connected to digital pin 5 to produce a sound in response.

Vibration feedback

Code

#include <PinButton.h>

const int motorPin = 3;  // Motor is connected to digital pin 3

// Create a new button object, listening on pin 5.
// You can have as many buttons as you like.
PinButton button(4);


void setup() {
    pinMode(motorPin, OUTPUT);  // Set motorPin as an output
}

void loop() {
  button.update();

    if (button.isLongClick()) {
        digitalWrite(motorPin, HIGH);
        delay(200);  // Wait for 200ms
        digitalWrite(motorPin, LOW);
  }

  if (button.isDoubleClick()) {
        digitalWrite(motorPin, HIGH);
        delay(200);  // Wait for 200ms
        digitalWrite(motorPin, LOW);
        delay(200);
        digitalWrite(motorPin, HIGH);
        delay(200);  // Wait for 200ms
        digitalWrite(motorPin, LOW);
  }
}

The code uses the PinButton library to detect different types of button presses (such as single clicks, double clicks, and long clicks) on a digital input pin. The code includes a setup() function and a loop() function.

In the setup() function, the motor pin is configured as an output using the pinMode() function.

In the loop() function, the update() method of the button object is called to check for button presses. If a long click is detected, the motor is turned on for 200 milliseconds and then turned off. If a double click is detected, the motor is turned on for 200 milliseconds, turned off for 200 milliseconds, and then turned on again for 200 milliseconds.

This code allows you to control the motor by pressing the button connected to digital input pin 4. The code also includes a delay between each on/off cycle of the motor to ensure that the motor has time to respond to the button presses.


Last update: April 20, 2023