Creative Code

EX03_binary_led_interrupt -(1) (버튼누를 때마다 다음 이진수 led출력) 본문

Arduino(C,C++)

EX03_binary_led_interrupt -(1) (버튼누를 때마다 다음 이진수 led출력)

빛하루 2023. 8. 11. 11:56
const int led[6] = {8,9,10,11,12,13};
const int SW_0 = 2;
const int SW_1 = 3;
int cnt =0;

void SW0_Pressed(void) {
  for (int i = 0; i<6; i++) {
    digitalWrite(led[i],cnt&(0x01 << i) ? HIGH : LOW);
  }
  cnt++;
}

void setup() {
  // put your setup code here, to run once:
  for (int i = 0; i<6; i++) {
    pinMode(led[i],OUTPUT);
  }
  attachInterrupt(digitalPinToInterrupt(SW_0),SW0_Pressed,RISING); //0에서 1로 바뀔때 SW0_Pressed 함수를 수행
  //attachInterrupt 를 사용하면 loop문을 안돌아도 내부 칩에서 자체적으로 명령을 수행한다.
}

void loop() {
  // put your main code here, to run repeatedly:

}