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?

ArduinoUnoR4WiFi: ボタンを押したら LED を点灯させる

Last updated at Posted at 2024-05-11

ハードの構成

ボタン A --- PD6
ボタン B --- PD8
LED A --- PD10
LED B --- PD11

結線図

button_diagram.jpg

button_may29.jpg

プログラム

button.ino
// ---------------------------------------------------------------------
/*
	button.ino

					May/29/2024

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

int count = 0;

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 ((count % 20) == 0)
		{
		display_proc(count,status_a,status_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);


  	count++;
}

// ---------------------------------------------------------------------
void display_proc(int count,int status_a,int status_b)
{
	Serial.print("count = " + String(count) + "\t");
	Serial.print(String(BUTTON_A) + " = ");
	Serial.print(status_a);
	Serial.print("\t" + String(BUTTON_B) + " = ");
	Serial.println(status_b);
}

// ---------------------------------------------------------------------
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?