Skip to content

Commit

Permalink
Create LED_Button
Browse files Browse the repository at this point in the history
  • Loading branch information
rkgus24 authored Apr 18, 2023
1 parent 8a14a8f commit bfe0292
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions LED_Button
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
unsigned long t1 = 0; // t1에 대한 변수
unsigned long t2 = 0; // t2에 대한 변수
unsigned long t3 = 0; // t3에 대한 변수
unsigned long t4= 0; // t4에 대한 변수

int btn_cnt = 0; // 버튼을 1회 누르면 1씩 올라가는 카운터

bool old_btn = HIGH;

int long_btn_cnt = 0;

void setup() {
Serial.begin(115200); // 통신할거임
pinMode(16, OUTPUT); // 출력으로 설정

}

void loop() { // 메인 루프 안에 delay() 함수를 2회 이상 쓸 수 없음. -> 아두이노 중급 이상

if(digitalRead(16) == HIGH) {
// LED가 켜져있는 상태
if(millis() - t4 > 3000) {
digitalWrite(16, LOW);
}
}
// 버튼이 눌려진 횟수를 업카운트 하고 싶음
bool now_btn = digitalRead(17);

// 엣지 검출(하강)
if(old_btn == HIGH && now_btn == LOW) {
t3 = millis();
}else if(old_btn == LOW && now_btn == HIGH) {
// 상승 엣지
// Serial.print("버튼을 눌렀다가 뗀 시간 : ");
// Serial.println(millis() - t3);

if(millis() - t3 > 1000) {
// 버튼을 길~게 누른 부분
btn_cnt++;
}else if(millis() - t3 > 50) {
long_btn_cnt++;
}
}

old_btn = now_btn; // n번째 값을 n-1에게 전달

if(digitalRead(17) == LOW) {
delay(200);
// 버튼이 눌러짐
btn_cnt++;
}
// LED를 1초 간격으로 토글한다(task1)
if(millis() - t1 > 1000) { // 1000 : 간격
t1 = millis();
// 토글한다
// 밑의 코드와 주석이 되어있는 코드는 같음
// LED를 1초 간격으로 토글(반짝거리게)시킨다
bool state = digitalRead(16);
digitalWrite(16, !state);
}

// 5초 간격으로 시리얼 모니터에 출력함(task2)
if(millis() - t2 > 1000) { // 1000 : 간격
t2 = millis();
Serial.println("현재 btn 카운터 =");
Serial.println(btn_cnt);
Serial.println(", 현재 long_btn 카운터=");
Serial.println(long_btn_cnt);
}

/*
digitalWrite(16, HIGH);
delay(1000);
digitalWrite(16, LOW);
delay(1000);
*/

}

0 comments on commit bfe0292

Please sign in to comment.