Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.02 KB | None | 0 0
  1.  
  2. #include <Servo.h>
  3.  
  4. Servo myservo;  // create servo object to control a servo
  5. // twelve servo objects can be created on most boards
  6.  
  7. int pos = 0;    // variable to store the servo position
  8. unsigned long time_now = 0;
  9. #include <avr/interrupt.h>
  10. #include <avr/wdt.h>
  11. #include <util/atomic.h>
  12. // The following addresses a problem in version 1.0.5 and earlier of the Arduino IDE
  13. // that prevents randomSeed from working properly.
  14. //        https://github.com/arduino/Arduino/issues/575
  15. #define randomSeed(s) srandom(s)
  16.  
  17. volatile uint32_t seed;  // These two variables can be reused in your program after the
  18. volatile int8_t nrot;    // function CreateTrulyRandomSeed()executes in the setup()
  19.                          // function.
  20.  
  21. void CreateTrulyRandomSeed()
  22. {
  23.   seed = 0;
  24.   nrot = 25; // Must be at least 4, but more increased the uniformity of the produced
  25.              // seeds entropy.
  26.  
  27.   // The following five lines of code turn on the watch dog timer interrupt to create
  28.   // the seed value
  29.   cli();                                            
  30.   MCUSR = 0;                                        
  31.   _WD_CONTROL_REG |= (1<<_WD_CHANGE_BIT) | (1<<WDE);
  32.   _WD_CONTROL_REG = (1<<WDIE);                      
  33.   sei();                                            
  34.  
  35.   while (nrot > 0);  // wait here until seed is created
  36.  
  37.   // The following five lines turn off the watch dog timer interrupt
  38.   cli();                                            
  39.   MCUSR = 0;                                        
  40.   _WD_CONTROL_REG |= (1<<_WD_CHANGE_BIT) | (0<<WDE);
  41.   _WD_CONTROL_REG = (0<< WDIE);                      
  42.   sei();                                            
  43. }
  44.  
  45. ISR(WDT_vect)
  46. {
  47.   nrot--;
  48.   seed = seed << 8;
  49.   seed = seed ^ TCNT1L;
  50. }
  51.  
  52. void setup()
  53. {
  54.   CreateTrulyRandomSeed();
  55.   randomSeed(seed);
  56.   myservo.attach(8);  // attaches the servo on pin 9 to the servo object
  57.   // The preceeding two function calls will take approximately 0.5 second to execute if
  58.   // nrot is set to 32 ... the rest of your setup code should FOLLOW from here.
  59. }
  60.  
  61.  
  62. void loop() {
  63.   int loop_delay = random(120, 702); // delay between loops
  64.   int period = random(4500,12000); // time before pausing again
  65.   if (time_now > period) { // pause for a while every `period`
  66.     delay(random(3000, 5000)); // how long to pause
  67.     time_now = 0;
  68.     return;
  69.   }
  70.    
  71.   for (pos = 0; pos <= 160; pos += 180) { // goes from 0 degrees to 180 degrees
  72.     // in steps of 1 degree
  73.     myservo.write(pos);              // tell servo to go to position in variable 'pos'
  74.     delay(100);                       // waits 15ms for the servo to reach the position
  75.   }
  76.   for (pos = 160; pos >= 0; pos -= 180) { // goes from 180 degrees to 0 degrees
  77.     myservo.write(pos);              // tell servo to go to position in variable 'pos'
  78.     delay(100);                       // waits 15ms for the servo to reach the position
  79.   }
  80.        
  81.   delay(loop_delay);      // adds random delay between (min,max)
  82.  
  83.   time_now += (100 + 100 + loop_delay);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement