Day1 04_LED_mission
2024. 1. 17. 18:07ㆍarduino
- 2번(빨강), 3번(노랑), 4번(연두)를 2초마다 차례대로 불을 키는 코드
const int ledRPin = 2;
const int ledYPin = 3;
const int ledGPin = 4;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(ledRPin, OUTPUT);
pinMode(ledYPin, OUTPUT);
pinMode(ledGPin, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(ledRPin, HIGH);
digitalWrite(ledYPin, LOW);
digitalWrite(ledGPin, LOW);
delay(2000);
digitalWrite(ledRPin, LOW);
digitalWrite(ledYPin, HIGH);
digitalWrite(ledGPin, LOW);
delay(2000);
digitalWrite(ledRPin, LOW);
digitalWrite(ledYPin, LOW);
digitalWrite(ledGPin, HIGH);
delay(2000);
}
- 같은 코드(배열, for문, 함수)
#define LED_DELAY 2000
const int ledPin[3] = { 2, 3, 4 };
void setup() {
// put your setup code here, to run once:
for (int i = 0; i < 3; i++) {
pinMode(ledPin[i], OUTPUT);
}
}
void controlLED(int r, int y, int g) {
digitalWrite(ledPin[0], r);
digitalWrite(ledPin[1], y);
digitalWrite(ledPin[2], g);
}
void loop() {
// put your main code here, to run repeatedly:
controlLED(HIGH, LOW, LOW);
delay(LED_DELAY);
controlLED(LOW, HIGH, LOW);
delay(LED_DELAY);
controlLED(LOW, LOW, HIGH);
delay(LED_DELAY);
}
'arduino' 카테고리의 다른 글
Day1 06_LED8 (0) | 2024.01.17 |
---|---|
Day1 05_millis(라이브러리 호출) (0) | 2024.01.17 |
Day 1 03 - for LED (0) | 2024.01.15 |
Day 1 02 - serial (0) | 2024.01.15 |
Day 1 01 - LED (0) | 2024.01.15 |