ふたつのボタンが、3秒以上押されると、関数を実行するプログラムです。
button_push.ino
// ---------------------------------------------------------------
/*
button_push.ino
Mar/28/2025
*/
// ---------------------------------------------------------------
#include <WiFiManager.h>
#define BTN1 GPIO_NUM_32
#define BTN2 GPIO_NUM_33
unsigned long pressStartTime = 0; // ボタン押下開始時間
bool buttonPressed = false; // ボタンが押された状態
WiFiManager wm;
String APNAME=wm.getDefaultAPName();
String CPID=APNAME.substring(6);
int icount = 0;
// ---------------------------------------------------------------
// 呼び出したい関数
void customFunction() {
Serial.println("ボタン長押し検知!関数呼び出し!");
Serial.println("APNAME = " + APNAME);
Serial.println("CPID = " + CPID);
}
// ---------------------------------------------------------------
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("*** setup *** start ***");
pinMode(BTN1,INPUT_PULLUP);
pinMode(BTN2,INPUT_PULLUP);
delay(1000);
Serial.println("*** setup *** end ***");
}
// ---------------------------------------------------------------
void loop() {
if ((digitalRead(BTN1) == LOW) && (digitalRead(BTN2) == LOW)) {
button_monitor_proc();
}
else {
buttonPressed = false;
}
delay(100);
icount++;
if ((icount % 20) == 0)
{
Serial.println("icount = " + String(icount));
}
}
// ---------------------------------------------------------------
void button_monitor_proc()
{
if (!buttonPressed) {
buttonPressed = true;
pressStartTime = millis();
}
else
{
const int delt = millis() - pressStartTime;
if (3000 <= delt) {
customFunction();
buttonPressed = false;
}
}
}
// ---------------------------------------------------------------