Arduino 328, Digital pin 2 HIGH?

Hi,

I want to know why when I initialize the pins 2-9 as INPUTS, the pin #2 does not have anything attached to it and is says that its value is 1. In addition, the Serial.flush() is not working either.
The code is below, why is this happening…?

int number = 0;
byte readpin[2][4] = {0};

// Pin Setup
byte pins[8] = {2,3,4,5,6,7,8,9};
                                
void setup()
{
   Serial.begin(9600);
   
  // Pin Setup
  for (int i=2; i<10; i++)
  {
    pinMode(pins[i], INPUT);
  }
}

void WriteToSegment()
{
  int pin = 2;
  
  for (byte i = 0; i<2; i++)
  {
    for(byte j=0; j<4; j++)
    {
      readpin[i][j] = digitalRead(pin);
      pin++;
    }
  }
  
}

void loop()
{
  WriteToSegment();
  
  int pin = 2;
  for (byte i = 0; i<2; i++)
  {
    for(byte j=0; j<4; j++)
    {
      number = readpin[i][j];
      Serial.print("Value of pin[");
      Serial.print(pin);
      Serial.print("] is ");
      Serial.println(number);
      number = 0;
      pin++;
    }
  }
  delay(2000);
  Serial.flush();
}

(deleted)

I want to know why when I initialize the pins 2-9 as INPUTS, the pin #2 does not have anything attached to it and is says that its value is 1.

Why are you reading from a pin with nothing attached to it? What you have encountered is known as "floating pin". Your Arduino is working properly.

In addition, the Serial.flush() is not working either.

How do you know this? The Serial.flush() function dumps everything in the incoming buffer into the bit bucket. You don't have anything that reads from the serial port, so, how do you know that the incoming buffer is NOT being emptied?

In addition, the Serial.flush() is not working either. How do you know this? The Serial.flush() function dumps everything in the incoming buffer into the bit bucket. You don't have anything that reads from the serial port, so, how do you know that the incoming buffer is NOT being emptied?

I am just trying to see if the algorithm worked, that's why I did not attached anything yet. I used the Serial.flush() because I though that it will be cleaning the Serial Monitor screen.

I used the Serial.flush() because I though that it will be cleaning the Serial Monitor screen.

You need Windex for that.

PaulS:

I used the Serial.flush() because I though that it will be cleaning the Serial Monitor screen.

You need Windex for that.

You are a comedian :D...

for (int i=2; i<10; i++)

The array indexes are 0 to 7, not 2 to 10. You’re never setting pins 2 or 3. You’re also accessing random data after the end of the array.