LoginSignup
0
0

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

Posted at

ハードの構成

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

ArduinoR4WiFi_may12_2024.jpg

プログラム

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

					May/12/2024

*/
// ---------------------------------------------------------------------
#define SWITCH_A 7
#define SWITCH_B 8
#define LED_A 10
#define LED_B 11

int count = 0;

void setup() {
	pinMode(SWITCH_A, INPUT);
	pinMode(SWITCH_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(SWITCH_A);
	int status_b = digitalRead(SWITCH_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) + "  ");
	Serial.print("7 = ");
	Serial.print(status_a);
	Serial.print("  8 = ");
	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