简体   繁体   English

如何设置LED从一开始就关闭(Arduino)

[英]How to set the LED to turn off from the start (Arduino)

so am... I 'am new to Arduino and I'm currently trying to make this work.. but I'm already doing this for an hour with luck not in my side... Here is the summary of what I'm doing: I have a Gizduino + 644 (a copy of Arduino with ATmega 644 here in Phil.), a IR Proximity Sensor (3 PIN - VCC, GRND, OUT), 2 LED's (Red and Yellow) and 2 100ohms Resistor. 所以......我是Arduino的新手,我现在正努力做到这一点......但是我已经这样做了一个小时而运气不在我的身边......以下是我的总结我正在做:我有一个Gizduino + 644(Arduino的副本,其中有ATmega 644在这里),一个红外接近传感器(3个PIN - VCC,GRND,OUT),2个LED(红色和黄色)和2个100ohms电阻。

So far, this is what I can do: 到目前为止,这是我能做的:

  • In Arduino IDE, I have a case in which if I type 'QRIN' - the Proximity and the Red LED will turn on... if the proximity sense something within its range.. the Yellow LED will turn ON. 在Arduino IDE中,我有一个案例,如果我键入'QRIN' - 接近和红色LED将打开...如果接近感测到其范围内的东西..黄色LED将打开。 If I type 'QROUT' - the Proximity will immediately turn off and the Red LED will turn on for 10 seconds and will turn off.. 如果我输入'QROUT' - 接近将立即关闭,红色LED将开启10秒钟并将关闭..

And this is the problem: 这就是问题所在:

  • The Yellow LED always turns ON if it's the first run(I mean I just click the upload button in IDE).... and that's a very big problem... it will only turn off if I type the cases: 'QRIN' and 'QROUT'.. 如果是第一次运行,黄色LED总是打开(我的意思是我只需点击IDE中的上传按钮)....这是一个非常大的问题......如果我输入案例,它只会关闭:'QRIN'和'QROUT'..

In my code, the names are the following: 在我的代码中,名称如下:

  • Red LED - LOCK 红色LED - 锁定
  • Yellow LED - PROX_SENSOR_LED 黄色LED - PROX_SENSOR_LED
  • Proximity - PROX 邻近 - PROX

This is my code in IDE: 这是我在IDE中的代码:

int LOCK = 13; //RED LED, in pin 13
int PROX = 12; //PROXIMITY, in pin 12
int ANALOG = 0; //OUT of Proximity,  in Analog 0
int PROX_SENSOR_LED = 7; //Yellow LED, in pin 7
int val = 0;  //value to store

void setup()
{
  Serial.begin(9600);
  pinMode(LOCK, OUTPUT); //set the pin # as output (VCC of the hardware)
  pinMode(PROX, OUTPUT); //set the pin # as output (VCC of the hardware)
  pinMode(PROX_SENSOR_LED, OUTPUT); //set the pin # as output (VCC of the hardware)
}

void loop()
{
  digitalWrite(PROX_SENSOR_LED, LOW); //sets the output pin initially to LOW (but doesnt work.. T_T)
  val = analogRead(ANALOG);  //read the input pin 0 to 1023
  if (val > 800)    //if the sensor value is higher threshold set OUTPUT HIGH
  {
    digitalWrite(PROX_SENSOR_LED, HIGH);  //sets output pin HIGH
    delay(100);    //waits for .1 second
  }

  char data = Serial.read(); //read 9600

  switch (data) //start of case... like 'ON' 'OFF'
  {
    case 'QRIN': //this is my 'ON'
                digitalWrite(PROX, HIGH); //turn the proximity to ON
                digitalWrite(LOCK, HIGH); //turn the lock to ON
                break;
    case 'QROUT': //this is my off 'OFF'
                digitalWrite(PROX, LOW); //turn the proximity to OFF
                digitalWrite(LOCK, HIGH); //turn the lock to ON
                delay(10000);              //for 10 seconds
                digitalWrite(LOCK, LOW);   //then off
                if (ANALOG = HIGH)  // I need this 'if' condition because if
                {    //this is not here... the Yellow LED is turn ON... 
                  digitalWrite(PROX_SENSOR_LED, LOW); //I don't know why.. T_T
                }
                break;
  }
}

Put the line at the end of the setup(): 将行放在setup()的末尾:

digitalWrite(PROX_SENSOR_LED, LOW);

Also if (ANALOG = HIGH) is a wrong statement and you assign HIGH to your ANALOG. 此外, if (ANALOG = HIGH)是一个错误的语句,并为您的ANALOG分配HIGH。 Change it as if (ANALOG == HIGH) . 将其更改为if (ANALOG == HIGH)

All GPIO will start-up in the high impedance input state so that the control to the LED is floating - the state of the LED in that case will depend on the LED drive circuit and whether it has a pull-up or pull-down resistor. 所有GPIO都将在高阻抗输入状态下启动,以便对LED的控制是浮动的 - 在这种情况下LED的状态将取决于LED驱动电路以及它是否具有上拉或下拉电阻。 That is to say it is a hardware problem not a software problem. 也就是说这是硬件问题而不是软件问题。

During upload the Arduino bootloader is running and it does not initialise any I/O that is not needed for the upload process. 上载期间,Arduino引导加载程序正在运行,它不会初始化上载过程不需要的任何I / O. If you cannot fix the hardware design so that it floats to the off state, then you would need to modify the bootloader in order to turn the LED off at the earliest opportunity - (there will still be a glitch that may or may not be visible). 如果您无法修复硬件设计以使其浮动到关闭状态,那么您需要修改引导加载程序以便尽早关闭LED - (仍然会出现可能会或可能不会显示的故障)。 That is probably a bad idea, because then you have an application specific bootloader rather than a generic one, and for other applications setting this I/O pin may be entirely undesirable. 这可能是一个坏主意,因为那时你有一个特定于应用程序的引导加载程序而不是通用引导加载程序,而对于其他应用程序来说,设置这个I / O引脚可能是完全不合需要的。

That said, it is not entirely clear why it is a problem that the LED is on during upload, and simply initialising the output in the setup() would seem acceptable in most cases. 也就是说,目前还不完全清楚为什么LED在上传过程中出现了问题,在大多数情况下,简单地初始化setup()的输出似乎是可以接受的。

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

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