LoginSignup
0
0

ArduinoUnoR4WiFi: ボタンを押した回数をカウント

Last updated at Posted at 2024-05-11

こちらのプログラムを改造してボタンを押した回数をカウントするようにしました。
ArduinoUnoR4WiFi: ボタンを押したら LED を点灯させる

プログラム

button_count.ino
// ---------------------------------------------------------------------
/*
	button_count.ino

					May/29/2024

*/
// ---------------------------------------------------------------------
#define BUTTON_A 6
#define BUTTON_B 8
#define LED_A 10
#define LED_B 11

int count = 0;
int count_a = 0;
int count_b = 0;
int status_a_before = LOW;
int status_b_before = LOW;

void setup() {
	pinMode(BUTTON_A, INPUT);
	pinMode(BUTTON_B, INPUT);
	pinMode(LED_A, OUTPUT);
	pinMode(LED_B, OUTPUT);
	Serial.begin(19200);
	delay(1000);
	Serial.println("*** start ***");
	delay(500);

	led_proc();
}

// ---------------------------------------------------------------------
void led_proc() {
	for (int it =0; it<5; it++)
		{
		digitalWrite(LED_A, HIGH);
		digitalWrite(LED_B, HIGH);
		delay(500);

		digitalWrite(LED_A, LOW);
		digitalWrite(LED_B, LOW);
		delay(500);
		}
}

// ---------------------------------------------------------------------
void loop() {
	int status_a = digitalRead(BUTTON_A);
	int status_b = digitalRead(BUTTON_B);

	if ((status_a == HIGH) && (status_a_before == LOW))
		{
		count_a++;
		display_proc(count,count_a,count_b);
		}

	if ((status_b == HIGH) && (status_b_before == LOW))
		{
		count_b++;
		display_proc(count,count_a,count_b);
		}
 
	if ((count % 100) == 0)
		{
		display_proc(count,count_a,count_b);
		}

	if (status_a == 1)
		{
		digitalWrite(LED_A, HIGH);
		}
	else
		{
		digitalWrite(LED_A, LOW);
		}

	if (status_b == 1)
		{
		digitalWrite(LED_B, HIGH);
		}
	else
		{
		digitalWrite(LED_B, LOW);
		}


	delay(50);
	status_a_before = status_a;
	status_b_before = status_b;

  	count++;
}

// ---------------------------------------------------------------------
void display_proc(int count,int count_a,int count_b)
{
	Serial.print("count = " + String(count) + "  ");
	Serial.print("A = ");
	Serial.print(count_a);
	Serial.print("  B = ");
	Serial.println(count_b);
}

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

実行時の様子

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