Arduino Mega Upload issues

I am in the process of developing an Automated Hydroponic System, I was able to develop all programs on my Uno but when I compile all the data and put it onto my Mega it freezes up at about 90% upload. I cannot figure out what I am doing wrong and have been stumped for the past few days. I even bought a new Mega to make sure that the board is not the issue. I know that my program seems a little messy but if someone could Please Help, it would be greatly appreciated. Thank you in advance.

//**********************************************************

//Water Temperature Information
#include <OneWire.h>

#include <DallasTemperature.h>

OneWire oneWire(A4); //Pin A4 47-Ohm Resistor

DallasTemperature sensors(&oneWire);

//**********************************************************
//Pin Information
int greenLed = 2; //Water Temp Below 80
int redLed = 3; //Water Temp Above 80

const int pump1 = 5; //Main NFT Pump
const int pump2 = 6; //Secondary NFT Pump
int pump3 = 7; //Drip “Dutch Bucket” Pump

#define DHT11PIN A1 //DHT11 Pin A1
const int waterFlow = A2; //Main Pump Flow Detector
const int waterLevel = A3;
#define SensorPin1 0 //pH meter Analog output to Arduino Analog Input 0
unsigned long int avgValue; //Store the average value of the sensor feedback
float b;
int buf[10],temp;

int buttonState = 0;
int buttonState2 = 0;

//**********************************************************

// DHT11 Calculation & Pin information

//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
}

#include <dht11.h>

dht11 DHT11;

//**********************************************************

void setup ()
{

sensors.begin();
Serial.begin(9600);

//Pin Callouts
pinMode(greenLed, OUTPUT); //Water Temp Below 80
pinMode(redLed, OUTPUT); //Water Temp Above 80
pinMode(waterFlow, INPUT); //Main Pump Flow Detector
pinMode(pump1, OUTPUT); //Main NFT Pump
pinMode(pump2, OUTPUT); //Secondary NFT Pump
pinMode(pump3, OUTPUT); //Drip “Dutch Bucket” Pump

pinMode(1,OUTPUT); //PH Output

//**********************************************************

//Turn on Main NFT Pump “pump1”
digitalWrite(pump1, HIGH);

//**********************************************************

}

void loop()
{

Serial.print("******************************************************");
Serial.print("\n");

//**********************************************************

//PH Testing

for(int i=0;i<10;i++) //Get 10 sample value from the sensor for smooth the value
{
buf*=analogRead(SensorPin1);*

  • delay(10);*
  • }*
  • for(int i=0;i<9;i++) //sort the analog from small to large*
  • {*
  • for(int j=i+1;j<10;j++)*
  • {*
    _ if(buf*>buf[j])_
    _
    {_
    _ temp=buf;
    buf=buf[j];
    buf[j]=temp;
    }
    }
    }
    avgValue=0;
    for(int i=2;i<8;i++) //take the average value of 6 center sample*

    avgValue+=buf*;
    float phValue=(float)avgValue5.0/1024/6; //convert the analog into millivolt_

    _ phValue=3.5phValue; //convert the millivolt into pH value_
    _ Serial.print(“pH:”);
    Serial.print(phValue,2);
    Serial.println(" “);
    /digitalWrite(13, HIGH);
    delay(800);

    * digitalWrite(13, LOW);
    /

    if(phValue < 5.5){
    * Serial.print(“pH LOW!!!”);
    //Serial.print(”\n");
    Serial.println();}
    if(phValue > 6.5){
    Serial.print(“pH HIGH!!!”);
    //Serial.print("\t");
    Serial.println();}
    //
    //Flow Control Switch

    buttonState = digitalRead(waterFlow);
    //

    //Water Level Control

    buttonState2 = digitalRead(waterLevel);
    //*********************************************************_

//DHT11 Print Files
* //Serial.println("\n");*
* int chk = DHT11.read(DHT11PIN);*
/*
* Serial.print("Read sensor: ");*
* switch (chk)*
* {*
* case 0: Serial.println(“OK”); break;*
* case -1: Serial.println(“Checksum error”); break;*
* case -2: Serial.println(“Time out error”); break;*
* default: Serial.println(“Unknown error”); break;*
* }*
*/
* Serial.print("Air Temp : ");*
* Serial.print(Fahrenheit(DHT11.temperature), 1);*
* Serial.println(“F”);*
delay(10);
* Serial.print("Humidity : ");*
* Serial.print((float)DHT11.humidity, 1);*
* Serial.println("%");*
//Water Temperature Alarms
if(Fahrenheit(DHT11.temperature) < 40){
* Serial.print(“FROST ADVISORY!!!”);*
* Serial.print("\t");*
* Serial.println();}*
//**********************************************************
//Water Temperature Print Files
sensors.requestTemperatures();
float currentTemp0;
currentTemp0 = sensors.getTempFByIndex(0);
Serial.print("Water Temp : ");
//Serial.print("\t");
Serial.print(currentTemp0,1);
Serial.println(“F”);
//Water Temperature Alarms
if(currentTemp0 > 80){
* Serial.print(“WATER To HOT!!!”);*
* Serial.print("\t");*
* Serial.println();}*
// Serial.println();

//Turn on Green Led if Temperature Less than 80
* if(currentTemp0 < 79)*
* digitalWrite(greenLed, HIGH);*
* if(currentTemp0 < 79)*
* digitalWrite(redLed, LOW);*

*//Turn on Red Led if Temperature is Greater than 80 *
* if(currentTemp0 > 80) *
* digitalWrite(redLed, HIGH);*
* if(currentTemp0 > 80) *
* digitalWrite(greenLed, LOW);*
//**********************************************************
//Pump Control
*//If waterFlow is Detected & leave pump1 on and leave pump2 off *
* if (buttonState == HIGH) { *
* // turn Pump1 on*
* digitalWrite(pump1, HIGH);}*
* else {*
* // turn Pump1 off:*
* digitalWrite(pump1, LOW);}*
*//If waterFlow is NOT Detected turn pump1 off and turn pump2 on *
* if (buttonState == LOW) { *
* // turn Pump1 on*
* digitalWrite(pump2, HIGH);}*
* else {*
* // turn Pump1 off:*
* digitalWrite(pump2, LOW);} *
* if(buttonState == LOW){*
* Serial.print(“Pump 1 Failure”);*
// Serial.print("\n");
* Serial.println();}*
//************************************************************

//Water Level
*//If water level in tank low is Detected Fill Tank *
/* if (buttonState2 == HIGH) {
* // turn Pump1 on*

* digitalWrite(pump1, HIGH);}*
* else {*
* // turn Pump1 off:*
* digitalWrite(pump1, LOW);}*
*//If waterFlow is NOT Detected turn pump1 off and turn pump2 on *
* if (buttonState == LOW) { *
* // turn Pump1 on*
* digitalWrite(pump2, HIGH);}*
* else {*
* // turn Pump1 off:*
* digitalWrite(pump2, LOW);} *
*/
* if(buttonState2 == HIGH){*
* Serial.print(“Tank Level Low”);*
// Serial.print("\n");
* Serial.println();}*
//************************************************************

//Setuence Monitoring only needs to be done ever 15 minutes
//delay(90000);
delay(1000); //Testing Purposes Monitoring
digitalWrite(pump3, LOW); //Drip Pump
//delay(1000); //Remove once in service.
//digitalWrite(pump3,LOW);
}
/*Credits:
* DojoDave*
* Tom Igoe*
* Jeremy Blum*

*/ [/color]

Serial.begin(9600);
 
 //Pin Callouts
 pinMode(greenLed, OUTPUT); //Water Temp Below 80
 pinMode(redLed, OUTPUT); //Water Temp Above 80
 pinMode(waterFlow, INPUT); //Main Pump Flow Detector
 pinMode(pump1, OUTPUT); //Main NFT Pump
 pinMode(pump2, OUTPUT); //Secondary NFT Pump
 pinMode(pump3, OUTPUT); //Drip "Dutch Bucket" Pump
 
 pinMode(1,OUTPUT); //PH Output

You can not use pin 1 for PH output (whatever that means) and Serial at the same time. Whatever you have connected to pin 1 is interfering with the upload.

I still cannot figure out what I have done wrong.

Well, for one thing, you've posted code incorrectly twice now. Use the icon with the # BEFORE pasting the code.

Do you, or do you not, have something connected to digital pin 1? If so, you can't use Serial in the sketch AND having the thing connected can interfere with uploading code. If not, why are you, in the first sketch, diddling with pin 1?

I thought this would be fixed by now. Do not use 3 exclamation marks ("!!!") in a row in your code. It puts the bootloader into a debug mode.

  // change this
  Serial.print("pH LOW!!!");
  // to this
  Serial.print("pH LOW!");

Pin 1 is open, I have now put my PH on pin 13, as seen in the first code below, the second code below is the one that I am trying to get working correctly, sorry about posting incorrectly, I have read many of these forums but this is the first time I have posted. I have also removed the !!! and tried to re-run the program without success.

/*
# This sample codes is for testing the pH meter V1.0.
 # Editor : YouYou
 # Date   : 2013.10.12
 # Ver    : 0.1
 # Product: pH meter
 # SKU    : SEN0161
*/

#define SensorPin 0          //pH meter Analog output to Arduino Analog Input 0
unsigned long int avgValue;  //Store the average value of the sensor feedback
float b;
int buf[10],temp;

void setup()
{
  pinMode(13,OUTPUT);  
  Serial.begin(9600);  
  Serial.println("Ready");    //Test the serial monitor
}
void loop()
{
  for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
  { 
    buf[i]=analogRead(SensorPin);
    delay(10);
  }
  for(int i=0;i<9;i++)        //sort the analog from small to large
  {
    for(int j=i+1;j<10;j++)
    {
      if(buf[i]>buf[j])
      {
        temp=buf[i];
        buf[i]=buf[j];
        buf[j]=temp;
      }
    }
  }
  avgValue=0;
  for(int i=2;i<8;i++)                      //take the average value of 6 center sample
    avgValue+=buf[i];
  float phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt
  phValue=3.5*phValue;                      //convert the millivolt into pH value
  Serial.print("    pH:");  
  Serial.print(phValue,2);
  Serial.println(" ");
  digitalWrite(13, HIGH);       
  delay(800);
  digitalWrite(13, LOW); 
}
//**********************************************************

//Water Temperature Information
#include <OneWire.h>

#include <DallasTemperature.h>

OneWire oneWire(A4); //Pin A4 47-Ohm Resistor

DallasTemperature sensors(&oneWire);

//**********************************************************
//Pin Information
int greenLed = 2; //Water Temp Below 80
int redLed = 3; //Water Temp Above 80

const int pump1 = 5; //Main NFT Pump
const int pump2 = 6; //Secondary NFT Pump
int pump3 = 7; //Drip "Dutch Bucket" Pump

#define DHT11PIN A1  //DHT11 Pin A1
const int waterFlow = A2; //Main Pump Flow Detector
const int waterLevel = A3;

#define SensorPin 0          //pH meter Analog output to Arduino Analog Input 0
unsigned long int avgValue;  //Store the average value of the sensor feedback
float b;
int buf[10],temp;

int buttonState = 0;
int buttonState2 = 0;

//**********************************************************

// DHT11 Calculation & Pin information

//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
	return 1.8 * celsius + 32;
}

#include <dht11.h>

dht11 DHT11;



//**********************************************************

void setup ()
{

 //Pin Callouts
 pinMode(greenLed, OUTPUT); //Water Temp Below 80
 pinMode(redLed, OUTPUT); //Water Temp Above 80
 pinMode(waterFlow, INPUT); //Main Pump Flow Detector
 pinMode(pump1, OUTPUT); //Main NFT Pump
 pinMode(pump2, OUTPUT); //Secondary NFT Pump
 pinMode(pump3, OUTPUT); //Drip "Dutch Bucket" Pump
 
  pinMode(13,OUTPUT);  
  Serial.begin(9600);  
  Serial.println("Ready");    //Test the serial monitor
 
//********************************************************** 

 //Turn on Main NFT Pump "pump1"
  digitalWrite(pump1, HIGH);
  
//**********************************************************  

 }

void loop()
{  
  
    Serial.print("******************************************************");
    Serial.print("\n");  
  
//**********************************************************

//PH Testing

  for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
  { 
    buf[i]=analogRead(SensorPin);
    delay(10);
  }
  for(int i=0;i<9;i++)        //sort the analog from small to large
  {
    for(int j=i+1;j<10;j++)
    {
      if(buf[i]>buf[j])
      {
        temp=buf[i];
        buf[i]=buf[j];
        buf[j]=temp;
      }
    }
  }
  avgValue=0;
  for(int i=2;i<8;i++)                      //take the average value of 6 center sample
    avgValue+=buf[i];
  float phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt
  phValue=3.5*phValue;                      //convert the millivolt into pH value
  Serial.print("    pH:");  
  Serial.print(phValue,2);
  Serial.println(" ");
  digitalWrite(13, HIGH);       
  delay(800);
  digitalWrite(13, LOW); 

 if(phValue < 5.5){
   Serial.print("pH LOW!");
   //Serial.print("\n");
   Serial.println();}

 if(phValue > 6.5){
   Serial.print("pH HIGH!");
   //Serial.print("\t");
   Serial.println();}

//**********************************************************

//Flow Control Switch
buttonState = digitalRead(waterFlow);

//**********************************************************

//Water Level Control
buttonState2 = digitalRead(waterLevel);

//**********************************************************
  
//DHT11 Print Files

  //Serial.println("\n");

  int chk = DHT11.read(DHT11PIN);

/*
  Serial.print("Read sensor: ");
  switch (chk)
  {
    case 0: Serial.println("OK"); break;
    case -1: Serial.println("Checksum error"); break;
    case -2: Serial.println("Time out error"); break;
    default: Serial.println("Unknown error"); break;
  }
*/

  Serial.print("Air Temp : ");
  Serial.print(Fahrenheit(DHT11.temperature), 1);
  Serial.println("F");

delay(10);

  Serial.print("Humidity : ");
  Serial.print((float)DHT11.humidity, 1);
  Serial.println("%");

//Water Temperature Alarms
 if(Fahrenheit(DHT11.temperature) < 40){
   Serial.print("FROST ADVISORY!");
   Serial.print("\t");
   Serial.println();}

//**********************************************************

//Water Temperature Print Files
sensors.requestTemperatures();
float currentTemp0;
currentTemp0 = sensors.getTempFByIndex(0);

Serial.print("Water Temp : ");
//Serial.print("\t");
Serial.print(currentTemp0,1);
Serial.println("F");

//Water Temperature Alarms
 if(currentTemp0 > 80){
   Serial.print("WATER To HOT!");
   Serial.print("\t");
   Serial.println();}

 //  Serial.println();
  
//Turn on Green Led if Temperature Less than 80 
  if(currentTemp0 < 79) 
   digitalWrite(greenLed, HIGH);
   if(currentTemp0 < 79)
   digitalWrite(redLed, LOW);
  
//Turn on Red Led if Temperature is Greater than 80  
  if(currentTemp0 > 80)  
   digitalWrite(redLed, HIGH);
  if(currentTemp0 > 80)  
   digitalWrite(greenLed, LOW);

//**********************************************************

//Pump Control
//If waterFlow is Detected & leave pump1 on and leave pump2 off   
  if (buttonState == HIGH) {     
    // turn Pump1 on 
    digitalWrite(pump1, HIGH);} 
  else {
    // turn Pump1 off:
    digitalWrite(pump1, LOW);}

//If waterFlow is NOT Detected turn pump1 off and turn pump2 on  
  if (buttonState == LOW) {     
    // turn Pump1 on 
    digitalWrite(pump2, HIGH);} 
  else {
    // turn Pump1 off:
    digitalWrite(pump2, LOW);}    

   if(buttonState == LOW){
    Serial.print("Pump 1 Failure");
//    Serial.print("\n");
    Serial.println();}

//************************************************************
 
//Water Level
//If water level in tank low is Detected Fill Tank   
/*  if (buttonState2 == HIGH) {     
    // turn Pump1 on 
    digitalWrite(pump1, HIGH);} 
  else {
    // turn Pump1 off:
    digitalWrite(pump1, LOW);}

//If waterFlow is NOT Detected turn pump1 off and turn pump2 on  
  if (buttonState == LOW) {     
    // turn Pump1 on 
    digitalWrite(pump2, HIGH);} 
  else {
    // turn Pump1 off:
    digitalWrite(pump2, LOW);}    
*/
   if(buttonState2 == HIGH){
    Serial.print("Tank Level Low");
//    Serial.print("\n");
    Serial.println();}

//************************************************************ 
 


//Setuence Monitoring only needs to be done ever 15 minutes
//delay(90000);
delay(1000); //Testing Purposes Monitoring
digitalWrite(pump3, LOW); //Drip Pump
//delay(1000); //Remove once in service.
//digitalWrite(pump3,LOW);






}


/*Credits: 
    DojoDave
    Tom Igoe
    Jeremy Blum
    
*/

Is it still failing on the upload?

  for(int i=0;i<9;i++)        //sort the analog from small to large
  {
    for(int j=i+1;j<10;j++)
    {
      if(buf[i]>buf[j])
      {
        temp=buf[i];
        buf[i]=buf[j];
        buf[j]=temp;
      }
    }
  }

That’s a pretty inefficient sort. The outer statement should be a while statement that monitors a flag that indicates whether or not any swaps took place. Your code will take just as long to sort an array that is in decreasing order as it will to sort one that is in increasing order (one that is already sorted).

When presented with a array that is already sorted, only one pass should be needed to discover that.

#define DHT11PIN A1  //DHT11 Pin A1
#define SensorPin 0          //pH meter Analog output to Arduino Analog Input 0

Adjacent pins - one referred to as 0 and one referred to as A1. Why?

//Turn on Green Led if Temperature Less than 80 
  if(currentTemp0 < 79) 
   digitalWrite(greenLed, HIGH);
   if(currentTemp0 < 79)
   digitalWrite(redLed, LOW);

The code does not do what the comment says. If you are going to have useless comments, you must make them match the code (or vice versa).

Using curly braces would allow you to get rid of one if statement.

Yes it was, and during the last upload to send you a screen shot it finally took. I have re-sent it 3 times now and it finally took. It must have been your !!! comment that fixed it.

Thank you very much for all of your assistance and patients with my beginner programing abilities.

SurferTim: I thought this would be fixed by now. Do not use 3 exclamation marks ("!!!") in a row in your code. It puts the bootloader into a debug mode.

  // change this
  Serial.print("pH LOW!!!");
  // to this
  Serial.print("pH LOW!");

It's a function of what bootloader was installed by the original manufacture of the board. So while the newer (fixed) bootloader for the 2560 is distrubutived with the IDE that doesn't mean that it made it into any user's specific mega2560 board. I suspect that many boards shipping today might still contain the older mega2560 bootloader, depending on source of purchase, shelf time of the board, etc.