Konfig: Short Press: Pin 8 , Long Press Pin 10, Pin Button: 4 (ezButton pin: 7).
Cara kerja: Awal nyala Short press (pin 8), ketika PB ditekan long press pin 8 mati lalu pin 10 nyala, ketika PB Short press pin 10 mati lalu pin 8 nyala, dst.
Kode:
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-long-press-short-press
*/
// constants won't change. They're used here to set pin numbers:
const int BUTTON_PIN = 4; // the number of the pushbutton pin
const int SHORT_PRESS_TIME = 1000; // 1000 milliseconds
const int LONG_PRESS_TIME = 1000; // 1000 milliseconds
const int SHORT_PRESS_PIN = 10; // pin to turn on for short press
const int LONG_PRESS_PIN = 8; // pin to turn on for long press
// Variables will change:
int lastState = LOW; // the previous state from the input pin
int currentState; // the current reading from the input pin
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
bool isPressing = false;
bool isLongDetected = false;
bool shortPressActive = false;
bool longPressActive = false;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(SHORT_PRESS_PIN, OUTPUT);
pinMode(LONG_PRESS_PIN, OUTPUT);
}
void loop() {
// read the state of the switch/button:
currentState = digitalRead(BUTTON_PIN);
if(lastState == HIGH && currentState == LOW) { // button is pressed
pressedTime = millis();
isPressing = true;
isLongDetected = false;
if (shortPressActive || longPressActive) {
digitalWrite(SHORT_PRESS_PIN, LOW); // turn off pin 10
digitalWrite(LONG_PRESS_PIN, LOW); // turn off pin 8
shortPressActive = false;
longPressActive = false;
}
} else if(lastState == LOW && currentState == HIGH) { // button is released
isPressing = false;
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
if( pressDuration < SHORT_PRESS_TIME ) {
if (!shortPressActive) {
Serial.println("A short press is detected");
digitalWrite(SHORT_PRESS_PIN, HIGH); // turn on pin 10
shortPressActive = true;
} else {
digitalWrite(SHORT_PRESS_PIN, LOW); // turn off pin 10
shortPressActive = false;
}
}
}
if(isPressing == true && isLongDetected == false) {
long pressDuration = millis() - pressedTime;
if( pressDuration > LONG_PRESS_TIME ) {
if (!longPressActive) {
Serial.println("A long press is detected");
digitalWrite(LONG_PRESS_PIN, HIGH); // turn on pin 8
longPressActive = true;
} else {
digitalWrite(LONG_PRESS_PIN, LOW); // turn off pin 8
longPressActive = false;
}
isLongDetected = true;
}
}
// save the the last state
lastState = currentState;
}
Gambar:
/*
Created by ArduinoGetStarted.com
This example code is in the public domain
Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-long-press-short-press
*/
#include <ezButton.h>
const int SHORT_PRESS_TIME = 1000; // 1000 milliseconds
const int LONG_PRESS_TIME = 1000; // 1000 milliseconds
ezButton button(7); // create ezButton object that attach to pin 7;
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
bool isPressing = false;
bool isLongDetected = false;
const int SHORT_PRESS_PIN = 8; // pin to turn on for short press
const int LONG_PRESS_PIN = 10; // pin to turn on for long press
void setup() {
Serial.begin(9600);
button.setDebounceTime(50); // set debounce time to 50 milliseconds
pinMode(SHORT_PRESS_PIN, OUTPUT);
pinMode(LONG_PRESS_PIN, OUTPUT);
}
void loop() {
button.loop(); // MUST call the loop() function first
if (button.isPressed()) {
pressedTime = millis();
isPressing = true;
isLongDetected = false;
}
if (button.isReleased()) {
isPressing = false;
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
if ( pressDuration < SHORT_PRESS_TIME ) {
Serial.println("A short press is detected");
digitalWrite(SHORT_PRESS_PIN, HIGH); // turn on pin 8 for short press
} else {
digitalWrite(SHORT_PRESS_PIN, LOW); // turn off pin 8
}
}
if (isPressing == true && isLongDetected == false) {
long pressDuration = millis() - pressedTime;
if ( pressDuration > LONG_PRESS_TIME ) {
Serial.println("A long press is detected");
digitalWrite(LONG_PRESS_PIN, HIGH); // turn on pin 10 for long press
isLongDetected = true;
} else {
digitalWrite(LONG_PRESS_PIN, LOW); // turn off pin 10
}
}
}
2. Menyalakan satu lampu custom pin dengan 1 atau 2Pushbutton
Konfig: Led Pin: adjsut (8,9,10,11), Pb: 6 (dan 7)
Cara kerja: jika klik short PB 6 maka akan on-off, jika klik long pb 6 akan ganti pin (8,9,10,11). Jika dua tombol Pb6 sebagai long dan pb 7 sebagai short.
const int BUTTON_PIN = 6; // pushbutton pin for selecting output pin (long press) and turning output on/off (short press)
int selectedPinIndex = 0; // index of selected output pin
bool outputState = false; // state of output
const int OUTPUT_PIN[] = {8, 9, 10, 11}; // array of output pin numbers
const int NUM_OUTPUT_PINS = sizeof(OUTPUT_PIN) / sizeof(OUTPUT_PIN[0]); // number of output pins
unsigned long lastButtonPressTime = 0; // last time button was pressed
bool buttonPressed = false; // flag to indicate if button is pressed
const unsigned long LONG_PRESS_DURATION = 1000; // duration for a long press (milliseconds)
const unsigned long DEBOUNCE_DELAY = 50; // debounce delay (milliseconds)
void setup() {
Serial.begin(9600); // initialize Serial Monitor
pinMode(BUTTON_PIN, INPUT_PULLUP); // set button pin as input with pull-up resistor
for (int i = 0; i < NUM_OUTPUT_PINS; ++i) {
pinMode(OUTPUT_PIN[i], OUTPUT); // set output pins as output
}
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN); // read button state
// Check for button press
if (buttonState == LOW && !buttonPressed) {
lastButtonPressTime = millis();
buttonPressed = true;
}
// Check for button release
if (buttonState == HIGH && buttonPressed) {
// Check if it's a short press or long press
if (millis() - lastButtonPressTime > LONG_PRESS_DURATION) {
selectedPinIndex = (selectedPinIndex + 1) % NUM_OUTPUT_PINS; // cycle through output pins
Serial.print("Selected output pin: ");
Serial.println(OUTPUT_PIN[selectedPinIndex]);
} else {
outputState = !outputState; // toggle output state
digitalWrite(OUTPUT_PIN[selectedPinIndex], outputState ? HIGH : LOW); // turn output on or off
Serial.print("Output pin ");
Serial.print(OUTPUT_PIN[selectedPinIndex]);
Serial.print(" state: ");
Serial.println(outputState ? "ON" : "OFF");
}
buttonPressed = false; // reset button pressed flag
delay(DEBOUNCE_DELAY); // debounce delay
}
}
Kode dua tombol:
const int SELECT_BUTTON_PIN = 6; // pushbutton pin for selecting output pin (long press)
const int ON_OFF_BUTTON_PIN = 7; // pushbutton pin for turning output on/off (short press)
int selectedPinIndex = 0; // index of selected output pin
bool outputState = false; // state of output
const int OUTPUT_PIN[] = {9, 10, 11}; // array of output pin numbers
const int NUM_OUTPUT_PINS = sizeof(OUTPUT_PIN) / sizeof(OUTPUT_PIN[0]); // number of output pins
unsigned long selectButtonPressTime = 0; // last time select button was pressed
unsigned long onOffButtonPressTime = 0; // last time on/off button was pressed
const unsigned long LONG_PRESS_DURATION = 1000; // duration for a long press (milliseconds)
const unsigned long DEBOUNCE_DELAY = 50; // debounce delay (milliseconds)
void setup() {
Serial.begin(9600); // initialize Serial Monitor
pinMode(SELECT_BUTTON_PIN, INPUT_PULLUP); // set select button pin as input with pull-up resistor
pinMode(ON_OFF_BUTTON_PIN, INPUT_PULLUP); // set on/off button pin as input with pull-up resistor
for (int i = 0; i < NUM_OUTPUT_PINS; ++i) {
pinMode(OUTPUT_PIN[i], OUTPUT); // set output pins as output
}
}
void loop() {
// Check for select button press
if (digitalRead(SELECT_BUTTON_PIN) == LOW) {
selectButtonPressTime = millis();
while (digitalRead(SELECT_BUTTON_PIN) == LOW) {} // wait for button release
delay(DEBOUNCE_DELAY); // debounce delay
}
// Check for on/off button press
if (digitalRead(ON_OFF_BUTTON_PIN) == LOW) {
if (millis() - onOffButtonPressTime > LONG_PRESS_DURATION) {
// long press detected, toggle output pin
selectedPinIndex = (selectedPinIndex + 1) % NUM_OUTPUT_PINS; // cycle through output pins
Serial.print("Selected output pin: ");
Serial.println(OUTPUT_PIN[selectedPinIndex]);
} else {
// short press detected, toggle output state
outputState = !outputState; // toggle output state
digitalWrite(OUTPUT_PIN[selectedPinIndex], outputState ? HIGH : LOW); // turn output on or off
Serial.print("Output pin ");
Serial.print(OUTPUT_PIN[selectedPinIndex]);
Serial.print(" state: ");
Serial.println(outputState ? "ON" : "OFF");
}
onOffButtonPressTime = millis();
delay(DEBOUNCE_DELAY); // debounce delay
}
}
Konfig: Pin 9 (led), Pin 6 (PB)
Cara kerja: Awal Off, tahan tombol 1x kedip 1 detik, 2x kedip 2 detik, 3x on terus, 4x Off, (tambahan ganti pon output di kode 2)
const int BUTTON_PIN = 6; // pushbutton pin for selecting blink mode
const int OUTPUT_PIN = 9; // output pin for LED
int selectedMode = 0; // selected blink mode (0: off, 1: 1 second, 2: 2 seconds, 3: 3 seconds)
unsigned long lastButtonPressTime = 0; // last time button was pressed
bool buttonPressed = false; // flag to indicate if button is pressed
const unsigned long LONG_PRESS_DURATION = 1000; // duration for a long press (milliseconds)
const unsigned long DEBOUNCE_DELAY = 50; // debounce delay (milliseconds)
void setup() {
Serial.begin(9600); // initialize Serial Monitor
pinMode(BUTTON_PIN, INPUT_PULLUP); // set button pin as input with pull-up resistor
pinMode(OUTPUT_PIN, OUTPUT); // set output pin as output
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN); // read button state
// Check for button press
if (buttonState == LOW && !buttonPressed) {
lastButtonPressTime = millis();
buttonPressed = true;
}
// Check for button release
if (buttonState == HIGH && buttonPressed) {
// Check if it's a short press or long press
if (millis() - lastButtonPressTime > LONG_PRESS_DURATION) {
// Long press: toggle blink mode
selectedMode = (selectedMode + 1) % 4; // cycle through blink modes (0, 1, 2, 3)
switch (selectedMode) {
case 0:
Serial.println("Blink mode: OFF");
break;
case 1:
Serial.println("Blink mode: 1 second");
break;
case 2:
Serial.println("Blink mode: 2 seconds");
break;
case 3:
Serial.println("On Terus mode");
break;
}
} else {
// Short press: do nothing
}
buttonPressed = false; // reset button pressed flag
delay(DEBOUNCE_DELAY); // debounce delay
}
// Handle blink mode
switch (selectedMode) {
case 1:
// Blink mode: 1 second
digitalWrite(OUTPUT_PIN, HIGH);
delay(1000);
digitalWrite(OUTPUT_PIN, LOW);
delay(1000);
break;
case 2:
// Blink mode: 2 seconds
digitalWrite(OUTPUT_PIN, HIGH);
delay(2000);
digitalWrite(OUTPUT_PIN, LOW);
delay(2000);
break;
case 3:
// Blink mode: 3 seconds
digitalWrite(OUTPUT_PIN, HIGH);
break;
default:
// Blink mode: OFF
digitalWrite(OUTPUT_PIN, LOW);
break;
}
}
0 Response to "Program arduino Push button "
Posting Komentar