Error with analogRead, interference?

Hey everyone, I borrowed some code from the forums to get a TMP37 temp sensor working on my mega ADK. Here is a sketch where it works 100% perfectly.

#define aref_voltage 5
int tempPin = A5;

void setup(void) {
  Serial.begin(115200);
}

void loop(void) {
  
  int mv = analogRead(tempPin) * ((aref_voltage * 1000) / 1024);
  int tempC = mv / 20;
  int tempF = (tempC / 5 * 9) + 32;

  Serial.print(tempC);
  Serial.println(" C");
  Serial.print(tempF);
  Serial.println(" F");
  Serial.println("");
  delay(1000);
}

Now I wanted to include this in my larger program, reading in variables from a satellite chip. So I copied the code EXACTLY, and tried to use it in my other program, but no matter what, it reads in 1023 as the analog value. I tried to hard code the tempPin variable as A5, or 5 and I get the same messed up output.

#include <Wire.h>
#include <Adafruit_GPS.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SoftwareSerial.h>
//   Connect the GPS TX (transmit) pin to Digital 3
//   Connect the GPS RX (receive) pin to Digital 2
//SoftwareSerial mySerial(3, 2);
Adafruit_GPS GPS(&Serial2);

//DEFINE LCD PINS and CONST
#define OLED_DC 11
#define OLED_CS 12
#define OLED_CLK 10
#define OLED_MOSI 9
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

#define aref_voltage 5
int tempPin = 5;
// this keeps track of whether we're using the interrupt
// off by default!
boolean usingInterrupt = false;
void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy


byte speed = 0;
byte NESW = 0;
byte hour = 0;
byte minute = 0;
byte timeDiff = 5;

void setup()  
{
  
  // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
  // also spit it out
  Serial.begin(115200);
  // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
  GPS.begin(9600);
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   // 1 Hz update rate
 
  useInterrupt(true);
  delay(1000);
  
  analogReference(EXTERNAL);
  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC);
  // init done
  display.clearDisplay();   // clears the screen and buffer
  display.setTextColor(WHITE, BLACK); 
}


// Interrupt is called once a millisecond, looks for any new GPS data, and stores it
SIGNAL(TIMER0_COMPA_vect) {
  char c = GPS.read();
  // if you want to debug, this is a good time to do it!
#ifdef UDR0
    if (c) UDR0 = c;
      
    // writing direct to UDR0 is much much faster than Serial.print 
    // but only one character can be written at a time. 
#endif
}

void useInterrupt(boolean v) {
  if (v) {
    // Timer0 is already used for millis() - we'll just interrupt somewhere
    // in the middle and call the "Compare A" function above
    OCR0A = 0xAF;
    TIMSK0 |= _BV(OCIE0A);
    usingInterrupt = true;
  } else {
    // do not call the interrupt function COMPA anymore
    TIMSK0 &= ~_BV(OCIE0A);
    usingInterrupt = false;
  }
}

uint32_t timer = millis();
void loop()                     // run over and over again
{

  
 
  // if a sentence is received, we can check the checksum, parse it...
  if (GPS.newNMEAreceived()) {
    // a tricky thing here is if we print the NMEA sentence, or data
    // we end up not listening and catching other sentences! 
    // so be very wary if using OUTPUT_ALLDATA and trytng to print out data
    //Serial.println(GPS.lastNMEA());   // this also sets the newNMEAreceived() flag to false
  
    if (!GPS.parse(GPS.lastNMEA()))   // this also sets the newNMEAreceived() flag to false
      return;  // we can fail to parse a sentence in which case we should just wait for another
  }

  // if millis() or timer wraps around, we'll just reset it
  if (timer > millis())  timer = millis();

  // approximately every 2 seconds or so, print out the current stats
  if (millis() - timer > 2000) { 
    timer = millis(); // reset the timer
    Serial.print(F("\nfreeMemory()="));
    Serial.println(freeRam());    
    Serial.println(("\n"));
    /*
    Serial.print(GPS.hour, DEC); Serial.print(F(":"));Serial.println(GPS.minute, DEC);
    Serial.print(F("Fix: ")); Serial.println((int)GPS.fix);
    Serial.print(F("Qual: ")); Serial.println((int)GPS.fixquality);
    */
    

    int mv = analogRead(tempPin) * ((aref_voltage * 1000) / 1024);
    int tempC = mv / 20;
    int tempF = (tempC / 5 * 9) + 32;
    Serial.print(tempF);
    Serial.println(" F");
    
    hour = GPS.hour;
    minute = GPS.minute;
    speed = (GPS.speed * 1.15);
    UpdateTime(hour, minute);  
    UpdateTemp(tempF);
    UpdateSpeed(speed);
    UpdateDirection(NESW); 
    
    // print the display
    display.display();
    
    if (GPS.fix) {   
      Serial.print(F("Speed: ")); Serial.println(GPS.speed);
      Serial.print(F("Angle: ")); Serial.println(GPS.angle);
      Serial.print(F("Alt: ")); Serial.println(GPS.altitude);
      Serial.print(F("Sats: ")); Serial.println((int)GPS.satellites);
      
    }
  }
}

int freeRam () 
{
  extern int __heap_start, *__brkval; 
  int v; 
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 
}


void UpdateTime(byte hour, byte minute){
  // correct the time zone
  byte i = timeDiff;
  while (i > 0){
    
    if (hour > 1){
      hour--;
    }
    else{
      hour=12;
    }
      
      i--;
  }
  
  
  // display the time
  display.setTextSize(2);
  if (hour < 10)
    display.setCursor(80,0);
  else
    display.setCursor(68,0);
    
  display.print(hour);
  display.print(":");
  if(minute < 10)
    display.print("0");
  display.print(minute);
     
}

void UpdateTemp(int Temp){
    // display the temperature
  display.setCursor(0,0);
  display.setTextSize(2);
  display.print(Temp);display.print("F  ");
  
}
  
void UpdateSpeed(int Speed){
  // display speed
  if(speed > 99)
  display.setCursor(28,17);
  else if(speed > 9){
  display.setCursor(42,17);
  display.fillRect(28, 17, 14, 28,BLACK);
  }
  else{
  display.setCursor(54,17);
  display.fillRect(28, 17, 26, 28,BLACK);
  }
  display.setTextSize(4);
  display.print(speed);display.print(" ");
  
  
  // write MPH below the speed
  display.setCursor(55,47);
  display.setTextSize(1);
  display.print("MPH");
} 

void UpdateDirection(byte NESW){
  // write direction (NESW)
  display.setCursor(0,50);
  display.setTextSize(2);
      switch (NESW) {
    case 1:
      display.print("N");
      break;
    case 2:
      display.print("E");
      break;
    case 3:
      display.print("S");
      break;
    case 4:
      display.print("W");
      break;
    }
}

No expert here, but I think this is your problem:

magruder13: analogReference(EXTERNAL); // by default, we'll generate the high voltage from the 3.3v line internally! (neat!) display.begin(SSD1306_SWITCHCAPVCC);