简体   繁体   English

arduino按下按钮C#应用程序时LED闪烁

[英]arduino flashing LED on pressed button c# application

I connected arduino with c# application by bluetooth connecton, On digital pins 2 and 4 are connected two LEDs When i press a button on c# application via bluetooth the command comes to arudino and "digital write" (LED HIGH) is executed and it glows without stopping. 我通过蓝牙connecton将arduino与c#应用程序相连,在数字引脚2和4上连接了两个LED当我通过蓝牙在c#应用程序上按一个按钮时,命令进入arudino并执行“数字写入”(LED高电平)并且它发光而没有停止。 My question is how to improve arduino code so that the LED light glows only when the button is pressed, or when we stop pressing the button with mouse it stops glowing. 我的问题是如何改善arduino代码,以使LED灯仅在按下按钮时才发光,或者当我们停止用鼠标按下按钮时它才停止发光。

#include <SoftwareSerial.h> 
int LED=2;
int LED=4;

void setup()
{

    pinMode(LED1, OUTPUT);
    pinMode(LED2, OUTPUT);
    Serial.begin(9600);
} 
void loop()
{

  if (Serial.available() > 0)
  {
  int command = Serial.read();
 {
   if(command == 'LED1_ON')
   {
     digitalWrite(LED1, HIGH);
   }
     else  if(command == 'LED2_ON')
   {
    digitalWrite(LED2, HIGH);
   }
  }
 }
}

sorry my bad English. 对不起,我英语不好。 I try to help you ;) First i suggest to avoid declaring your variables in loop() function. 我试图为您提供帮助;)首先,我建议避免在loop()函数中声明变量。

 int command = Serial.read();

It is good habit to declare this at the beginning of your source code (where is your leds declared). 在源代码的开头(声明了led的位置)处声明它是一个好习惯。 But the question. 但是问题。 I guess, your serial link is aviable, when you press the button isnt it? 我想,您的串行链接是可用的,当您按下按钮时不是吗? So than you can turn off your leds when it is not available: 因此,当不可用时,您可以关闭其指示灯:

 if (Serial.available() > 0)
 { ....}
 else { /* turn off youre leds */ }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM