I've been working on this prop for a while and finally got it working. See video below.
Currently the owl head moves randomly about every 4 seconds (delay(1000)). The one thing I wish I could do is to make that delay a random number, say between 500 and 2000. I'm not good with the coding. Can anyone help me with that?
Incidentally, I'm only using one servo here—the yservo part of the code. I should take the xservo part out.
#include <Servo.h>
Servo xservo;
Servo yservo;
int xpos;
int ypos;
void setup()
{
xservo.attach(9);
yservo.attach(10);
}
void loop()
{
{
ypos = random(180); //generate random value for y-servo
yservo.write(ypos); //y-servo moves to new position
delay(1000);
}
{
xpos = random(180); //generate random value for x-servo
xservo.write(xpos); //x-servo moves to new position
delay(1000);
}
}
#include <Servo.h>
Servo xservo;
Servo yservo;
int xpos;
int ypos;
void setup()
{
xservo.attach(9);
yservo.attach(10);
}
void loop()
{
{ // no need to add this
ypos = random(180); //generate random value for y-servo
yservo.write(ypos); //y-servo moves to new position
delay(1000);
}// no need to add this
{// no need to add this
xpos = random(180); //generate random value for x-servo
xservo.write(xpos); //x-servo moves to new position
delay(1000);
}// no need to add this
}
then for random delay, simply use,
int delayAmount = random(500,2000); //generate number between 500 and 200
delay(delayAmount); // delay by the random amount
PaulS:
If you can get a random number for the servo position, it seems trivial to get a random number for the delay() value.
What have you tried?
It's one line of code.
I'm a complete newbie and I didn't know precisely how to generate a random delay. I'm on Project 05 of the Arduino Project book. Sort of jumping the gun a little with this owl project.
The reason for the random delay is that it makes the owl movements more realistic. Otherwise it's too regular.
The owl will have LED lights for eyes and I'll have a boombox making owl hoots. Should be very cool.