我正在尝试使用PIR传感器制作一个Arduino闹鬼的南瓜来触发LED用于灯光和嘴巴。我希望嘴唇LED立即关闭,但希望眼睛消失。我已经在这个工作了好几个小时,并且无法弄清楚为什么眼睛的LED不会淡入或淡出,即使相同的代码snippit在它自己的程序中运行得很好。我可能错过了一些简单易行的东西,但似乎无法找到它。
要温柔。我知道代码很乱。我尝试了很多不同的东西,并倾向于将它们评论出来而不是删除,以防我以后需要它们。
//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();
}
答案 0 :(得分:0)
非常简单的疏忽。这一行:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
digitalWrite(eyespin, fadeValue);
应该写:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
analogWrite(eyespin, fadeValue);
注意它现在的analogWrite而不是digitalWrite。数字写入只能产生值0/1,我们需要0-255。
希望这能为你解决问题。