아두이노 int 2byte
외부 인터럽트
2번 3번
인터럽트 받기
Trigger : HIGH, LOW,CHANGE, RISING, FALLING 등.
level Trigger : low or high
edge Trigger : low-> high : RISING or high->low : FALLING
스위치 예제 - 스위치 LED 켜기
#define SW 5
#define LED 3
void setup() {
// put your setup code here, to run once:
pinMode(SW,INPUT);
pinMode(LED,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(SW)) { // 스위치 누르면 LED를 켬
digitalWrite(LED,HIGH);
}
else {
digitalWrite(LED,LOW);
}
}
인터럽트 예제 - 스위치를 활용
#define LED 9
#define SW 2
volatile int state = LOW;
void setup() {
// put your setup code here, to run once:
pinMode(LED,OUTPUT);
attachInterrupt(0,blink,CHANGE);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LED,state);
}
void blink() { // 외부 인터럽트 0 루틴
state=!state;
delay(500);
}
응용 - 스위치 부저 + RGB LED + 인터럽트
#define SW 2
#define VR A0
#define LED 5
#define PZ 3
#define R 9
#define G 10
#define B 11
#define BUZZER 4
volatile int state = LOW;
void setup() {
// put your setup code here, to run once:
pinMode(SW,INPUT);
pinMode(VR,INPUT);
pinMode(LED,OUTPUT);
pinMode(PZ,OUTPUT);
pinMode(R,OUTPUT);
pinMode(G,OUTPUT);
pinMode(B,OUTPUT);
pinMode(BUZZER,OUTPUT);
Serial.begin(9600);
attachInterrupt(0,blink,FALLING); // 인터럽트 설정 attachInterrupt(번호,함수명,트리거)
digitalWrite(R,0);
digitalWrite(G,0);
digitalWrite(B,0);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(digitalRead(SW));
if(state==HIGH){
analogWrite(R,255);
digitalWrite(BUZZER,HIGH);
delay(1000);
state=LOW;
}
else{
digitalWrite(BUZZER,LOW);
digitalWrite(R,0);
digitalWrite(G,0);
digitalWrite(B,0);
}
}
void blink() { // 외부 인터럽트 0 루틴
state = HIGH;
}
응용 - 스위치 2개를 이용한 곡전환
#include "piano.h"
#define PIEZO 8
#define SW1 2
#define SW2 3
int melody[]={ sol, sol, ra, ra, sol, sol, mi, sol, sol, mi, mi, re, sol, sol, ra, ra, sol, sol, mi, sol, mi, re, mi, do};
int rythem[]={ 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 1, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 1};
int melody1[]={ do1, ra, ra, sif, sol, sol, fa, sol, ra, sif, do1, do1, do1, do1, ra, ra, ra, sif, sol, sol, fa, ra, do1, do1, ra, ra,ra};
int rythem1[]={ 4, 4, 2, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2};
volatile int state = LOW;
volatile int state1 = LOW;
void setup() {
// put your setup code here, to run once:
pinMode(PIEZO,OUTPUT);
pinMode(SW1,INPUT);
pinMode(SW2,INPUT);
Serial.begin(9600);
attachInterrupt(0,sw1,RISING);
attachInterrupt(1,sw2,RISING);
}
void loop() {
// put your main code here, to run repeatedly:
for(int i=0;i<24;i++)
{
if(!state) break;
int mel = melody[i];
int ry = 1000/rythem[i];
int pause = ry*1.3;
Serial.print(mel);
Serial.print("\t");
Serial.print(ry);
Serial.print("\t");
Serial.println(pause);
tone(PIEZO,mel,ry);
delay(pause);
if(i==23)state=LOW;
}
delay(100);
for(int i=0;i<26;i++)
{
if(!state1) break;
int mel = melody1[i];
int ry = 1000/rythem1[i];
int pause = ry*1.3;
Serial.print(mel);
Serial.print("\t");
Serial.print(ry);
Serial.print("\t");
Serial.println(pause);
tone(PIEZO,mel,ry);
delay(pause);
if(i==25)state1=LOW;
}
delay(100);
}
void sw1(){
state=!state;
state1=LOW;
}
void sw2(){
state1=!state1;
state=LOW;
}