Anyone want to modify this code for me? What it does right now is use two servos to move an eye around randomly. I want to modify it so that using a motion sensor, one servo controls the eye moving randomly back and forth, and the other servo controls the eyelid opening when it senses movement, and closing when there's no more movement. So the eye servo would still be random, but the eyelid servo would go from 0 to 180 when there's movement, then back from 180 to 0 after a few seconds of no movement.
#include <Servo.h> //include servo library for servo control
Servo horServo; //servo for left/right movement
Servo vertServo; //servo for up/down movement
byte randomhor; //define random horizontal position variable
byte randomvert; //define random vertical position variable
int randomdelay; //define random delay variable
#define HLEFTLIMIT 0 //define left limit on horizontal (left/right) servo
#define HRIGHTLIMIT 180 //define right limit on horizontal (left/right) servo
#define VTOPLIMIT 60 //define top limit on vertical (up/down) servo
#define VBOTLIMIT 180 //define bottom limit on horizontal (up/down) servo
void setup()
{
horServo.attach(4); //horizontal servo on pin 8
vertServo.attach(5); //vertical servo on pin 9
randomSeed(analogRead(0)); //Create some random values using an unconnected analog pin
}
void loop()
{
randomhor = random(HLEFTLIMIT, HRIGHTLIMIT); //set limits
randomvert = random(VTOPLIMIT, VBOTLIMIT); //set limits
randomdelay = random(1000, 4000); //moves every 1 to 4 seconds
horServo.write(randomhor); //write to the horizontal servo
vertServo.write(randomvert); //write to the vertical servo
delay(randomdelay); //delay a random amount of time (within values set above)
}
moving_eye.ino (1.24 KB)