1) VCC(빨간색): Votage of Commonollecter, 5V
2) GND(검은색): Ground, 접지, 0V
3) 부품 연결선
1) pullup 방식
2) pulldown 방식
//Led.h
#pragma once
#include <Arduino.h>
class Led{
protected:
int pin; //연결할 핀 번호
public:
void on();
void off();
void setValue(int value);
int toggle();
};
// Led.cpp
#include "Led.h"
Led::Led(int pin): pin(pin) {
// this ->pin = pin;
pinMode(pin, OUTPUT);
}
void Led::on() {
digitalWrite(pin, HIGH);
}
void Led::off() {
digitalWrite(pin, LOW);
}
void Led::setValue(int value) {
digitalWrite(pin, value);
}
int Led::toggle() {
int v = !digitalRead(pin);
digitalWrite(pin, v);
return v;
}
#include "Led.h"
Led leds[4] = { Led(3), Led(4), Led(5), Led(6) };
int out_no = 0; // 출력 값 출력 순서 번호(0-3)
void setup() {
}
void loop() {
for(int n = 0;n < 4;n++){
if(n == out_no) {
leds[n].on();
}
else {
leds[n].off();
}
}
out_no = (++out_no) % 4;
delay(1000);
}