Go Down

Topic: Problems fading and RGB led whilst using sleep library. (Read 331 times) previous topic - next topic

LPDE

Hello everyone,

I am trying to fade an RGB led from red, green to amber whilst going to sleep in between transitions. The reason for this is to save power. However I also want the Arduino to wake up for 2s after sleeping for 8s a number of times.

What I want it to do is  - Fade to red
                                - Stay on red and sleep for a number of counts
                                - Fade to amber
                                - Stay on amber and sleep for a number of counts
                                - Fade to green
                                - Stay on Green and finish

but what its actually doing is  - Fade to red
                                         - Stay on red and sleep for a number of counts
                                         - Fade to amber
                                         - blinks amber when awake and red when sleeping
                                         - Fade to green
                                         - Stay on Green and finish

Code: [Select]
#include <avr/sleep.h>
#include <avr/sleep.h>
#include <avr/wdt.h>

//Constants that won't change
const int BLUE = 11;              // the number of the blue LED
const int GREEN = 10;            // the number of the green LED
const int RED = 9;              // the number of the red LED

//Constants that will change
int Timerstart = 0; //Check if the light sequence has started or finished
int Timerstage = 1; //At which stage the lighting sequence is at
int SleepCount;  //The amount of times the Arduino has slept for

// watchdog interrupt
ISR (WDT_vect)
{
  wdt_disable();  // disable watchdog
}  // end of WDT_vect

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);

  // initialize the digital pin as an output.
  pinMode(BLUE, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(RED, OUTPUT);

}

void loop() {

  Serial.println (SleepCount);

  //******************************************************************************************************** RED GREEN AMBER ******************************************************************************************************************************************************

  if (Timerstart == 0)
  {
    if (Timerstage == 1) {

      for (int RVALUE = 0; RVALUE < 255; RVALUE = RVALUE + 1) {

        analogWrite(RED, RVALUE);   // Increment the red LED voltage value.
        delay(10); // Time taken for the brightness to reach 255 (in milliseconds).
        Timerstage = 2;
      }
    }

    analogWrite(RED, 255);   // Stay on Red

    if (Timerstage == 2)
    {
      while (SleepCount < 3)
      {

        MonitorBattery();
        LowPowerMode();
      }
    }

    if (SleepCount == 3)
    {
      Timerstage = 3;
      SleepCount = 0;
    }

    if (Timerstage == 3) {
      for (int GVALUE = 0; GVALUE < 20; GVALUE = GVALUE + 1) {

        analogWrite(GREEN, GVALUE); // Increment the green LED voltage value.
        delay(40); // Time taken for the brightness to reach 20 (in milliseconds).
        Timerstage = 4;
      }
    }

    analogWrite(GREEN, 20); // Stay on Green
    if (Timerstage == 4)
    {
      while (SleepCount < 3)
      {
        MonitorBattery();
        LowPowerMode();
      }
    }
    if (SleepCount == 3)
    {
      Timerstage = 5;
      SleepCount = 0;
    }

    float z = 1.416;

    for (int GVALUE = 20, RVALUE = 255; GVALUE<255, RVALUE>0; GVALUE = GVALUE + 1, RVALUE = RVALUE - z) {
      analogWrite(GREEN, GVALUE);   // Increment the green LED voltage value.
      analogWrite(RED, RVALUE);   // Decrement the red LED voltage value.
      delay(10); // Time taken for the brightness to reach 255 and 0 (in milliseconds).
    }

    analogWrite(GREEN, 150);   
    Timerstart = 1;// End of lighting sequence and stay on Green.
  }

  //************************************************************************************************************** End of Loop *********************************************************************************************************************************************
 
}
void LowPowerMode() { //Sleep during light sequence
  // disable ADC
  ADCSRA = 0;

  // clear various "reset" flags
  MCUSR = 0;
  // allow changes, disable reset
  WDTCSR = bit (WDCE) | bit (WDE);
  // set interrupt mode and an interval
  WDTCSR = bit (WDIE) | bit (WDP3) | bit (WDP0);    // set WDIE, and 8 seconds delay
  wdt_reset();  // pat the dog

  set_sleep_mode (SLEEP_MODE_PWR_DOWN);
  noInterrupts ();           // timed sequence follows
  sleep_enable();

  // turn off brown-out enable in software
  MCUCR = bit (BODS) | bit (BODSE);
  MCUCR = bit (BODS);
  interrupts ();             // guarantees next instruction executed
  sleep_cpu ();

  // cancel sleep as a precaution
  sleep_disable();
}
void MonitorBattery() { //Wakes up for 2s before going to sleep again.

  unsigned long startTime = millis();
  unsigned long interval = 2000; // 2 seconds
  SleepCount = SleepCount + 1;

  while (millis() - startTime < interval)
  {
    Serial.println("Seep Count"); //Sleep Status Check
    Serial.println (SleepCount);
  }
}


I'm assuming its because i cant use PMW during low power so I need away around that?Or it could just be my programming

Go Up