BASIC4MCU | 질문게시판 | LED 밝기 제어를 어떻게 해야할까요...?
페이지 정보
작성자 초보왕초보 작성일2019-04-24 20:53 조회4,929회 댓글0건본문
스위치 하나를 가지고 한번씩 누를때 마다 LED ON/OFF가 되고, 스위치를 1초정도 꾹 누르면 밝기 조절모드로 되고, 이 상태에서 한번 클릭 시 밝기가 밝아지고 (PWM) 다시 여기서 1초 꾹 누르면 이 세팅이 저장되는 코딩을 어떻게 짜야 할까요...?
지금 PWM 사용하는 것 까진 했는데 그 다음이 감이 잘 안잡힙니다...
const int Touch = 3;
const int LED = 0;
int touchTime = 0;
int lasttouchSave = 0;
int analogValue;
int bottonState = 0;
void setup(){
pinMode(Touch, INPUT);
pinMode(LED, OUTPUT);
}
void loop()
buttonState = digitalRead(Touch);
int touchSave = digitalRead(Touch);
int bright = digitalRead(Touch);
{
if (buttonState == HIGH) {
analogWrite(LED, analogValue);
} else {
analogWrite(LED, 0);
}
}
{
if (touchSave != lasttouchSave){
if (touchSave == HIGH){
touchTime ++;
lasttouchSave = touchSave;
if (touchTime % 8 == 0){
analogValue = 255;
}
if (touchTime % 8 == 1){
analogValue = 216;
}
if (touchTime % 8 == 2){
analogValue = 180;
}
if (touchTime % 8 == 3){
analogValue = 144;
}
if (touchTime % 8 == 4){
analogValue = 108;
}
if (touchTime % 8 == 5){
analogValue = 72;
}
if (touchTime % 8 == 6){
analogValue = 36;
}
if (touchTime % 8 == 7){
analogValue = 0;
}
analogWrite(LED, analogValue);
} else {
}
delay(10);
}
lasttouchSave = touchSave;
}
댓글 0
조회수 4,929등록된 댓글이 없습니다.