1

我正在尝试使用 PIR 传感器制作一个 Arduino Haunted 南瓜来触发灯和嘴的 LED。我希望嘴里的 LED 立即关闭,但希望眼睛消失。我已经为此工作了几个小时,无法弄清楚为什么眼睛 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();
}
4

1 回答 1

1

非常简单的疏忽。该行:

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。

希望这可以为您解决问题。

于 2014-11-21T22:19:10.813 回答