Hi All,
I'm putting together a Halloween project - Pumpkin with flashing/fading eyes + spider that moves (Stuck to a servo!).
I managed to get the LEDs (eyes!) flashing/fading...then attached the servo. Problem is, the servo jitters when I expect the LEDs to fade - See below fadeEyes(), ands the LEDs do not fade if the servo pin is attached, by attached, I mean the line servoSpider.attach(spider); runs, I have tried with the servo disconnected, thinking I might have a faulty servo, although the servo does work AOK on its own.
Any help appreciated...
Thanks,
Simon.
/*
Pumpkin Halloween 2012
* 2 Flashing Red LED eyes
* 1 Servo driven spinning spider
*/
#include <Servo.h>
// Spider
Servo servoSpider;
int servoSpiderPos = 0;
int servoSpiderWaitTime = 2;
int servoSpiderRotate = 360;
// Eyes
int spider = 4; // Pin for Spider
int eyeOne = 9; // Pin for Eye 1
int eyeTwo = 10; // Pin for Eye 2
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
long actionDelay; // Random delay between eye flash/delay (Between 3 and 20 seconds)
int onOffTime = 100; // How long eyes on/off for in blink stage
int blinks = 25; // How many times eyes blink
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(9600);
servoSpider.attach(spider);
pinMode(eyeOne, OUTPUT);
pinMode(eyeTwo, OUTPUT);
//actionDelay = 1000;
}
// the loop routine runs over and over again forever:
void loop() {
Serial.println("Begin: Loop");
actionDelay = random(500, 3000);
Serial.println(actionDelay);
fadeEyes();
delay(actionDelay);
blinkEyes();
delay(actionDelay);
// Spin spider?
if ((actionDelay % 2) == 0)
{
awakeSpider();
}
Serial.println("End: Loop");
}
void fadeEyes()
{
Serial.println("Begin: Fade Eyes");
for (int i = 0; i < 255; i++)
{
Serial.println(i);
Serial.println(eyeOne);
Serial.println(eyeTwo);
analogWrite(eyeOne, i);
analogWrite(eyeTwo, i);
delay(5);
}
for (int i = 255; i >= 0; i--)
{
Serial.println(i);
Serial.println(eyeOne);
Serial.println(eyeTwo);
analogWrite(eyeOne, i);
analogWrite(eyeTwo, i);
delay(5);
}
Serial.println("Complete: Fade Eyes");
}
void blinkEyes()
{
Serial.println("Begin: Blinking Eyes");
for (int i = 0; i <= blinks; i++)
{
analogWrite(eyeOne, 255);
analogWrite(eyeTwo, 255);
delay(onOffTime);
analogWrite(eyeOne, 0);
analogWrite(eyeTwo, 0);
delay(onOffTime);
}
Serial.println("Complete: Blinking Eyes");
}
void awakeSpider()
{
Serial.println("Begin: Awake Spider");
servoSpider.write(0);
delay(500);
servoSpider.write(170);
delay(500);
Serial.println("Complete: Awake Spider");
}