Cannot get loop to recognize pin in state

EDIT:
Problem solved the arduino board was bad, I swapped it out with a different one and it fixed the problem.

I am extremely new to programming.

The problem I have is that I have a working 8 bit binary counter to led display however when I add in the code to monitor the state of digital pin 13 to set the outputs to low instead of counting it doesn't, it just ignores the pin and starts counting
int ledPin[] = {2, 3, 4, 5, 6, 7, 8, 9}; //define digital output pins
int val = 0; //define digital input state of high or low

void setup()
{
pinMode (13, INPUT_PULLUP); // set digital pin 13 to pullup high
// setup output pins for display
for (int i = 0; i < 8; i++)
{
pinMode(ledPin[i], OUTPUT);
}
}

void loop()
{
val = digitalRead(13); // read the state of pin 13
if (val == HIGH)
{
for (int i = 0; i < 8; i++)
{
digitalWrite(ledPin[i], LOW); // set digital outs to low
}
}
else
{
for (byte counter = 0; counter <= 256; counter++)
{
displayBinary(counter);
delay(100);
}
}
}

void displayBinary(byte numToShow)
{
for (int i =0;i<8;i++)
{
if (bitRead(numToShow, i)==1)
{
digitalWrite(ledPin[i], HIGH);
}
else
{
digitalWrite(ledPin[i], LOW);
}
}
}

A byte value can never be equal to 256

Please remember to use code tags when posting code.

What is connected to pin 13? Is there a pull down resistor on pin 13? If not the pin can be "floating" and its state is indeterminate. The easy way to fix it is to set the pinMode to INPUT_PULLUP so that the pin is in a known state (HIGH) when not connected to anything. Then the pin will read HIGH when unconnected and LOW when connected to ground. Adjust your code accordingly.

I have the code set to INPUT_PULLUP for digital pin 13 and I have verified that the state changes when I pull it low if I take the binary counter code out of the program and just set it to turn an led on

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.