【发布时间】:2017-10-26 18:02:33
【问题描述】:
我想用 Arduino Uno 实现一个简单的 LED 控制器,它会进入睡眠状态并具有不同的按钮。
按钮的功能是:
- 数字 2:开关按钮
- 数字 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);
}
【问题讨论】:
-
睡什么?我在代码中没有看到任何关于 sleep 的引用。它还有很多未使用的变量和缺失的函数。创建一个Minimal, Complete, and Verifiable example。
-
感谢您的帖子,这是我的第一篇。下次我会创造一个更好的。我将粘贴其他功能。所有变量都被使用,对不起,我会从现在开始创建更好的代码。我无法粘贴所有代码,因为它受字符限制
-
您不应该发布所有代码。你应该把你的代码减少到minimal reproducible example。删除不属于您所面临问题的任何内容。您不需要 8 个按钮、无数种颜色等等来证明睡眠会关闭所有 LED。 1个led也足够了。删除所有不必要的东西。无论如何,这很可能会帮助您找到错误。
-
别说下次你会做得更好。首先改进这篇文章;)
-
给你。我最小化了代码,现在它只有一个按钮和睡眠。我希望,这很容易找到问题。
标签: arduino arduino-uno arduino-ide led sleep-mode