0

I'm a beginner on arduino.

enter image description here

I have an arduino mega with two LED (like the image with only 2 less and without the buzzer). A red LED and a green LED. When the arduino receive the code 200 I want the red LED turn on and the green LED turn off. But when I test that but any led turn off or turn on..

void setup()
{
  pinMode(LR, OUTPUT);
  pinMode(LV, OUTPUT);
  Serial.begin(9600);
}

void readSerial(){
  while(Serial.available() > 0){
    car = Serial.read();

    if(car != '+'){
      buff[i] = car;
      buff[i+1] = '\0';
      i++;
    } 
    else{
      sscanf(buff, "%d", &received_code);
    }
  }
}

void loop()
{     
  readSerial();
  if (received_code == 200){
    LRstate = HIGH;
    digitalWrite(LR, LRstate); // turn the LED on (HIGH is the voltage level)
    digitalWrite(LR,LOW);
    delay(100);
    digitalWrite(LV,LOW); 
  } 
}

How can I solve my problem?

Thanks a lot.

  • 1
    Please rephrase But when I test that but any led turn off or turn on., as this sentence isn't very clear. – Gerben Jan 7 '17 at 14:00
  • Based on the connection and coding problems, I read this as "none of the LEDs responded." – JRobert Jan 7 '17 at 19:04
  • JRobert addresses some of the wiring problems in his answer, but also note that the outer two rows on each side of the breadboard (the rows between red and blue lines) are connected the long way – that is, make up 50-pin bus lines – vs the columns-of-5 on each side of the centerline being connected the short way, making 5-pin bus lines. So the diagram connects a bunch of Arduino outputs together, which is wrong wrong wrong usually – James Waldby - jwpat7 Jan 7 '17 at 22:06
  • If your circuit is really connected as shown in the diagram, then your Arduino should have grilled already, since this diagram shows all outputs pins for LEDs directly connected to GND: short-circuit when an output pin is set to HIGH! – jfpoilpret Jan 8 '17 at 9:19
1

First, make sure the LEDs are connected correctly: I presume the red LED is connected to whichever pin LR is, and the green LED to LV.

According to your diagram, your LEDs will be on if the output is HIGH, and off if the output is LOW. Your code sets LR to high, but then immediately to LOW again.

Here's a fixed (I hope) verson of your loop():

void loop()
{     
  readSerial();
  if (received_code == 200){
    // Turn the red LED on
    digitalWrite(LR,HIGH);

    // Turn the green LED off
    digitalWrite(LV,LOW); 
  } 
}

Unrelated to the problem you're having, but worth a mention anyway:

There's nothing in your code to stop you overrunning buf if you get a longer input than you're expecting. If this happens you'll get unpredictable results as you trash over whatever happens to be in that part of memory.

  • The code you showed turns on LR and turns off LV if received_code == 200, which I suppose meets the specification as given, since how the LEDs get back to LR off and LV on isn't stated. But the question's loop() has a delay(100); in it, suggesting switching them back after that delay – James Waldby - jwpat7 Jan 7 '17 at 18:49
  • @jwpat7 Yes - although I wasn't sure if that was intentional. It's hard to tell which of that group of lines doesn't match the intended behaviour, since it's not absolutely clear what the intended behaviour is. If the OP clarifies what he wants, I'll adjust accordingly. – Mark Smith Jan 7 '17 at 18:55
0

In addition to the code changes noted by @MarkSmith, the LEDs are not connected properly. The straight LED pin on the Fritzing diagram is its cathode and must be connected to grounded, and the bent pin is its anode and must be at +V (+5v, in this case), for the LED to light. To turn the LED on, the anode must be at +V and the cathode at ground. To turn the LED off bring both pins to the same voltage, usually by grounding the anode (logic 0 on its connected pin).

enter image description here

To identify the cathode on a real LED, look inside its plastic blob (that's a technical term :) and you'll see two bits of metal, each on a wire, like two flags. The larger flag is the cathode. Usually, the cathode also has a shorter external lead and the plastic blob has a flat edge on that side, but the larger flag is definitively the cathode:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.