Newer
Older
DC27_EarPlug / Attiny85_Source.ino
root on 15 Jul 2019 4 KB initial commit
  1. #include <avr/sleep.h>
  2. #include <avr/wdt.h>
  3. #ifndef cbi
  4. #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  5. #endif
  6. #ifndef sbi
  7. #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  8. #endif
  9.  
  10. int led = 1; // the PWM pin the LED is attached to
  11. volatile boolean f_wdt = 1;
  12. int counter = 0;
  13.  
  14. // the setup routine runs once when you press reset:
  15. void setup() {
  16. pinMode(led, OUTPUT);
  17. setup_watchdog(15); // approximately 4 seconds sleep
  18.  
  19. }
  20.  
  21. void fade(){
  22. for (int i = 0; i < 255; i++){
  23. analogWrite(led, i);
  24. delay(5);
  25. }
  26. for (int i = 255; i > 0; i--){
  27. analogWrite(led, i);
  28. delay(5);
  29. }
  30. }
  31.  
  32. void jumpFade(){
  33. for(int i = 0; i < 6; i++){
  34. int j = i * 50;
  35. analogWrite(led, j);
  36. delay(500);
  37. }
  38. for(int i = 5; i > 0; i--){
  39. int j = i * 50;
  40. analogWrite(led, j);
  41. delay(500);
  42. }
  43. }
  44.  
  45. void morse(){
  46. lS();
  47. lA();
  48. lY();
  49. space();
  50. lH();
  51. lI();
  52. space();
  53. }
  54.  
  55. void loop() {
  56. if (f_wdt==1) { // wait for timed out watchdog / flag is set when a watchdog timeout occurs
  57. f_wdt=0; // reset flag
  58. }
  59. fade();
  60. fade();
  61. fade();
  62. system_sleep();
  63. jumpFade();
  64. jumpFade();
  65. jumpFade();
  66. system_sleep();
  67. morse();
  68. morse();
  69. morse();
  70. system_sleep();
  71. }
  72.  
  73. //functions for the letters and the numbers
  74. void lA () {dot();dash();shortspace();}//letter A in morse code!
  75. void lB () {dash();dot();dot();dot();shortspace();}//same for B
  76. void lC () {dash();dot();dash();dot();shortspace();}
  77. void lD () {dash();dot();dot();shortspace();}
  78. void lE () {dot();shortspace();}
  79. void lF () {dot();dot();dash();dot();shortspace();}
  80. void lG () {dash();dash();dot();shortspace();}
  81. void lH () {dot();dot();dot();dot();shortspace();}
  82. void lI () {dot();dot();shortspace();}
  83. void lJ () {dot();dash();dash();dash();shortspace();}
  84. void lK () {dash();dot();dash();shortspace();}
  85. void lL () {dot();dash();dot();dot();shortspace();}
  86. void lM () {dash();dash();shortspace();}
  87. void lN () {dash();dot();shortspace();}
  88. void lO () {dash();dash();dash();shortspace();}
  89. void lP () {dot();dash();dash();dot();shortspace();}
  90. void lQ () {dash();dash();dot();dash();shortspace();}
  91. void lR () {dot();dash();dot();shortspace();}
  92. void lS () {dot();dot();dot();shortspace();}
  93. void lT () {dash();shortspace();}
  94. void lU () {dot();dot();dash();shortspace();}
  95. void lV () {dot();dot();dot();dash();shortspace();}
  96. void lW () {dot();dash();dash();shortspace();}
  97. void lX () {dash();dot();dot();dash();shortspace();}
  98. void lY () {dash();dot();dash();dash();shortspace();}
  99. void lZ () {dash();dash();dot();dot();shortspace();}
  100. void n1 () {dot();dash();dash();dash();dash();shortspace();}//number 1 in morse code
  101. void n2 () {dot();dot();dash();dash();dash();shortspace();}
  102. void n3 () {dot();dot();dot();dash();dash();shortspace();}
  103. void n4 () {dot();dot();dot();dot();dash();shortspace();}
  104. void n5 () {dot();dot();dot();dot();dot();shortspace();}
  105. void n6 () {dash();dot();dot();dot();dot();shortspace();}
  106. void n7 () {dash();dash();dot();dot();dot();shortspace();}
  107. void n8 () {dash();dash();dash();dot();dot();shortspace();}
  108. void n9 () {dash();dash();dash();dash();dot();shortspace();}
  109. void n0 () {dash();dash();dash();dash();dash();shortspace();}
  110. void space () {delay (1200);}//space between words
  111. void dot () {analogWrite(led, 150); delay (300); digitalWrite(led,LOW); delay (300);}//the dot this code make the led on for 300 than off for 300
  112. void dash () {analogWrite(led, 150); delay (900); digitalWrite(led,LOW); delay (300);}//the dash this code make the led on for 900 than off for 300
  113. void shortspace () {delay(600);}//space between letters
  114.  
  115. // set system into the sleep state
  116. // system wakes up when wtchdog is timed out
  117. void system_sleep() {
  118. digitalWrite(led,LOW);
  119. cbi(ADCSRA,ADEN); // switch Analog to Digitalconverter OFF
  120. set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
  121. sleep_enable();
  122. sleep_mode(); // System sleeps here
  123. sleep_disable(); // System continues execution here when watchdog timed out
  124. sbi(ADCSRA,ADEN); // switch Analog to Digitalconverter ON
  125. }
  126. // 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
  127. // 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
  128. void setup_watchdog(int ii) {
  129. byte bb;
  130. int ww;
  131. if (ii > 9 ) ii=9;
  132. bb=ii & 7;
  133. if (ii > 7) bb|= (1<<5);
  134. bb|= (1<<WDCE);
  135. ww=bb;
  136. MCUSR &= ~(1<<WDRF);
  137. // start timed sequence
  138. WDTCR |= (1<<WDCE) | (1<<WDE);
  139. // set new watchdog timeout value
  140. WDTCR = bb;
  141. WDTCR |= _BV(WDIE);
  142. }
  143.  
  144. // Watchdog Interrupt Service / is executed when watchdog timed out
  145. ISR(WDT_vect) {
  146. counter++;
  147. f_wdt=1; // set global flag
  148. }
Buy Me A Coffee