user43736: I connected three LEDs to an Arduino Uno and wrote a simple LED class. I toggle all the leds the main loop, but for some reason one led (connected to pin7) always misbehaves, skips one toggle or stays on all the time. Initially all leds are off (in begin() function). What could cause this problem?
main.cpp
#include "led.h"
#include <Arduino.h>

Led leds[] = {
    Led(2),
    Led(3),
    Led(7)
};

void setup()
{
    Serial.begin(9600);
    for (Led& l : leds) l.begin();
}

void loop()
{
    for (Led &l : leds) l.toggle();
    delay(500);
}

Led.h
class Led {
public:
    Led(int pin);
    void begin();
    void toggle();

private:
    const int _pin; 
};

Led.cpp
#include "led.h"
#include "Arduino.h"

Led::Led(int pin) : _pin(pin) 
{}

void Led::begin()
{
    pinMode(_pin, OUTPUT);
    digitalWrite(_pin, LOW);
}

void Led::toggle()
{
    digitalWrite(_pin, !digitalRead(_pin));
}


user43736: It seems to be a hardware fault, as others have suggested:
I connected the LED to another pin(8) and it works perfectly, everytime.
Looks like something got fried internally on that specific pin

43736: <p>I connected three LEDs to an Arduino Uno and wrote a simple LED class. I toggle all the leds the main loop, but for some reason <strong>one led (connected to pin7) always misbehaves</strong>, skips one toggle or stays on all the time. Initially all leds are off (in begin() function). What could cause this problem?</p>
<p><strong>main.cpp</strong></p>
<pre><code>#include &quot;led.h&quot;
#include &lt;Arduino.h&gt;

Led leds[] = {
    Led(2),
    Led(3),
    Led(7)
};

void setup()
{
    Serial.begin(9600);
    for (Led&amp; l : leds) l.begin();
}

void loop()
{
    for (Led &amp;l : leds) l.toggle();
    delay(500);
}
</code></pre>
<p><strong>Led.h</strong></p>
<pre><code>class Led {
public:
    Led(int pin);
    void begin();
    void toggle();

private:
    const int _pin; 
};
</code></pre>
<p><strong>Led.cpp</strong></p>
<pre><code>#include &quot;led.h&quot;
#include &quot;Arduino.h&quot;

Led::Led(int pin) : _pin(pin) 
{}

void Led::begin()
{
    pinMode(_pin, OUTPUT);
    digitalWrite(_pin, LOW);
}

void Led::toggle()
{
    digitalWrite(_pin, !digitalRead(_pin));
}
</code></pre>
 43736: <p>It seems to be a hardware fault, as others have suggested:
I connected the LED to another pin(8) and it works perfectly, everytime.
Looks like something got fried internally on that specific pin</p>
