attachPCINT

BABY CAT·2023년 7월 17일
0

Arduino

목록 보기
16/28

#include "PinChangeInterrupt.h"



const int ledPin = 13;

const int buttonPin = 4;



int led_state = LOW;

bool led_state_changed = false;



void buttonPressed(){

 led_state = (led_state==LOW)?HIGH:LOW;

 led_state_changed = true;

}



void setup() {

 // put your setup code here, to run once:

 pinMode(ledPin, OUTPUT);

 pinMode(buttonPin, INPUT);

 attachPCINT(digitalPinToPCINT(buttonPin), buttonPressed, CHANGE);

}



void loop() {

 // put your main code here, to run repeatedly:

 if(led_state_changed){

  led_state_changed= false;

  digitalWrite(ledPin, led_state);

 }

}

1개의 댓글

comment-user-thumbnail
2023년 7월 18일

아주 유익한 내용이네요!

답글 달기