이전 시간에는 아두이노와 릴레이 모듈을 이용하여 LED를 1초에 한번씩 깜빡이는 예제와 스위치를 이용하여 LED를 ON/OFF하는 예제를 실습해보았다. 이번에는 로커 스위치와 릴레이모듈을 합쳐서 스위치를 ON했을 때에만 릴레이가 작동하여 LED를 1초에 한번씩 깜빡이도록 해보자.
회로도

코드
코드 보기int Relay = 3;
int Switch = 9;
void setup()
{
pinMode(Relaypin,OUTPUT); // 릴레이를 출력으로 설정
pinMode(Switch,INPUT_PULLUP); // 스위치를 입력으로 설정
}
void loop()
{
if(digitalRead(Switch)==LOW) // 스위치를 누르면
{
while()
{
digitalWrite(Relay,HIGH); // 1채널 릴레이 ON
delay(1000);
digitalWrite(Relay,LOW); // 1채널 릴레이 OFF
delay(1000);
}
}
else // 스위치를 누르지 않으면
{
digitalWrite(Relay,LOW); // 1채널 릴레이 OFF
delay(100);
}
}
실습

great put up, very informative. I ponder why the other specialists of this sector do not realize this.
You must continue your writing. I am sure, you’ve a great readers’ base already!
Hi! I’ve been following your site for some time now and finally got the bravery to go ahead and give you a
shout out from Kingwood Texas! Just wanted to say keep up the good work!
Hello, I enjoy reading all of your article.
I wanted to write a little comment to support you.
I enjoy what you guys tend to be up too. Such
clever work and coverage! Keep up the good works guys I’ve added you guys to
our blogroll.