Basic Arduino Count with LEDs

Hello,

Jumping back in to Arduino stuff after a few years in another hobby.

I have simple count loop with LEDs turning on with the corresponding count value. I'm not sure why count value 0 does not turn on the LED? It's not a wiring issue or bad LED.

Is it because 0 is not considered a valid value?

Should I begin to count at 1 if 0 is not valid?

Thank you in advance.

const int ledPin0 = 7;
  const int ledPin1 = 8;
  const int ledPin2 = 9;
  const int ledPin3 = 10;
  const int ledPin4 = 11;
  int count=0;
  

void setup() {
  
  Serial.begin(9600);
  pinMode(ledPin0,OUTPUT);
  pinMode(ledPin1,OUTPUT);
  pinMode(ledPin2,OUTPUT);
  pinMode(ledPin3,OUTPUT);
  pinMode(ledPin4,OUTPUT);
  
}

// the loop function runs over and over again forever
void loop() {
  
Serial.print("Count Value: ");
 Serial.println(count++);
 delay(400);
 
 if (count  == 0) {
 digitalWrite(ledPin0, HIGH);}
 else {digitalWrite(ledPin0, LOW);
}
if (count  == 1) {
 digitalWrite(ledPin1, HIGH);}
 else {digitalWrite(ledPin1, LOW);
}
 if (count  == 2) {
 digitalWrite(ledPin2, HIGH);}
 else {digitalWrite(ledPin2, LOW);
}
if (count  == 3) {
 digitalWrite(ledPin3, HIGH);}
 else {digitalWrite(ledPin3, LOW);
}
if (count  == 4) {
 digitalWrite(ledPin4, HIGH);}
 else {digitalWrite(ledPin4, LOW);
}
 
 if (count == 5) {count =0;}
  
}
 if (count  == 0) {
 digitalWrite(ledPin0, HIGH);}
 else {digitalWrite(ledPin0, LOW);

count has been incremented to 1 whilst printing it before this test is executed

You are incrementing count before you ever get to your if statements:

void loop() {

  Serial.print("Count Value: ");
  Serial.println(count++);
  delay(400);

  if (count  == 0) {
    digitalWrite(ledPin0, HIGH);
  }
  else {
    digitalWrite(ledPin0, LOW);
  }

I see what you are saying, but the other LEDs correctly illuminate when the count equals their corresponding values. It is just count 0 and ledPin0 that do not illuminate However, in the serial monitor I see the 0 as it cycles.

try this:

digitalWrite(ledPin0, count==0);

OK, thank you everyone. I combined all your feedback and fixed my problem. Also started the count at 1 instead of 0.

PS I'm not a programmer :pensive: I'm a hardware guy!

const int ledPin0 = 7;
  const int ledPin1 = 8;
  const int ledPin2 = 9;
  const int ledPin3 = 10;
  const int ledPin4 = 11;
  int count=1;
  

void setup() {
  
  Serial.begin(9600);
  pinMode(ledPin0,OUTPUT);
  pinMode(ledPin1,OUTPUT);
  pinMode(ledPin2,OUTPUT);
  pinMode(ledPin3,OUTPUT);
  pinMode(ledPin4,OUTPUT);
  
}

// the loop function runs over and over again forever
void loop() {

   digitalWrite(ledPin0, count==1);
   digitalWrite(ledPin1, count==2);
   digitalWrite(ledPin2, count==3);
   digitalWrite(ledPin3, count==4);
   digitalWrite(ledPin4, count==5);
/* if (count  == 1) {
 digitalWrite(ledPin0, HIGH);}
 else {digitalWrite(ledPin0, LOW);
} 

if (count  == 2) {
 digitalWrite(ledPin1, HIGH);}
 else {digitalWrite(ledPin1, LOW);
}
 if (count  == 3) {
 digitalWrite(ledPin2, HIGH);}
 else {digitalWrite(ledPin2, LOW);
}
if (count  == 4) {
 digitalWrite(ledPin3, HIGH);}
 else {digitalWrite(ledPin3, LOW);
}
if (count  == 5) {
 digitalWrite(ledPin4, HIGH);}
 else {digitalWrite(ledPin4, LOW);
}*/
  
Serial.print("Count Value: ");
 Serial.println(count++);
 delay(1000);



 
 if (count == 6) {count =1;}
  
}

This

is equivalent to

So you are seeing the value 0 printed, but it is afterwards incremented to 1. So when you get to the first check

it is never 0.

Try this instead:

void loop() {
  
Serial.print("Count Value: ");
 Serial.println(count); // do not increment here
 delay(400);
 
 if (count  == 0) {
 digitalWrite(ledPin0, HIGH);}
 else {digitalWrite(ledPin0, LOW);
}
if (count  == 1) {
 digitalWrite(ledPin1, HIGH);}
 else {digitalWrite(ledPin1, LOW);
}
 if (count  == 2) {
 digitalWrite(ledPin2, HIGH);}
 else {digitalWrite(ledPin2, LOW);
}
if (count  == 3) {
 digitalWrite(ledPin3, HIGH);}
 else {digitalWrite(ledPin3, LOW);
}
if (count  == 4) {
 digitalWrite(ledPin4, HIGH);}
 else {digitalWrite(ledPin4, LOW);
}

  // increment here, just before testing
 count++;
 if (count == 5) {count =0;}
  
}