Facebook
From vvd, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 258
  1. #include <Adafruit_NeoPixel.h>
  2. #include <avr/sleep.h>
  3. #include <avr/power.h>
  4. #include <avr/wdt.h>
  5.  
  6. #ifdef __AVR__
  7. #include <avr/power.h>
  8. #endif
  9.  
  10. #define PIN 13
  11. #define NEOPIXELS 59
  12.  
  13. int zapal = 0 ;
  14. volatile int f_wdt = 1;
  15.  
  16. ISR(WDT_vect) {
  17.   if (f_wdt == 0) {
  18.     // here we can implement a counter the can set the f_wdt to true if
  19.     // the watchdog cycle needs to run longer than the maximum of eight
  20.     // seconds.
  21.     f_wdt = 1;
  22.   }
  23. }
  24.  
  25. void enterSleep(void)
  26. {
  27.   // There are five different sleep modes in order of power saving:
  28.   // SLEEP_MODE_IDLE - the lowest power saving mode
  29.   // SLEEP_MODE_ADC
  30.   // SLEEP_MODE_PWR_SAVE
  31.   // SLEEP_MODE_STANDBY
  32.   // SLEEP_MODE_PWR_DOWN - the highest power saving mode
  33.   set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  34.   sleep_enable();
  35.  
  36.   // Now enter sleep mode.
  37.   sleep_mode();
  38.  
  39.   // The program will continue from here after the WDT timeout
  40.  
  41.   // First thing to do is disable sleep.
  42.   sleep_disable();
  43.  
  44.   // Re-enable the peripherals.
  45.   power_all_enable();
  46. }
  47.  
  48. void setupWatchDogTimer() {
  49.   // The MCU Status Register (MCUSR) is used to tell the cause of the last
  50.   // reset, such as brown-out reset, watchdog reset, etc.
  51.   // NOTE: for security reasons, there is a timed sequence for clearing the
  52.   // WDE and changing the time-out configuration. If you don't use this
  53.   // sequence properly, you'll get unexpected results.
  54.  
  55.   // Clear the reset flag on the MCUSR, the WDRF bit (bit 3).
  56.   MCUSR &= ~(1 << WDRF);
  57.  
  58.   // Configure the Watchdog timer Control Register (WDTCSR)
  59.   // The WDTCSR is used for configuring the time-out, mode of operation, etc
  60.  
  61.   // In order to change WDE or the pre-scaler, we need to set WDCE (This will
  62.   // allow updates for 4 clock cycles).
  63.  
  64.   // Set the WDCE bit (bit 4) and the WDE bit (bit 3) of the WDTCSR. The WDCE
  65.   // bit must be set in order to change WDE or the watchdog pre-scalers.
  66.   // Setting the WDCE bit will allow updates to the pre-scalers and WDE for 4
  67.   // clock cycles then it will be reset by hardware.
  68.   WDTCSR |= (1 << WDCE) | (1 << WDE);
  69.  
  70.   /**
  71.       Setting the watchdog pre-scaler value with VCC = 5.0V and 16mHZ
  72.       WDP3 WDP2 WDP1 WDP0 | Number of WDT | Typical Time-out at Oscillator Cycles
  73.       0    0    0    0    |   2K cycles   | 16 ms
  74.       0    0    0    1    |   4K cycles   | 32 ms
  75.       0    0    1    0    |   8K cycles   | 64 ms
  76.       0    0    1    1    |  16K cycles   | 0.125 s
  77.       0    1    0    0    |  32K cycles   | 0.25 s
  78.       0    1    0    1    |  64K cycles   | 0.5 s
  79.       0    1    1    0    |  128K cycles  | 1.0 s
  80.       0    1    1    1    |  256K cycles  | 2.0 s
  81.       1    0    0    0    |  512K cycles  | 4.0 s
  82.       1    0    0    1    | 1024K cycles  | 8.0 s
  83.   */
  84.   WDTCSR  = (0 << WDP3) | (1 << WDP2) | (1 << WDP1) | (1 << WDP0);
  85.   // Enable the WD interrupt (note: no reset).
  86.   WDTCSR |= _BV(WDIE);
  87. }
  88.  
  89.  
  90. // Parameter 1 = number of pixels in strip
  91. // Parameter 2 = Arduino pin number (most are valid)
  92. // Parameter 3 = pixel type flags, add together as needed:
  93. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  94. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  95. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  96. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  97. Adafruit_NeoPixel strip = Adafruit_NeoPixel(NEOPIXELS, PIN, NEO_GRB + NEO_KHZ800);
  98.  
  99. // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
  100. // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
  101. // and minimize distance between Arduino and first pixel.  Avoid connecting
  102. // on a live circuit...if you must, connect GND first.
  103.  
  104. void setup() {
  105.   // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
  106. #if defined (__AVR_ATtiny85__)
  107.   if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  108. #endif
  109.   // End of trinket special code
  110.  
  111.   Serial.begin(9600);
  112.   strip.begin();
  113.   Serial.println("Initialising...");
  114.   delay(50);
  115.   strip.show(); // Initialize all pixels to 'off'
  116.   setupWatchDogTimer();
  117.   Serial.println("Initialisation complete.");
  118.   delay(50);
  119. }
  120.  
  121. void loop() {
  122.   Serial.print(zapal);
  123.   Serial.print(f_wdt);
  124.  
  125.   if (f_wdt != 1) {
  126.     return;
  127.   }
  128.  
  129.   if (zapal == 0) {
  130.     colorWipe(strip.Color(0, 0, 255), 100); // Blue
  131.     zapal = 1;
  132.     Serial.print(f_wdt);
  133.  
  134.   }
  135.   else {
  136.     Serial.print("SLEEP");
  137.     f_wdt = 0;
  138.     Serial.print(f_wdt);
  139.  
  140.     enterSleep();
  141.     //colorWipe(strip.Color(255, 0, 0), 50); // Blue
  142.  
  143.   }
  144.   // Some example procedures showing how to display to the pixels:
  145.   //colorWipe(strip.Color(255, 0, 0), 50); // Red
  146.   //colorWipe(strip.Color(0, 255, 0), 50); // Green
  147.   // Send a theater pixel chase in...
  148.   //theaterChase(strip.Color(127, 127, 127), 50); // White
  149.   //theaterChase(strip.Color(127, 0, 0), 50); // Red
  150.   //theaterChase(strip.Color(0, 0, 127), 50); // Blue
  151.  
  152.   //rainbow(20);
  153.   //rainbowCycle(20);
  154.   //theaterChaseRainbow(50);
  155. }
  156.  
  157. // Fill the dots one after the other with a color
  158. void colorWipe(uint32_t c, uint8_t wait) {
  159.   for (uint16_t i = 0; i < strip.numPixels(); i++) {
  160.     strip.setPixelColor(i, c);
  161.     strip.show();
  162.     Serial.print("LED: ");
  163.     Serial.println(i);
  164.     delay(wait);
  165.   }
  166. }
  167.  
  168. void rainbow(uint8_t wait) {
  169.   uint16_t i, j;
  170.  
  171.   for (j = 0; j < 256; j++) {
  172.     for (i = 0; i < strip.numPixels(); i++) {
  173.       strip.setPixelColor(i, Wheel((i + j) & 255));
  174.     }
  175.     strip.show();
  176.     delay(wait);
  177.   }
  178. }
  179.  
  180. // Slightly different, this makes the rainbow equally distributed throughout
  181. void rainbowCycle(uint8_t wait) {
  182.   uint16_t i, j;
  183.  
  184.   for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
  185.     for (i = 0; i < strip.numPixels(); i++) {
  186.       strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  187.     }
  188.     strip.show();
  189.     delay(wait);
  190.   }
  191. }
  192.  
  193. //Theatre-style crawling lights.
  194. void theaterChase(uint32_t c, uint8_t wait) {
  195.   for (int j = 0; j < 10; j++) { //do 10 cycles of chasing
  196.     for (int q = 0; q < 3; q++) {
  197.       for (int i = 0; i < strip.numPixels(); i = i + 3) {
  198.         strip.setPixelColor(i + q, c);  //turn every third pixel on
  199.       }
  200.       strip.show();
  201.  
  202.       delay(wait);
  203.  
  204.       for (int i = 0; i < strip.numPixels(); i = i + 3) {
  205.         strip.setPixelColor(i + q, 0);      //turn every third pixel off
  206.       }
  207.     }
  208.   }
  209. }
  210.  
  211. //Theatre-style crawling lights with rainbow effect
  212. void theaterChaseRainbow(uint8_t wait) {
  213.   for (int j = 0; j < 256; j++) {   // cycle all 256 colors in the wheel
  214.     for (int q = 0; q < 3; q++) {
  215.       for (int i = 0; i < strip.numPixels(); i = i + 3) {
  216.         strip.setPixelColor(i + q, Wheel( (i + j) % 255)); //turn every third pixel on
  217.       }
  218.       strip.show();
  219.  
  220.       delay(wait);
  221.  
  222.       for (int i = 0; i < strip.numPixels(); i = i + 3) {
  223.         strip.setPixelColor(i + q, 0);      //turn every third pixel off
  224.       }
  225.     }
  226.   }
  227. }
  228.  
  229. // Input a value 0 to 255 to get a color value.
  230. // The colours are a transition r - g - b - back to r.
  231. uint32_t Wheel(byte WheelPos) {
  232.   WheelPos = 255 - WheelPos;
  233.   if (WheelPos < 85) {
  234.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  235.   }
  236.   if (WheelPos < 170) {
  237.     WheelPos -= 85;
  238.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  239.   }
  240.   WheelPos -= 170;
  241.   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  242. }