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?

M5Core2: タッチパネルの使い方

Last updated at Posted at 2025-02-24

プログラム

touch_panel01.ino
// ---------------------------------------------------------------
//	touch_panel01.ino
//
//						Feb/24/2025
// ---------------------------------------------------------------
#include <M5Core2.h>

#define	RADIUS	50
#define	HANTEI	60
bool button_a = false;
bool button_b = false;

int pos_before[] = {0,0};
int count_omit = 0;
int nn = 0;
// ---------------------------------------------------------------
void setup() {
	M5.begin();
  
	M5.Lcd.fillScreen(TFT_BLACK);
	M5.Lcd.fillCircle(60, 120, RADIUS, GREEN);
	M5.Lcd.fillCircle(260, 120, RADIUS, GREEN);
}

// ---------------------------------------------------------------
void touch_proc(int xx_in, int yy_in)
{
	int ip = sq(xx_in - 60) + sq(yy_in - 120);
	double lla = sqrt(ip);
	int iq = sq(xx_in - 260) + sq(yy_in - 120);
	double llb = sqrt(iq);
	Serial.printf("nn = %d\t",nn);
	Serial.printf("lla = %f\t",lla);
	Serial.printf("llb = %f\n",llb);

	if (lla <= HANTEI)
		{
		if(button_a)
			{
			M5.Lcd.fillCircle(60, 120, RADIUS, GREEN);
			}
		else
			{
			M5.Lcd.fillCircle(60, 120, RADIUS, RED);
			}
		button_a = !button_a;
		}
	else if (llb <= HANTEI)
		{
		if(button_b)
			{
			M5.Lcd.fillCircle(260, 120, RADIUS, GREEN);
			}
		else
			{
			M5.Lcd.fillCircle(260, 120, RADIUS, RED);
			}
		button_b = !button_b;
		}

	nn++;
}

// ---------------------------------------------------------------
void loop() {
  M5.update();  // タッチ状態を更新
  
  if (M5.Touch.ispressed()) {
    Point touchPoint = M5.Touch.getPressPoint();
  
	if ((pos_before[0]!= touchPoint.x) && (pos_before[1] != touchPoint.y) 
		&& (70 <=touchPoint.y) && (touchPoint.y <= 170))
		{

		touch_proc(touchPoint.x, touchPoint.y);

		pos_before[0] = touchPoint.x;
		pos_before[1] = touchPoint.y;

		count_omit = 10;
		}
	}
  
	delay(100);  // ループの遅延

	if (0 < count_omit)
		{
		count_omit--;
		}
	else
		{
		pos_before[0] = 0;
		pos_before[1] = 0;
		}
}

// ---------------------------------------------------------------

Arduino IDE 2.3.4

image.png

実行時の様子

image.png

0
0
0

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?