It chirps, it's electronic, it's the chirpbot.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
3.9 KiB

2 years ago
  1. // https://www.youtube.com/watch?v=tVwQ8cb8HbQ
  2. // ATtyny85 chirping and sleep
  3. int analogPin = 3;
  4. /////////////
  5. #include <avr/sleep.h>
  6. void setup() {
  7. pinMode(PB4, OUTPUT); //BEEP
  8. pinMode(PB3, INPUT); // ANALOG INPUT FOR RANDOM
  9. // randomSeed(analogRead(0));
  10. randomSeed(analogRead(PB3));
  11. setup_watchdog(7); //8 seconds
  12. }
  13. /////////////
  14. void loop() {
  15. sleep();
  16. }
  17. /////////////
  18. void sleep() {
  19. digitalWrite (PB4,LOW);
  20. ADCSRA &= ~_BV(ADEN); // ADC off
  21. set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  22. sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
  23. sei(); // Enable interrupts
  24. sleep_cpu(); // sleep
  25. // cli(); // Disable interrupts
  26. // sleep_disable(); // Clear SE bit
  27. // sei(); // Enable interrupts
  28. }
  29. // Watchdog Interrupt Service / is executed when watchdog timed out
  30. ISR(WDT_vect) {
  31. cli(); // Disable interrupts
  32. sleep_disable(); // Clear SE bit
  33. sei();
  34. // BEGIN: this is where sound is generated
  35. tweet(2, 1);
  36. //chirp();
  37. // END: this is where sound is generated
  38. // setup_watchdog(random(5)+4); // add by tez
  39. // randomSeed(analogRead(analogPin));
  40. setup_watchdog(random(5)+random(3) + 2); // add by tez
  41. }
  42. // 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
  43. // 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
  44. void setup_watchdog(int ii) {
  45. byte bb;
  46. int ww;
  47. if (ii > 9 ) ii=9;
  48. bb=ii & 7;
  49. if (ii > 7) bb|= (1<<5);
  50. bb|= (1<<WDCE);
  51. ww=bb;
  52. MCUSR &= ~(1<<WDRF);
  53. // start timed sequence
  54. WDTCR |= (1<<WDCE) | (1<<WDE);
  55. // set new watchdog timeout value
  56. WDTCR = bb;
  57. WDTCR |= _BV(WDIE);
  58. }
  59. /////////////
  60. void chirp() {
  61. int inten_rand, chirpNum_rand;
  62. inten_rand = int(random(2,4));
  63. chirpNum_rand = int(random(3,6));
  64. highChirp(inten_rand,chirpNum_rand);
  65. // delay(100);
  66. delay(random(50,200));
  67. if(random(2) == true){
  68. lowChirp(200,2);
  69. }
  70. // delay(100);
  71. delay(random(50,200));
  72. // tweet(4,2);
  73. }
  74. /////////////
  75. void highChirp(int intensity, int chirpsNumber){
  76. int i;
  77. int x;
  78. int chirp_delay;
  79. // chirp_delay = random(6, 9)*4;
  80. chirp_delay = random(1, 15)*4;
  81. int reptimes = random(60, 300);
  82. for(int veces=0; veces<=chirpsNumber; veces++){
  83. for (i=50; i<reptimes; i=(i+1)) // 200
  84. {
  85. for (x=0; x<intensity; x++)
  86. {
  87. digitalWrite (PB4,HIGH);
  88. delayMicroseconds(i);
  89. //if(i>300) delayMicroseconds(i);
  90. //else delayMicroseconds ((400-i));
  91. digitalWrite (PB4,LOW);
  92. delayMicroseconds(i);
  93. //if(i>300) delayMicroseconds(i);
  94. //else delayMicroseconds ((400-i));
  95. //delayMicroseconds (i);
  96. }
  97. }
  98. delay(chirp_delay);
  99. chirp_delay=chirp_delay*0.75;
  100. }
  101. }
  102. /////////////
  103. void lowChirp(int intensity, int chirpsNumber){
  104. int i;
  105. int x;
  106. int reptimes = random(1, 2); //random(100, 300);
  107. for(int veces=0; veces<=chirpsNumber; veces++){
  108. for (i=0; i<reptimes; i++) // 200
  109. {
  110. digitalWrite (PB4,HIGH);
  111. delayMicroseconds(i);
  112. digitalWrite(PB4,LOW);
  113. delayMicroseconds(i);
  114. }
  115. int i_hi = random(90,150);
  116. int i_lo = abs(i_hi - random(30,90));
  117. // for (i=90; i>80; i--)
  118. for (i=i_hi; i>i_lo; i--)
  119. {
  120. int xrep = random(1,9);
  121. for ( x=0; x<xrep; x++) // 5
  122. {
  123. digitalWrite (PB4,HIGH);
  124. delayMicroseconds (i);
  125. digitalWrite (PB4,LOW);
  126. delayMicroseconds (i);
  127. }
  128. }
  129. }
  130. }
  131. /////////////
  132. void tweet(int intensity, int chirpsNumber){
  133. int i;
  134. int x;
  135. //normal chirpsNumber 3, normal intensity 5
  136. for(int veces=0; veces<chirpsNumber; veces++){
  137. for (int i=80; i>0; i--)
  138. {
  139. for (int x=0; x<intensity; x++)
  140. {
  141. digitalWrite (PB4,HIGH);
  142. delayMicroseconds (i);
  143. digitalWrite (PB4,LOW);
  144. delayMicroseconds (i);
  145. }
  146. }
  147. }
  148. delay(1000);
  149. }