简体   繁体   中英

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.

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. If I type 'QROUT' - the Proximity will immediately turn off and the Red LED will turn on for 10 seconds and will turn off..

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'..

In my code, the names are the following:

  • Red LED - LOCK
  • Yellow LED - PROX_SENSOR_LED
  • Proximity - PROX

This is my code in 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():

digitalWrite(PROX_SENSOR_LED, LOW);

Also if (ANALOG = HIGH) is a wrong statement and you assign HIGH to your ANALOG. Change it as 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. 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. 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). 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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