Strange behaviour from Arduino with I2C

Hi all,
I am working on an Arduino based PID controller. Earlier in the day I had a basic version (thermocouple shield, 16x2 lcd using 4 wires) working, but wanted to add some buttons for a menu where I could change set point as well as P,I & D values. Sadly I had run out of I/O pins on my Duemilanove.
So I changed to a I2C 16x2 lcd, and after much gnashing of teeth managed to get a Hello World out of it.
I then integrated the new lcd's commands into the previous program, while commenting out the previous lcd's commands.
Now I find, after uploading the program, all works well. However when I remove the usb cable and later replace the cable, for some reason I have to upload the program again before the Arduino will run the program.

What gives?

My Arduino had never exhibited this strange behaviour before, so I'm assuming its something to do with the I2C?

I just discovered if I hit the reset buttons a few times when it powers up without doing anything, that seems to fix it. I have never had to do that before, usually it just works everytime I power it up.

There is probably a bug in your sketch. Post it.

Thanks for your reply Skyjumper!

Here is the sketch so far

  Single_Temp.pde - Example using the MAX6675 Library.
  Created by Ryan McLaughlin <>

  This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
 * PID RelayOutput Example
 * Same as basic example, except that this time, the output
 * is going to a digital pin which (we presume) is controlling
 * a relay.  The pid is designed to output an analog value,
 * but the relay can only be On/Off.
 *   To connect them together we use "time proportioning
 * control"  Tt's essentially a really slow version of PWM.
 * First we decide on a window size (5000mS say.) We then 
 * set the pid to adjust its output between 0 and that window
 * size.  Lastly, we add some logic that translates the PID
 * output into "Relay On Time" with the remainder of the 
 * window being "Relay Off Time"

#include <MAX6675.h>
//#include <LiquidCrystal.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <PID_v1.h>
#define RelayPin 6

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,0.5,225,4, DIRECT);

int WindowSize = 5000;
unsigned long windowStartTime;

int LED1 = 9;             // Status LED Pin
int CS = 10;             // CS pin on MAX6675
int SO = 12;              // SO pin of MAX6675
int SCK = 13;             // SCK pin of MAX6675
int units = 1;            // Units to readout temp (0 = raw, 1 = ?C, 2 = ?F)
float temperature = 0.0;  // Temperature output variable

// Initialize the MAX6675 Library for our chip
MAX6675 temp(CS,SO,SCK,units);

// initialize the library with the numbers of the interface pins
//LiquidCrystal lcd(1, 11, 5, 4, 3, 2);

// MAX6675 Library already sets pin modes for MAX6675 chip!
void setup() {
    windowStartTime = millis();
  //initialize the variables we're linked to
  Setpoint = 35;
  //tell the PID to range between 0 and the full window size
  myPID.SetOutputLimits(0, WindowSize);
  //turn the PID on
   // set up the LCD's number of columns and rows: 
  //lcd.begin(16, 2);
  lcd.init();                      // initialize the lcd 
  pinMode(LED1, OUTPUT);

void loop() {
	// Read the temp from the MAX6675
	temperature = (temp.read_temp());

	if(temperature < 0) {                   
		// If there is an error with the TC, temperature will be < 0
                lcd.print("Thermocouple Error on CS");
                lcd.println( temperature ); 
		digitalWrite(LED1, HIGH);
	} else {
                lcd.println( temperature ); 
                lcd.print("  ");
                lcd.println( Setpoint );
                lcd.print("  ");
		digitalWrite(LED1, LOW);
                 Input = temperature;
   * turn the output pin on/off based on pid output
  unsigned long now = millis();
  if(now - windowStartTime>WindowSize)
  { //time to shift the Relay Window
    windowStartTime += WindowSize;
  if(Output > now - windowStartTime) digitalWrite(RelayPin,HIGH);
  else digitalWrite(RelayPin,LOW);

I've been assigning SPI pins CS,MISO,SCK,MOSI as input/output pins with pinMode(), and enabling pullup resistors on CS and MISO in Setup(). With a scope, I noticed these pins can float and cause weirdness. Check it by touching them with your finger and seeing if things change.
I'm not sure what the MAX6675 library does for SPI setup, but it's good for 4.3MHz SPI which seems fast enough.

I have found that SPI.begin takes care of the pinModes for MOSI,MISO SCK, you only need to declare SS.

Same with Wire.begin and SCL/SDA.