Arduino的睡眠模式


from http://www.geek-workshop.com/thread-8715-1-1.html


睡眠模式Energy指令                        中文直译                              消耗电流
IdleIdle()                                            閒置                                      15mA
ADC Noise ReductionSleepADC()   类比数位转换器降低杂讯  6.5mA
Power-savePowerSave()                    省电                                      1.62mA
StandbyStandby()                              待机                                      1.62mA
Extended Standby                              延长待机                              0.84mA
Power-downPowerDown()                断电                                      0.36mA


  1. #include <Enerlib.h>

  2. Energy energy;             // 宣告"Energy"程式物件

  3. const byte swPin = 2;      // 開關腳位
  4. const byte ledPin = 13;    // LED腳位
  5. byte times = 0;            // 記錄執行次數
  6. volatile byte state = 0;   // 暫存執行狀態

  7. void wakeISR() {
  8.    if (energy.WasSleeping()) {
  9.     state = 1;
  10.   } else {
  11.     state = 2;
  12.   }
  13. }

  14. void setup() {
  15.   Serial.begin(9600);

  16.   pinMode(ledPin, OUTPUT);
  17.   pinMode(swPin, INPUT);
  18.   digitalWrite(swPin, HIGH);

  19.   attachInterrupt(0, wakeISR, CHANGE);  // 附加中斷服務常式
  20.   
  21.   Serial.println("Running...");
  22. }

  23. void loop()
  24. {
  25.   if (state == 1) {
  26.     Serial.println("Was sleeping...");
  27.   } else if (state == 2) {
  28.     Serial.println("Was awake...");
  29.   }
  30.   state = 0;
  31.   
  32.   digitalWrite(ledPin, !digitalRead(ledPin));
  33.   delay(500);
  34.   times ++;
  35.   Serial.println(times);

  36.   if (times > 5) {
  37.     times = 0;
  38.     Serial.println("Go to sleep...");
  39.     energy.PowerDown();
  40.   }
  41. }

留言

熱門文章