One of the basic user interactions is button press. You can specify how the arduino program behaves when you press a long or short button. The following arduino sample codes show a few options.
The following arduino example, on a single keypress, writes to the serial console that the key has been pressed. It doesn’t matter how long we pressed the button.
/**************************************/
/* https://myhomethings.eu */
/**************************************/
const byte buttonPin = 8;
bool printFlag = false;
void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if(buttonState == LOW && printFlag == false)
{
Serial.println("button is pressed");
printFlag = true;
delay(200);
}
else
{
printFlag = false;
}
}
The following example code already runs according to the three different states of the pushbutton. These states are the key pressed, the short key press, and the long key press.
ELEGOO Nano Board CH 340/ATmega 328P
advertising
/**************************************/
/* https://myhomethings.eu */
/**************************************/
const byte buttonPin = 8;
long holdingTime;
long previousHoldingTime;
unsigned long firstButtonPressTime;
byte buttonState;
byte previousButtonState = HIGH;
void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == LOW && previousButtonState == HIGH && (millis() - firstButtonPressTime) > 200)
{
firstButtonPressTime = millis();
}
holdingTime = (millis() - firstButtonPressTime);
if (holdingTime > 50)
{
if (buttonState == LOW && holdingTime > previousHoldingTime)
{
Serial.println("button is pressed");
}
if (buttonState == HIGH && previousButtonState == LOW)
{
if (holdingTime <= 400)
{
Serial.println("short button press");
}
if (holdingTime > 400)
{
Serial.println("long button press");
}
}
}
previousButtonState = buttonState;
previousHoldingTime = holdingTime;
}
For example, the pressed button can be followed by light or sound feedback. According to the following arduino diagram, the on-board LED lights up when the button is pressed. When the button is pressed briefly, the beeper beeps once, and when the button is pressed for a long time, it beeps twice. Let’s build this circuit first.
Then upload the code to Arduino Uno .
/**************************************/
/* https://myhomethings.eu */
/**************************************/
const byte buttonPin = 8;
const byte tweeter = 9;
const byte ledPin = 13;
long holdingTime;
long previousHoldingTime;
unsigned long firstButtonPressTime;
byte buttonState;
byte previousButtonState = HIGH;
const int note = 4699;
void beepsOnce()
{
tone(tweeter, note, 125);
delay(180);
noTone(tweeter);
}
void beepsTwice()
{
for (int i = 0; i < 2; i++)
{
tone(tweeter, note, 125);
delay(60);
noTone(tweeter);
delay(60);
}
}
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == LOW && previousButtonState == HIGH && (millis() - firstButtonPressTime) > 100)
{
firstButtonPressTime = millis();
}
holdingTime = (millis() - firstButtonPressTime);
if (holdingTime > 50)
{
if (buttonState == LOW && holdingTime > previousHoldingTime)
{
Serial.println("button is pressed");
digitalWrite(ledPin, HIGH);
}
if (buttonState == HIGH && previousButtonState == LOW)
{
if (holdingTime <= 400)
{
Serial.println("short button press");
beepsOnce();
digitalWrite(ledPin, LOW);
}
if (holdingTime > 400)
{
Serial.println("long button press");
beepsTwice();
digitalWrite(ledPin, LOW);
}
}
}
previousButtonState = buttonState;
previousHoldingTime = holdingTime;
}
Another option is to abort the arduino program by pressing the button. Use attachInterrupt () to exit the infinite while loop. Iterrupt pins on Arduino Uno, Nano, boards is: 2, 3.
const byte interruptPin = 3;
void buttonInterrupt()
{
Serial.println("Button is pressed!");
}
void setup()
{
Serial.begin(9600);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), buttonInterrupt, CHANGE);
}
void loop()
{
while(true)
{
delay(100);
}
}
See other examples as well. ESP32, ESP8266, Arduino sketches, examples
HiLetgo Nano V3.0 ATmega328P CH340G
advertising – amazon.com