如何在蓝牙命令后使LED开启5秒钟

时间:2015-04-21 14:26:05

标签: bluetooth arduino led

我想通过蓝牙模块(hc 05)在galileo gen 2上控制LED。我需要以下程序:发送一个字符后,让我们说'1',连接到引脚2的LED亮5秒钟然后它关闭并等待另一个命令。我怎么能这样做?

我试过代码: (这实际上是针对伽利略第二代,但是我的蓝牙工作正常,我在另一个例子中检查了它的开启和关闭)

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

它还没有工作......

1 个答案:

答案 0 :(得分:0)

我认为你的问题就在这一行

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

RETRIGGERABLE METHOD(即如果你在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);
}