Red LED breaks code

I am a complete newb when it comes to electronics. Today is my second day playing with the Arduino and I ran in to something that I can't explain. I wanted to try some simple stuff before I try to build my first project. Today I added a second LED to a breadboard that already had an LED and a button. My goal was to cycle through the LEDs bringing both off, then one on, then both on, then one off, then both off, etc... with the push of the button. I have blue, white, green, and red LEDs.

This works with all of the LEDs except the red ones. When ever I put the red LED in, it just lights up after the first button push and stays lit up no matter what after that. What could cause this? LEDs seem like simple devices that should either light up if they work or not if they don't. Does anyone know why an LED would stop the code from functioning and stop my other LED from turning on or off?

Thanks!

int ledPin = 13;                // choose the pin for the LED
int ledPin2 = 12;
int inputPin = 2;               // choose the input pin (for a pushbutton)
int val = 0;                    // variable for reading the pin status
int ledval = 0;
int ledval2 = 0;

void setup() {
  pinMode(ledPin2, OUTPUT);      // declare LED as output
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare pushbutton as input
}

void loop(){
  val = digitalRead(inputPin);  // read input value
  ledval2 = digitalRead(ledPin2);  // read input value
  ledval = digitalRead(ledPin);
  
  if (val == HIGH) {            // check if the input is HIGH
    if(ledval == HIGH && ledval2 == LOW)
    {
      digitalWrite(ledPin2, HIGH);  
      delay(200);  
  }
    else  if(ledval == HIGH && ledval2 == HIGH)
    {
        digitalWrite(ledPin, LOW);  
        delay(200);
    }
     else  if(ledval == LOW && ledval2 == HIGH)
    {
        digitalWrite(ledPin2, LOW); 
        delay(200);
    }
    else
    {
       digitalWrite(ledPin, HIGH);  
       delay(200);
    }
    
  }
}

I'm not sure but I think you can't use a digitalRead in an output pin. I'd manually state the status of the ledvals in each "if" cycle.

I agree with Ultra9k - you can't reliable read the value of an output pin. Instead you want to manually set the values when you turn the LEDs on/off. Something like:

digitalWrite(ledPin2, HIGH); ledval2 = HIGH;

Good luck!

I doubt your LED is "breaking" your code. More than likely the red LEDs you have are pulling down the output voltage below a readable on state. Check the voltage at the pin with the different LEDs. A higher value resistor used with the red ones might fix it with regard to the method you are trying. I agree with Kitep and Ultra9k that you could just write the pin state and read the last variable value you set instead of the actual pin state. Another thought, are you using a current limiting resistor in series with each LED?

you can't reliable read the value of an output pin

I respectfully disagree. Although maintaining separate values in variables is a correct solution, there have been many instances in other threads that show you can use digitalWrite(Pin, !digitalRead(Pin)) on an output pin. It seems to be a common way to toggle an output pin.

See, for example:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1244066635/3#3

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1242928992/8#8 ff.

Very true as well. They are I/O pins. Back to the original post though, I think he's trying to figure out the "red ones"

Evil red ones...

How have you connected up the LEDs? Are you using a current limiting resistor or have you connected the LED directly to the I/O pin?

If you have connected the LED directly, it could be that a red LED will generally have a lower forward voltage (~ < 2.0V) than other colours, so that the digitalread() of the input isn’t being read as a “1” because the forward voltage of the LED at the input is too low to be a true TTL “1”…?

Without digging into the digitalRead() function and the ATmega168 datasheet, that could be the problem…

Thank you all very much for the help. Tonight I will try managing the LEDs states using variables and not reads and I will let you know how I make out.

I changed the code to keep track of the state of the LEDs and not Read the state and sure enough the red LEDs behaved as they should. So I think as some of you have stated the Red LEDs were not reading as HIGH even when they were. Glad I got this solved.

Thanks everyone.