Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- Servo myservo; // create servo object to control a servo
- // twelve servo objects can be created on most boards
- int pos = 0; // variable to store the servo position
- unsigned long time_now = 0;
- #include <avr/interrupt.h>
- #include <avr/wdt.h>
- #include <util/atomic.h>
- // The following addresses a problem in version 1.0.5 and earlier of the Arduino IDE
- // that prevents randomSeed from working properly.
- // https://github.com/arduino/Arduino/issues/575
- #define randomSeed(s) srandom(s)
- volatile uint32_t seed; // These two variables can be reused in your program after the
- volatile int8_t nrot; // function CreateTrulyRandomSeed()executes in the setup()
- // function.
- void CreateTrulyRandomSeed()
- {
- seed = 0;
- nrot = 25; // Must be at least 4, but more increased the uniformity of the produced
- // seeds entropy.
- // The following five lines of code turn on the watch dog timer interrupt to create
- // the seed value
- cli();
- MCUSR = 0;
- _WD_CONTROL_REG |= (1<<_WD_CHANGE_BIT) | (1<<WDE);
- _WD_CONTROL_REG = (1<<WDIE);
- sei();
- while (nrot > 0); // wait here until seed is created
- // The following five lines turn off the watch dog timer interrupt
- cli();
- MCUSR = 0;
- _WD_CONTROL_REG |= (1<<_WD_CHANGE_BIT) | (0<<WDE);
- _WD_CONTROL_REG = (0<< WDIE);
- sei();
- }
- ISR(WDT_vect)
- {
- nrot--;
- seed = seed << 8;
- seed = seed ^ TCNT1L;
- }
- void setup()
- {
- CreateTrulyRandomSeed();
- randomSeed(seed);
- myservo.attach(8); // attaches the servo on pin 9 to the servo object
- // The preceeding two function calls will take approximately 0.5 second to execute if
- // nrot is set to 32 ... the rest of your setup code should FOLLOW from here.
- }
- void loop() {
- int loop_delay = random(120, 702); // delay between loops
- int period = random(4500,12000); // time before pausing again
- if (time_now > period) { // pause for a while every `period`
- delay(random(3000, 5000)); // how long to pause
- time_now = 0;
- return;
- }
- for (pos = 0; pos <= 160; pos += 180) { // goes from 0 degrees to 180 degrees
- // in steps of 1 degree
- myservo.write(pos); // tell servo to go to position in variable 'pos'
- delay(100); // waits 15ms for the servo to reach the position
- }
- for (pos = 160; pos >= 0; pos -= 180) { // goes from 180 degrees to 0 degrees
- myservo.write(pos); // tell servo to go to position in variable 'pos'
- delay(100); // waits 15ms for the servo to reach the position
- }
- delay(loop_delay); // adds random delay between (min,max)
- time_now += (100 + 100 + loop_delay);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement