0
PIR 센서를 사용하여 Arduino Haunted 호박을 만들어 조명 및 입을위한 LED를 트리거하려고합니다. 나는 입 LED를 즉시 끄기를 원하지만 눈이 사라 지길 바란다. 나는 지금 몇 시간 동안이 작업을 해왔고 왜 똑같은 코드 스 니핏 (snippit)이 자신의 프로그램에서 잘 작동한다고해도 눈 LED가 퇴색하지 않을 이유를 알 수 없습니다. 작고 쉬운 것을 놓친 것 같지만 찾을 수가 없습니다.Arduino PWM LED가 페이드 인 또는 페이 아웃되지 않음
부드럽게 관리하십시오. 나는 코드가 지저분하다는 것을 안다. 나는 수많은 다른 것들을 시도해 보았고 나중에 필요할 경우를 대비해 삭제하는 것이 아니라 그들을 주석 처리하는 경향이 있습니다.
//Uses a PIR sensor to detect movement.
//Randomly selects a number that corresponds to a set of LED's
//Lights up LED's
//Author:
//Date: 11/12/2014
int inputPin = A1; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
//int randNumber; //variable for holding the random number
//int eyeblue =6; //Variable for Blue eye LED's
//int eyered =9; //Variable for Red eye LED's
//int eyegreen =3; //Variable for Green eye LED's
//int mouthblue =10; // Variable for Blue mouth LED's
//int mouthred = 11; //Variable for Red mouth LED's
//int mouthgreen = 5; //Variable for Green eye LED's
int eyespin = 0;
int mouthpin = 0;
int moutharray[] ={
5,10,11};
int eyesarray[] = {
3,6,9};
//int thisPin ;
void setup(){
Serial.begin(9600);
pinMode(inputPin, INPUT); // declare PIR sensor as input
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
//randomSeed(analogRead(0));
}
void motion(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
eyes();
//mouth();
delay(1000);
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
delay(300);
}
}
else {
delay(300);
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
void eyes(){
eyespin = eyesarray [random (0, 3)];
mouthpin = moutharray[random (0, 3)];
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
digitalWrite(eyespin, fadeValue); // turn the LED on (HIGH is the voltage level)
delay(30);
}
digitalWrite(mouthpin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
digitalWrite(eyespin, fadeValue); // turn the LED off by making t
delay(30);
}
digitalWrite(mouthpin, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
}
void loop(){
// eyes();
// mouth();
motion();
}
대단히 감사합니다! –