Arduino mega trying to read a byte on port L

I am trying to read a byte on port L with the code below. I have set up a simple experiment to read in the grounded pins ( via resistors). I expected to see all zero's but instead get a random display of 0's or 1's, not quite what I expected. I also do not necessarily see a whole 8 bits displayed, sometime 1 bit sometimes 6 bits. I haven't used the Arduino before, and I am trying to use it to get round an obsolescence problem that has suddenly occurred at work. Ultimately the data would be strobed in, processed then transmitted out down an rs232 line. I have read the ports/pins bit in the references but i have probably misunderstood something quite fundamental but I don't know what it is.
Any help gratefully received.

Byte portL_Read;

void setup() {

Serial.begin(9600);
DDRL = B00000000;
PORTL = B00000000;
}

void loop() {

portL_Read = PINL;
Serial.print("Data ");
Serial.println(portL_Read, BIN);
delay(1000);
}

Tommy2110:
I expected to see all zero's but instead get a random display of 0's or 1's, not quite what I expected.

What value of resistor are you using between the input pins and ground?
Are you sure you have the PORTL pins? 42 (MSB) through 49 (LSB)?

Tommy2110:
I also do not necessarily see a whole 8 bits displayed, sometime 1 bit sometimes 6 bits.

Serial.print() will suppress leading zeroes.

Tommy2110:
I have set up a simple experiment to read in the grounded pins ( via resistors). I expected to see all zero's but instead get a random display of 0's or 1's

Given that you have grounded some pins via resistors, what do you have connected to the non-grounded pins? If the answer is 'nothing' then you have floating inputs which could return arbitrary values. You could avoid that by enabling the internal pull-up resistors, or by adding external pull-up of pull-down resistors.

We have an R2 board where port L pins map to 23-30 on the interface connector (pin 30 lsb). I used some 10k resistors that I had handy from a Arduino dev kit. I grounded the input pins of this port via the resistors.

Tommy2110:
We have an R2 board where port L pins map to 23-30 on the interface connector (pin 30 lsb). I used some 10k resistors that I had handy from a Arduino dev kit. I grounded the input pins of this port via the resistors.

Are you sure of your pin numbers? The mapping in 1.0.5 shows PORTL as pins D49 through D42. Check your pins_arduino.h file.

/Applications/Arduino 1.0.5.app/Contents/Resources/Java/hardware/arduino/variants/mega/pins_arduino.h

Try moving your pull-down resistors to 42-49 to see if you get zeroes.

Thanks to all, yes I was on the wrong pins. I have now moved to the correct pins and changed the data type to char instead of byte. Now I understand what you meant by the print function suppressing leading zero's. I tried triggering an external interrupt within which I could capture the data, and use serial.write to re-transmit the data to the serial port. It would seem that using a serial write statement doesn't work within the interrupt mechanism. I am guessing that there is some sort of conflict with the internal code of the Arduino. Does anyone know if my assumption is correct?

During an interrupt service routine (ISR) all interrupts are disabled. The serial output needs interrupts to know when it is time to send out the next character so Serial output doesn't work inside an ISR. Serial I/O also takes much more time than is advisable in an ISR.

I changed my interrupt routine to dump the data in a buffer and increment a pointer. The outside loop then just monitored for new data and then serially transmitted what it found. The program works well, I am pleased with it. It still requires a bit of polishing I am not an expert C programmer but it does the job.

Many thanks for the replies !!!

Tommy