Help with TTL pulse collection

Hello all!

I am trying to collect data from TTL pulses sent out from a rodent behavioral apparatus through an Arduino Uno and ultimately to Bonsai software to time lock these pulses to another stream of data.

I expect that my TTL pulse data should look like values close to zero, and then close to 5V when there is a behavioral event. In my current configuration, with 4 TTL outputs to first four analog pins and one ground wire from the behavioral apparatus to the analog ground and one to the digital ground, we are collecting data that fluctuates between 0 and 4890 consistently, not related to behavioral events. We tried adding an additional ground wire between the arduino (other analog ground) and the behavioral apparatus, and are now getting data that fluctuates between ~10 - ~3800, that still doesn't make sense.

The code we are using to collect the pulses is copied below (copied from another group). Can anyone tell me if I am applying a weird transformation to the data that would cause me to get these odd numbers? Or any advice on the grounding?

#include <Firmata.h>

byte analogPin; //define a variable that will be sent to the computer

void analogWriteCallback(byte pin, int value) //compose a message containing info from pin
{
pinMode(pin, OUTPUT);
analogWrite(pin, value);
}

void setup() //run this once, prepare arduino for communicating w computer
{
Firmata.setFirmwareVersion(FIRMATA_MAJOR_VERSION, FIRMATA_MINOR_VERSION);
Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
Firmata.begin();
}

void loop() //run this as long as arduino has power
{
int channels = 4; //set at 1-6 based on the number of pins you will be using
while (Firmata.available()) {
Firmata.processInput();
}
for (analogPin = 0; analogPin < channels; analogPin ++){
Firmata.sendAnalog(analogPin, (analogRead(analogPin)*(5000/1024))); //send the value read at pin A0 to computer
//delay(1); //set sampling rate
}

}

It sounds like a hardware problem. Please post a complete wiring diagram. A pen and paper drawing is perfectly acceptable.

Pictures attached below.