0
我正在試圖製作一個Arduino鬧鬼的南瓜,使用PIR傳感器來觸發燈光和嘴巴的LED。我想讓嘴巴的LED立即關閉,但希望眼睛消失。我已經研究了幾個小時,並且無法弄清楚爲什麼眼睛的LED不會褪色或熄滅,即使相同的代碼snippit在它自己的程序中工作得很好。我可能錯過了一些小而簡單的東西,但似乎無法找到它。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();
}
非常感謝! –