My code won't let me connect to ground station (code inside)

Hi all, I am new to messing around with Ardupilot!

I’ve been trying to modify the ArduCopter code so that a certain behavior triggers based on an ADC value. My hardware platform is a PX4, and I have been compiling against the px4-v2 build target. In order to view the current value of the ADC I figured the simplest way to do this was to hijack the sonar signal as described here: http://diydrones.com/forum/topics/custom-sensors-and-real-time-logging

However, when I do so, my pixhawk no longer consistently connects with the ground station via USB. I get errors like “write timed out”, “only received one hearbeat”, and a dropped connection leading to “COM16 is restricted”

I can’t figure out what is causing such a shoddy mavlink connection. I will post my code relevent code as a reply to this post shortly. I would appreciate any advice you guys might have.

EDIT: Modifications are below:
UserCode.cpp

AP_HAL::AnalogSource *adc;

#ifdef USERHOOK_INIT
void Copter::userhook_init()
{
    // put your initialisation code here
    // this will be called once at start-up
    adc = hal.analogin->channel(13);
    ain = 20.0f;
}
#endif

...

#ifdef USERHOOK_SUPERSLOWLOOP
void Copter::userhook_SuperSlowLoop()
{
    // put your 1Hz code here
    ain = adc->voltage_latest();
}
#endif

UserVariables.h

...
float ain;
...

GCS_Mavlink.cpp

//#if CONFIG_SONAR == ENABLED
void NOINLINE Copter::send_rangefinder(mavlink_channel_t chan)
{
    // exit immediately if sonar is disabled
    /*if (!sonar.has_data()) {
        return;
    }
    mavlink_msg_rangefinder_send(
            chan,
            sonar.distance_cm() * 0.01f,
            sonar.voltage_mv() * 0.001f);*/
    //mavlink_msg_rangefinder_send(chan, ain, 14);
    mavlink_msg_rangefinder_send(chan, 13, 14);
}
//#endif

...

 case MSG_RANGEFINDER:
//#if CONFIG_SONAR == ENABLED
        CHECK_PAYLOAD_SIZE(RANGEFINDER);
        copter.send_rangefinder(chan);
//#endif
        break;

Thanks for the help!