Arduino LED控制睡眠

时间:2017-10-26 18:02:33

标签: arduino arduino-uno arduino-ide led sleep-mode

我想实现一个带有Arduino Uno的简单LED控制器,它可以进入睡眠状态并具有不同的按钮。

按钮的功能是:

  • 数字2:ON OFF按钮
  • 数字3:唤醒按钮

一切正常,但是当它进入睡眠状态时,LED也会熄灭。我希望在30秒之后,当Arduino进入睡眠状态时,灯仍然亮着。

这是我的代码:

      #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);
      } 

1 个答案:

答案 0 :(得分:2)

您正在使用AVR的关机睡眠模式。在此模式下,所有定时器都会关闭以节省电量。 没有计时器 - &gt;没有PWM - &gt;没有模拟输出 - &gt;没有PWM驱动的LED

保持LED开启另一种睡眠模式。

请参阅 http://www.atmel.com/images/Atmel-8271-8-bit-AVR-Microcontroller-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet_Complete.pdf了解详情。

但说实话,我不太确定这是否有意义。如果您通过3输出驱动LED,那么通过将MCU置于睡眠状态可以节省的功率可能只有几个百分点。

由于睡眠会使CPU停止运行,因此您的程序将无法在30秒后关闭LED。

为什么不等待30s才入睡?替代方案是一些外部定时电路也会耗电。所以我想在30秒内再增加几毫安仍然是一个更好的选择。