ATMega32 digitalRead and write trouble

Try turning on the internal pullup resistors:

for (int i=0; i<31; i++)
pinMode(i,INPUT);
digitalWrite(i, HIGH); // << add this

Move this After the pinModes so the Tx pin is not declared an Input after you turn on the Serial library/function:

Serial.begin(9600);