BASIC4MCU | 질문게시판 | 아두이노 터치센서 질문
페이지 정보
작성자 master 작성일2018-12-17 23:09 조회3,904회 댓글0건
https://www.basic4mcu.com/bbs/board.php?bo_table=gac&wr_id=6990
본문
터치센서 아두이노 코딩 제발 도와주세요ㅠㅠㅠㅠㅠ
const int switchPin = 7;
const int motorPin = 9;
int switchState = 0;
void setup() {
pinMode(motorPin, OUTPUT);
pinMode(7, INPUT);
}
void loop() {
switchState = digitalRead(switchPin);
if(digitalRead(7) == HIGH) {
digitalWrite(motorPin, HIGH);
}
{
boolean state = false;
if(digitalRead(7) == HIGH) {
digitalWrite(motorPin, LOW);
}
}
}
———-/——————-
터치센서를 한번 누르면 켜지고 다시 한번 누르면 꺼지도록 하고싶은데 켜지긴켜지는데 다시 누르면 꺼지지가 않네요ㅠㅠㅠ그냥 약해지기만해요 급합니다 제발 도와주세요
//
// DateTime : 2018-12-12 오전 10:03:22// by Ok-Hyun Park//const int switchPin=7;const int motorPin=9;//int switchState=0;int motorState=0;//void setup(){pinMode(motorPin,OUTPUT);pinMode(switchPin,INPUT);}//void loop(){if(digitalRead(switchPin)){if(switchState==0){ switchState=1;if(motorState){ motorState=0; digitalWrite(motorPin,LOW); }else { motorState=1; digitalWrite(motorPin,HIGH); }}}else switchState=0;}
몇줄 안되도 초보자에겐 어려울 수 있으니 설명을 해드리죠
if(digitalRead(switchPin)==HIGH){ // 스위치를 누르면if(switchState==0){ switchState=1;}}else switchState=0;
초기값 switchState=0;이므로 누르면 if문이 실행 되지만
switchState=1;로 변경하므로 오래 누르고 있어도 1회만 실행 됩니다.
디바운싱 코드로 생각하시면 됩니다.
else switchState=0;
스위치를 누르지 않으면 0으로 초기화 시킵니다.
//
if(motorState){ motorState=0; digitalWrite(motorPin,LOW); }else { motorState=1; digitalWrite(motorPin,HIGH); }motorState가 1 이면(0 이 아니면) motorState=0;으로 변경하고 모터 출력 OFF
motorState가 0 이면 motorState=1;로 변경하고 모터 출력 ON
댓글 0
조회수 3,904등록된 댓글이 없습니다.