0

I want to implement a simple LED controller with an Arduino Uno, that goes to sleep and has different buttons.

Functions of buttons are:

  • Digital 2: Button for ON OFF
  • Digital 3: Button for Wake up

Everything works ok, but when it goes to sleep, the LEDs also turn off. I want that after 30 seconds, when Arduino goes to sleep, lights stays on.

Here is my code:

      #include <avr/sleep.h>
      #define REDPIN 10
      #define GREENPIN 11
      #define BLUEPIN 9
      #define delayTime 20  //za fading cas

      unsigned long interval= 30000;
      unsigned long previousMillis = 0;
      const int ledPin = 12;        // the pin that the LED is attached to
      const int buttonPin1 = 2;     //on off

      bool vklop = false;
      int bela = 10;
      int barva;
      int prejsnja_barva = 0;

      int buttonPushCounter1 = 0;   // counter for the number of button presses
      int buttonState1 = 0;         // current state of the button
      int lastButtonState1 = 0;     // previous state of the button



      /////////////////////////////////////*SETUP*/////////////////////////////////////////
      void setup() 
      {
      pinMode(buttonPin1, INPUT);  
      pinMode(ledPin, OUTPUT);
      Serial.begin(9600);
      pinMode(3,INPUT);                //because of interrupts PIN digital 3
      digitalWrite(3,HIGH);
      }

      /////////////////////////////////////*LOOP*/////////////////////////////////////////
      void loop() 
      {
         unsigned long currentMillis = millis();

         if ((currentMillis-previousMillis) > interval)       //15s timer
        {
          previousMillis = currentMillis;      
          Serial.println("SLEEP!");    // kaj delaj po preteku 5s
          delay(50);
          sleepSetup();                        //sleep mode
        }
         else
        {
        buttonState1 = digitalRead(buttonPin1);


      /////////////////////////////////////ON/OFF///////////////////////////////////////// 
      /////////////////////////////////////ON/OFF/////////////////////////////////////////
        if (buttonState1 != lastButtonState1)  // compare the buttonState to its previous state
        {

          if (buttonState1 == HIGH) // if the state has changed, increment the counter
          {

            buttonPushCounter1++;   // if the current state is HIGH then the button went from off to on:
            Serial.println("on");
            Serial.print("number of BUTTON1 pushes: ");
            Serial.println(buttonPushCounter1);
            digitalWrite(ledPin, HIGH);
            if(buttonPushCounter1 % 2 == 0)
            {
              setColor(bela, bela, bela);
              vklop = true;
              barva = 13;
            }
            else
            { 
              setColor(0, 0, 0);
              vklop = false;

            }
          } 
          else                      // if the current state is LOW then the button went from on to off:
          {

            Serial.println("off");
            digitalWrite(ledPin, LOW);
          }

          delay(50);                // Delay a little bit to avoid bouncing
        }

        lastButtonState1 = buttonState1;  // save the current state as the last state, for next time through the loop

      }

      }



      /////////////////////////////////functions/////////////////////////////////////////////
      /////////////////////////////////functions/////////////////////////////////////////////
      /////////////////////////////////functions/////////////////////////////////////////////
       void setColor(int red, int green, int blue)
        {
        analogWrite(REDPIN, red);
        analogWrite(GREENPIN, green);
        analogWrite(BLUEPIN, blue);
        }




      void sleepSetup(void)
      {

          sleep_enable();    // Set sleep enable (SE) bit:
          attachInterrupt(1, pinInterrupt, LOW);   // Set pin 2 as interrupt and attach handler:
          set_sleep_mode(SLEEP_MODE_PWR_DOWN);    // define our preferred sleep mode:
          digitalWrite(13,LOW);
          sleep_cpu();     

          Serial.println("Just woke up!");           //OD TU SE NADALJUJE PO PRITISKU TIPKE
          digitalWrite(13,HIGH);
      }


       void pinInterrupt() //ISR
      {
        sleep_disable(); 
        detachInterrupt(0);
      } 
XAB3
  • 32
  • 6
  • What sleep? I don't see any references to sleep in the code. It also has a lot unused variables and missing functions. Create a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – gre_gor Oct 26 '17 at 18:29
  • Thank you for your post, It is my first one. Next time I will create a better one. I will paste other functions. All variables are used, sorry I will create better code in the future from now one. I cannot paste all code, cause it is limited by characters – J.Puschnik Oct 26 '17 at 19:05
  • 1
    you are not supposed to post all code. you are supposed to reduce your code to a [mcve]. remove anything that does not belong to the problem you're facing. you don't need 8 buttons, countless colours and whatnot to demonstrate that sleep turns off all leds. 1 led would also be enough. delete everything that is not necessary . this will most likely help you find the error anyway. – Piglet Oct 26 '17 at 19:35
  • 1
    and don't say you'll do it better next time. improve this post first ;) – Piglet Oct 26 '17 at 19:38
  • Here you go. I minimalized code, now it's only one button and sleep. I hope, that's easy to find a problem. – J.Puschnik Oct 26 '17 at 20:02

1 Answers1

2

You're using AVR's Power Down sleep mode. In this mode all timers are turned off to save power. No timers -> no PWM -> no analogue output -> no PWM driven LEDs

To keep the LED on use another sleep mode.

See http://www.atmel.com/images/Atmel-8271-8-bit-AVR-Microcontroller-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet_Complete.pdf for details.

But to be honest I am not quite sure if this makes any sense. If you're driving an LED through 3 outputs the power you can save by putting the MCU into sleep is maybe a few percent.

And as sleep stops the CPU and hence your program you won't be able to have the LEDs turn off after 30s.

Why not just wait 30s befor going to sleep? The alternative would be some external timing circuitry that would also consume power. So I guess having a few milliamps more for 30 seconds is still a better alternative.

Piglet
  • 27,501
  • 3
  • 20
  • 43