如何在蓝牙命令后使 LED 亮起 5 秒
How to make a led turn on for 5 seconds after bluetooth command
我想通过 Galileo gen 2 上的蓝牙模块 (hc 05) 控制 LED。我需要以下过程:发送一个字符后,比方说“1”,连接到引脚 2 的 LED 点亮 5秒,然后它关闭并等待另一个命令。我该怎么做?
我试过代码:
(这实际上适用于 Galileo Gen 2,但我的蓝牙工作正常,我在其他示例中通过打开和关闭 LED 进行了检查)
include TimerOne.h
TTYUARTClass* gSerialOnePtr = &Serial1;
char input;
int led = 2;
long offAt = 0;
enum States // set values for enum Mode
{
on,
off
};
States currentState, nextState;
void setup()
{
Timer1.initialize(1000000); // seting interrupt time to 1 sec
Timer1.attachInterrupt(checkBluetooth); // Declaring ISR Function name
gSerialOnePtr->begin(9600); //start serial connection
pinMode(led, OUTPUT);
currentState = off;
}
void loop()
{
switch(currentState) // android app sends letters from A to I each letter turn on different Mode.
{
case on://red color
ledOn();
break;
case off://green color
ledOff();
break;
default:
ledOff();
break;
}
currentState = nextState; //saving next mode in current mode.
}
void checkBluetooth() //ISR for timer1
{
if(gSerialOnePtr->available()) // checking if data arrived from bluetooth module.
{
input = gSerialOnePtr->read(); // save character from serial in bt.
if(input == '1')
{
currentState = on;
}
else if(input == '2')
{
currentState = off;
}
}
}
void ledOn()
{
if( (digitalRead(led) == LOW ) )
{
digitalWrite(led, HIGH);
offAt = millis() + 5000; //store var of now + 5 seconds
}
if(digitalRead(led) == HIGH) //if led is on
{
if(millis() >= offAt) //see if it's time to turn off LED
{
digitalWrite(led, LOW); //it's time. this also re-enables the button
}
}
}
void ledOff()
{
digitalWrite(led, LOW);
}
还没有生效...
我认为你的问题是行
currentState = nextState; //saving next mode in current mode.
试着评论一下。或者,如果你想按照我想的方式使用它,你必须写
void setup()
{
[...]
currentState = off;
nextState = off;
}
[...]
void checkBluetooth() //ISR for timer1
{
if(gSerialOnePtr->available())
{
input = gSerialOnePtr->read(); // save character from serial in bt.
if(input == '1')
{
nextState = on;
}
else if(input == '2')
{
nextState = off;
}
}
}
对了,我也会写
while(gSerialOnePtr->available())
因此,如果您向它发送一个 10 字节的数组,则不必等待 10 秒...就我个人而言,我也会将中断时间减少到比方说 0.1 秒,但这不是强制性的..
编辑:
要在 5 秒后关闭,我会写
不可重触发方法(即,如果您发送“1”时它会被丢弃)
unsigned long startTime;
void setup()
{
[...]
startTime = 0;
}
[...]
void ledOn()
{
digitalWrite(led, HIGH);
if (millis() - startTime > 5000)
nextState = off;
}
void ledOff()
{
digitalWrite(led, LOW);
startTime = millis();
}
可重新触发的方法(即,如果您在 5 秒时发送“1”,将重新开始)
unsigned long startTime;
void setup()
{
[...]
startTime = 0;
}
[...]
void checkBluetooth()
{
[...]
if(input == '1')
{
nextState = on;
startTime = millis();
}
else if(input == '2')
{
[...]
}
void ledOn()
{
digitalWrite(led, HIGH);
if (millis() - startTime > 5000)
nextState = off;
}
void ledOff()
{
digitalWrite(led, LOW);
}
我想通过 Galileo gen 2 上的蓝牙模块 (hc 05) 控制 LED。我需要以下过程:发送一个字符后,比方说“1”,连接到引脚 2 的 LED 点亮 5秒,然后它关闭并等待另一个命令。我该怎么做?
我试过代码: (这实际上适用于 Galileo Gen 2,但我的蓝牙工作正常,我在其他示例中通过打开和关闭 LED 进行了检查)
include TimerOne.h
TTYUARTClass* gSerialOnePtr = &Serial1;
char input;
int led = 2;
long offAt = 0;
enum States // set values for enum Mode
{
on,
off
};
States currentState, nextState;
void setup()
{
Timer1.initialize(1000000); // seting interrupt time to 1 sec
Timer1.attachInterrupt(checkBluetooth); // Declaring ISR Function name
gSerialOnePtr->begin(9600); //start serial connection
pinMode(led, OUTPUT);
currentState = off;
}
void loop()
{
switch(currentState) // android app sends letters from A to I each letter turn on different Mode.
{
case on://red color
ledOn();
break;
case off://green color
ledOff();
break;
default:
ledOff();
break;
}
currentState = nextState; //saving next mode in current mode.
}
void checkBluetooth() //ISR for timer1
{
if(gSerialOnePtr->available()) // checking if data arrived from bluetooth module.
{
input = gSerialOnePtr->read(); // save character from serial in bt.
if(input == '1')
{
currentState = on;
}
else if(input == '2')
{
currentState = off;
}
}
}
void ledOn()
{
if( (digitalRead(led) == LOW ) )
{
digitalWrite(led, HIGH);
offAt = millis() + 5000; //store var of now + 5 seconds
}
if(digitalRead(led) == HIGH) //if led is on
{
if(millis() >= offAt) //see if it's time to turn off LED
{
digitalWrite(led, LOW); //it's time. this also re-enables the button
}
}
}
void ledOff()
{
digitalWrite(led, LOW);
}
还没有生效...
我认为你的问题是行
currentState = nextState; //saving next mode in current mode.
试着评论一下。或者,如果你想按照我想的方式使用它,你必须写
void setup()
{
[...]
currentState = off;
nextState = off;
}
[...]
void checkBluetooth() //ISR for timer1
{
if(gSerialOnePtr->available())
{
input = gSerialOnePtr->read(); // save character from serial in bt.
if(input == '1')
{
nextState = on;
}
else if(input == '2')
{
nextState = off;
}
}
}
对了,我也会写
while(gSerialOnePtr->available())
因此,如果您向它发送一个 10 字节的数组,则不必等待 10 秒...就我个人而言,我也会将中断时间减少到比方说 0.1 秒,但这不是强制性的..
编辑:
要在 5 秒后关闭,我会写
不可重触发方法(即,如果您发送“1”时它会被丢弃)
unsigned long startTime;
void setup()
{
[...]
startTime = 0;
}
[...]
void ledOn()
{
digitalWrite(led, HIGH);
if (millis() - startTime > 5000)
nextState = off;
}
void ledOff()
{
digitalWrite(led, LOW);
startTime = millis();
}
可重新触发的方法(即,如果您在 5 秒时发送“1”,将重新开始)
unsigned long startTime;
void setup()
{
[...]
startTime = 0;
}
[...]
void checkBluetooth()
{
[...]
if(input == '1')
{
nextState = on;
startTime = millis();
}
else if(input == '2')
{
[...]
}
void ledOn()
{
digitalWrite(led, HIGH);
if (millis() - startTime > 5000)
nextState = off;
}
void ledOff()
{
digitalWrite(led, LOW);
}