3_servo_n_2_moto_random.txt (1052Bytes)
Hi..New to code; I ask for advice on sketch shown below(attached txt also). This is another halloween bust that needs 2 motors+3 servo's all set to move randomly.
- Movement does not look so random..I do not know how to add Random seed which some say makes it better ?
- how to make entire movement triggered by 4-pin PIR sensor (ping & echo)? or even a shorter range IR reflection optocoupler ?
#include <Servo.h>
Servo servo1; // Define servo1
Servo servo2; // Define servo2
Servo servo3; // Define servo3
int MotPin1 = 9; // connect motor1 to pin 9
int MotPin2 = 10; // connect motor2 to pin 10
void setup()
{
pinMode(MotPin1, OUTPUT);
servo1.attach(3); // attaches the servo on pin 3
servo2.attach(5); // attaches the servo on pin 5
servo3.attach(6); // attaches the servo on pin 6
}
void loop()
{
// Run Motor 1 for 0.3 second at half speed
analogWrite(MotPin1, 150);
delay(300);
analogWrite(MotPin1, 0);
// delay for random time 0.5 - 2 seconds
delay(random(500,2000));
// Run Motor 2 for 0.5 second at half speed
analogWrite(MotPin2, 150);
delay(300);
analogWrite(MotPin1, 0);
// delay for random time 0.7 - 2,5 seconds
delay(random(700,2500));
// Move the servos to new random positions
servo1.write(random(60, 120));
delay(100);
servo2.write(random(30, 90));
delay (150);
servo3.write(random(30, 135));
delay(180);
}