0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ESP32: ボタンの長押し検知

Last updated at Posted at 2025-03-29

ふたつのボタンが、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;
			}
		}
}

// ---------------------------------------------------------------
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?