- #include <Adafruit_NeoPixel.h>
- #include <avr/sleep.h>
- #include <avr/power.h>
- #include <avr/wdt.h>
- #ifdef __AVR__
- #include <avr/power.h>
- #endif
- #define PIN 13
- #define NEOPIXELS 59
- int zapal = 0 ;
- volatile int f_wdt = 1;
- ISR(WDT_vect) {
- if (f_wdt == 0) {
- // here we can implement a counter the can set the f_wdt to true if
- // the watchdog cycle needs to run longer than the maximum of eight
- // seconds.
- f_wdt = 1;
- }
- }
- void enterSleep(void)
- {
- // There are five different sleep modes in order of power saving:
- // SLEEP_MODE_IDLE - the lowest power saving mode
- // SLEEP_MODE_ADC
- // SLEEP_MODE_PWR_SAVE
- // SLEEP_MODE_STANDBY
- // SLEEP_MODE_PWR_DOWN - the highest power saving mode
- set_sleep_mode(SLEEP_MODE_PWR_DOWN);
- sleep_enable();
- // Now enter sleep mode.
- sleep_mode();
- // The program will continue from here after the WDT timeout
- // First thing to do is disable sleep.
- sleep_disable();
- // Re-enable the peripherals.
- power_all_enable();
- }
- void setupWatchDogTimer() {
- // The MCU Status Register (MCUSR) is used to tell the cause of the last
- // reset, such as brown-out reset, watchdog reset, etc.
- // NOTE: for security reasons, there is a timed sequence for clearing the
- // WDE and changing the time-out configuration. If you don't use this
- // sequence properly, you'll get unexpected results.
- // Clear the reset flag on the MCUSR, the WDRF bit (bit 3).
- MCUSR &= ~(1 << WDRF);
- // Configure the Watchdog timer Control Register (WDTCSR)
- // The WDTCSR is used for configuring the time-out, mode of operation, etc
- // In order to change WDE or the pre-scaler, we need to set WDCE (This will
- // allow updates for 4 clock cycles).
- // Set the WDCE bit (bit 4) and the WDE bit (bit 3) of the WDTCSR. The WDCE
- // bit must be set in order to change WDE or the watchdog pre-scalers.
- // Setting the WDCE bit will allow updates to the pre-scalers and WDE for 4
- // clock cycles then it will be reset by hardware.
- WDTCSR |= (1 << WDCE) | (1 << WDE);
- /**
- Setting the watchdog pre-scaler value with VCC = 5.0V and 16mHZ
- WDP3 WDP2 WDP1 WDP0 | Number of WDT | Typical Time-out at Oscillator Cycles
- 0 0 0 0 | 2K cycles | 16 ms
- 0 0 0 1 | 4K cycles | 32 ms
- 0 0 1 0 | 8K cycles | 64 ms
- 0 0 1 1 | 16K cycles | 0.125 s
- 0 1 0 0 | 32K cycles | 0.25 s
- 0 1 0 1 | 64K cycles | 0.5 s
- 0 1 1 0 | 128K cycles | 1.0 s
- 0 1 1 1 | 256K cycles | 2.0 s
- 1 0 0 0 | 512K cycles | 4.0 s
- 1 0 0 1 | 1024K cycles | 8.0 s
- */
- WDTCSR = (0 << WDP3) | (1 << WDP2) | (1 << WDP1) | (1 << WDP0);
- // Enable the WD interrupt (note: no reset).
- WDTCSR |= _BV(WDIE);
- }
- // Parameter 1 = number of pixels in strip
- // Parameter 2 = Arduino pin number (most are valid)
- // Parameter 3 = pixel type flags, add together as needed:
- // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
- // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
- // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
- // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(NEOPIXELS, PIN, NEO_GRB + NEO_KHZ800);
- // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
- // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
- // and minimize distance between Arduino and first pixel. Avoid connecting
- // on a live circuit...if you must, connect GND first.
- void setup() {
- // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
- #if defined (__AVR_ATtiny85__)
- if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
- #endif
- // End of trinket special code
- Serial.begin(9600);
- strip.begin();
- Serial.println("Initialising...");
- delay(50);
- strip.show(); // Initialize all pixels to 'off'
- setupWatchDogTimer();
- Serial.println("Initialisation complete.");
- delay(50);
- }
- void loop() {
- Serial.print(zapal);
- Serial.print(f_wdt);
- if (f_wdt != 1) {
- return;
- }
- if (zapal == 0) {
- colorWipe(strip.Color(0, 0, 255), 100); // Blue
- zapal = 1;
- Serial.print(f_wdt);
- }
- else {
- Serial.print("SLEEP");
- f_wdt = 0;
- Serial.print(f_wdt);
- enterSleep();
- //colorWipe(strip.Color(255, 0, 0), 50); // Blue
- }
- // Some example procedures showing how to display to the pixels:
- //colorWipe(strip.Color(255, 0, 0), 50); // Red
- //colorWipe(strip.Color(0, 255, 0), 50); // Green
- // Send a theater pixel chase in...
- //theaterChase(strip.Color(127, 127, 127), 50); // White
- //theaterChase(strip.Color(127, 0, 0), 50); // Red
- //theaterChase(strip.Color(0, 0, 127), 50); // Blue
- //rainbow(20);
- //rainbowCycle(20);
- //theaterChaseRainbow(50);
- }
- // Fill the dots one after the other with a color
- void colorWipe(uint32_t c, uint8_t wait) {
- for (uint16_t i = 0; i < strip.numPixels(); i++) {
- strip.setPixelColor(i, c);
- strip.show();
- Serial.print("LED: ");
- Serial.println(i);
- delay(wait);
- }
- }
- void rainbow(uint8_t wait) {
- uint16_t i, j;
- for (j = 0; j < 256; j++) {
- for (i = 0; i < strip.numPixels(); i++) {
- strip.setPixelColor(i, Wheel((i + j) & 255));
- }
- strip.show();
- delay(wait);
- }
- }
- // Slightly different, this makes the rainbow equally distributed throughout
- void rainbowCycle(uint8_t wait) {
- uint16_t i, j;
- for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
- for (i = 0; i < strip.numPixels(); i++) {
- strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
- }
- strip.show();
- delay(wait);
- }
- }
- //Theatre-style crawling lights.
- void theaterChase(uint32_t c, uint8_t wait) {
- for (int j = 0; j < 10; j++) { //do 10 cycles of chasing
- for (int q = 0; q < 3; q++) {
- for (int i = 0; i < strip.numPixels(); i = i + 3) {
- strip.setPixelColor(i + q, c); //turn every third pixel on
- }
- strip.show();
- delay(wait);
- for (int i = 0; i < strip.numPixels(); i = i + 3) {
- strip.setPixelColor(i + q, 0); //turn every third pixel off
- }
- }
- }
- }
- //Theatre-style crawling lights with rainbow effect
- void theaterChaseRainbow(uint8_t wait) {
- for (int j = 0; j < 256; j++) { // cycle all 256 colors in the wheel
- for (int q = 0; q < 3; q++) {
- for (int i = 0; i < strip.numPixels(); i = i + 3) {
- strip.setPixelColor(i + q, Wheel( (i + j) % 255)); //turn every third pixel on
- }
- strip.show();
- delay(wait);
- for (int i = 0; i < strip.numPixels(); i = i + 3) {
- strip.setPixelColor(i + q, 0); //turn every third pixel off
- }
- }
- }
- }
- // Input a value 0 to 255 to get a color value.
- // The colours are a transition r - g - b - back to r.
- uint32_t Wheel(byte WheelPos) {
- WheelPos = 255 - WheelPos;
- if (WheelPos < 85) {
- return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
- }
- if (WheelPos < 170) {
- WheelPos -= 85;
- return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
- }
- WheelPos -= 170;
- return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
- }