I am attempting to make an Arduino Haunted pumpkin using a PIR sensor to trigger the LED's for the lights and mouth. I want the mouth LED's to shut off immediately, but want the eyes to fade away. I've been working on this for hours now and can't figure out why the eye LED's will not fade in OR out, even though the same code snippit works in it's own program just fine. I am probably missing something small and easy, but cannot seem to find it.
Be gentle. I know that the code is messy. I've tried numerous different things and tend to comment them out rather than deleting in case I need them later.
//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();
}
Very simple oversight. The line:
Should be written:
Notice its now analogWrite rather than digitalWrite. Digital write can only produce values 0/1, we need 0-255.
Hope this fixes things up for you.