Sketch compiles to Mega1280 but not to Mega2560

Hi!
I can't find information anywhere on this.
This sketch compiles ok if i choose target Mega1280.
Now i have a 2560 that i would like to use but if i select Mega2560 as target, the compiler throws an error:

/usr/lib/gcc/avr/4.7.0/../../../avr/lib/avr6/crtm2560.o: In function __bad_interrupt': ../../../../crt1/gcrt1.S:195: relocation truncated to fit: R_AVR_13_PCREL against symbol __vector_45' defined in .text section in /usr/lib/gcc/avr/4.7.0/../../../avr/lib/avr6/crtm2560.o
collect2: error: ld returned 1 exit status

Another !funny effect is that if i comment out certain lines (344, 350 or 367), it compiles ok.
This minimized sketch really doesnt do a lot besides reporting free RAM, reading a DS1820 temp sensor, accessing a SD card and clears the TFT screen.
It has the UTFT and UTouch libs loaded.

Anyone experienced user who has a clue of what's going on?

Sketch below:

#include <UTFT.h>
#include <UTouch.h>
#include <stdlib.h>
#include <OneWire.h>
#include <MemoryFree.h>
#include <SD.h>

File myFile;

#include <DallasTemperature.h>


// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(9);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);


// Declare which fonts we will be using
extern uint8_t BigFont[];

// Uncomment the next two lines for the Arduino 2009/UNO
//UTFT        myGLCD(ITDB24D,19,18,17,16);   // Remember to change the model parameter to suit your display module!
//UTouch      myTouch(15,10,14,9,8);

// Uncomment the next two lines for the Arduino Mega
UTFT        myGLCD(ITDB32S, 38,39,40,41);   // Remember to change the model parameter to suit your display module!
UTouch      myTouch(6,5,4,3,2);



int x, y, z, zz;

char stCurrent[20]="";
int stCurrentLen=0;
char stLast[20]="";
char SdFnCA[7],SdDataCA[99];

float TempC;//celsius, fahrenheit;

int MaxSettings=1; //--------------------------------

String sSett[9],sSettIni[9];
String spc20="                    ";
int SettItem=0;
float fSett[9];

long zt;

String S,SdData, SdFn;
String sAppName,sAppVer,sAppNameUC;









void SdInit()
{

  
  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
   pinMode(53, OUTPUT);
   
  if (!SD.begin(53)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

}




void SdSave()
{

  SdData.toCharArray(SdDataCA, 99);
  SdFn.toCharArray(SdFnCA, 15);
  
  
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  SD.remove(SdFnCA);
  myFile = SD.open(SdFnCA, FILE_WRITE);
  
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to ");
    Serial.println(SdFnCA);
    Serial.print("Data:");
    Serial.println(SdDataCA);

    myFile.println(SdDataCA);
	// close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  
}



void SdLoad()
{
char CZ;
int i=0;
  //SdData.toCharArray(SdDataCA, 99);
  SdFn.toCharArray(SdFnCA, 15);
  
  // re-open the file for reading:
  myFile = SD.open(SdFnCA);
  if (myFile) {
    Serial.print("Reading from ");
    Serial.println(SdFnCA);
    Serial.print("Data:");
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
        CZ=myFile.read();
        SdDataCA[i]=CZ;
        i++;
        
    	Serial.write(CZ);
    }
    // close the file:
    myFile.close();
    SdDataCA[i]=0;
    Serial.print("Done. Data=");
    Serial.println(SdDataCA);
    SdData=SdDataCA;
    Serial.println(SdData);
  } else {
  	// if the file didn't open, print an error:
    Serial.print("");
    Serial.print("*** Error opening ");
    Serial.println(SdFnCA);    
  }
}







void ScrClr()
{myGLCD.clrScr(); myGLCD.setFont(BigFont);}



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



void ChkFree()
{

  Serial.println("");  
  Serial.print("freeRAM=");
  Serial.print(freeMemory()); 
  Serial.print(" ("); 
  Serial.print(freeRam());  
  Serial.println(") B");
  
}


String FixRoundErr(String S)
{
float zf;
String SF;

zf=StrToFloat(S);
S=FloatToStr(zf,0,5);
SF=RemoveZeros(S);
return SF;
 
  
}  


String RemoveZeros(String S)
{
  
  int z,zz;
  char ZC; 
  zz=S.length()-1;
  
  Serial.println("");
  Serial.println("S="+S);
  
  for (z=zz; z>0; z--) 
    {  
      ZC=S.charAt(z);
      
   
      if (ZC!=48) 
            {
              if (ZC==46) {S=S.substring(0,z);}
              break;
            }
      S=S.substring(0,z);
    }
      
      //Serial.println(" S final="+S);
      
      return S;
}









float StrToFloat(String str)
{
      char carray[str.length() + 1]; //determine size of the array
      str.toCharArray(carray, sizeof(carray)); //put str into an array
      return atof(carray);
}

String FloatToStr(float f, int l, int d )
{
      String S;
      char zCA[20];
      dtostrf(f,l,d,zCA);
      //zCA+=0;
      S=zCA;
      
      return S;
}






void ShowTemp()
{ 
  // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
 
  
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures

  Serial.println("DONE");  
  TempC=sensors.getTempCByIndex(0);  
  Serial.print("Temperature for the device 1 (index 0) is: ");
  Serial.println(TempC);  
  
  //TempC=sensors.getTempCByIndex(0);  
  S=FloatToStr(TempC,4,1);
  myGLCD.print(S+"'C",LEFT,224);
  
  
 
}



void SaveThis()
{
  fSett[z]=StrToFloat(stCurrent);
  SdFn=sSettIni[z];
  SdData=stCurrent;
  SdSave();   
  
}


void LoadIniFiles()
{
 for (z=0; z<MaxSettings; z++) {LoadThis(); }
  
}

void LoadThis()
{
   SdFn=sSettIni[z];
   SdLoad();
   S=FixRoundErr(SdData);
   fSett[z]=StrToFloat(S);
 
}





void loadInit()
{
  
 sAppVer="0.1";
 sAppName="TESTTEST";
 sAppNameUC=sAppName;
 sAppNameUC.toUpperCase(); 
 
 
}




void setup()
{
 
  myGLCD.InitLCD(); 
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);
  loadInit();
  
  Serial.begin(9600);  
  
  sensors.begin();
  
  
  
  
 
    
    ShowTemp();        // Compiles ok if i comment this line...
  
    ChkFree(); 
    
    SdInit();
      
    LoadIniFiles();    // ...or if i comment this line
    
    ChkFree();
    
}





void loop()
{
  
  while (true)
  { 
    
 
    ScrClr();   // also compiles if i comment this line
      

  }
}

Could you please edit this and put your code in [ code ] [/ code ] tags

Ah, that looks better. Maybe I should post the libraries as well??

Ah, that looks better. Maybe I should post the libraries as well??

No, but links to them.

Have you tried to make a minimal sketch, which also shows your problem?

/usr/lib/gcc/avr/4.7.0/../../../avr/lib/avr6/crtm2560.o: In function __bad_interrupt': ../../../../crt1/gcrt1.S:195: relocation truncated to fit: R_AVR_13_PCREL against symbol __vector_45' defined in .text section in /usr/lib/gcc/avr/4.7.0/../../../avr/lib/avr6/crtm2560.o
collect2: error: ld returned 1 exit status

Is that really the whole output you get?

void loop()
{

while (true)
{

ScrClr(); // also compiles if i comment this line

}
}

An infinite loop within an infinite loop.

Added: It appears that the problems lie in those 3 functions. Instead of commenting out the entire call to the function, go through each and just comment out one line at a time to see what the actual problem is.

Ok, i thought i had minimized it already from 70k, but it could be made smaller.
The infinite loop inside infinite loop removed.
Still the same error:

/usr/lib/gcc/avr/4.7.0/../../../avr/lib/avr6/crtm2560.o: In function __bad_interrupt': ../../../../crt1/gcrt1.S:195: relocation truncated to fit: R_AVR_13_PCREL against symbol __vector_41' defined in .text section in /usr/lib/gcc/avr/4.7.0/../../../avr/lib/avr6/crtm2560.o
collect2: error: ld returned 1 exit status

Modified code:

#include <UTFT.h>
#include <UTouch.h>
#include <stdlib.h>
#include <OneWire.h>
#include <MemoryFree.h>
#include <SD.h>
#include <DallasTemperature.h>


OneWire oneWire(9);

DallasTemperature sensors(&oneWire);


// Declare which fonts we will be using
extern uint8_t BigFont[];

// Uncomment the next two lines for the Arduino 2009/UNO
//UTFT        myGLCD(ITDB24D,19,18,17,16);   // Remember to change the model parameter to suit your display module!
//UTouch      myTouch(15,10,14,9,8);

// Uncomment the next two lines for the Arduino Mega
UTFT        myGLCD(ITDB32S, 38,39,40,41);   // Remember to change the model parameter to suit your display module!
UTouch      myTouch(6,5,4,3,2);





float TempC;

String sSett[9],sSettIni[9];

String S;

float fSett[9];







void ScrClr()
{myGLCD.clrScr(); myGLCD.setFont(BigFont);}





float StrToFloat(String str)
{
      char carray[str.length() + 1]; //determine size of the array
      str.toCharArray(carray, sizeof(carray)); //put str into an array
      return atof(carray);
}



String FloatToStr(float f, int l, int d )
{
      String S;
      char zCA[20];
      dtostrf(f,l,d,zCA);
      //zCA+=0;
      S=zCA;
      
      return S;
}








void setup()
{
 
  
  myGLCD.InitLCD(); 
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);
 
  
  Serial.begin(9600);  
  
  sensors.begin();        // Compiles ok if commented
    
  TempC=23.4;  

  S=FloatToStr(TempC,4,1);    // Compiles ok if commented
  
  myGLCD.print(S+"'C",LEFT,224);
    
  S="1.23";
  fSett[1]=StrToFloat(S);  // Compiles ok if commented
    
}





void loop()
{
  
    
 
    ScrClr();   // also compiles if i comment this line
      

 
}

The strange thing is that it works fine with the 1280 target.

Libraries:

UTFT and UTouch:
http://www.henningkarlsen.com/electronics/library.php?id=51

Dallas:
http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library

The other libs are bundled with the Arduino IDE.

Ok, one othe thing about the whole code in general, can you get rid of all the white space?

Actual code:
You have to go through all your functions, and inspect and comment out one by one to see what the problem is.
Example:

String FloatToStr(float f, int l, int d )
{
String S; //this is already declaired at the top of the code, why declair it again?

edit: Maybe those libraries are only meant to be used with a 1280 Mega.

A linux box I bet, and using the repository version of the IDE.

/usr/lib/gcc/avr/4.7.0/../../../avr/lib/avr6/crtm2560.o: In function `__bad_interrupt':

Have you tried the Arduino website version? It has all that stuff (avr-gcc and avr-libc) included. You can have both on your computer together. Download the correct OS software from here:

Unpack it in a local directory. Navigate to that directory and run the arduino shell script there. See if that does any better.

I use Ubuntu v11.10 and v12.04 with the Linux 32 bit version IDE. Works great on both, except for the contrast on the menu with v12.04. :~

The Ubuntu repository IDE had problems with both versions of Ubuntu.

I'm having a very similar problem.

Error message:

d:/arduino-1.0.3-windows/arduino-1.0.3/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/lib/avr6/crtm2560.o: In function __vector_default': (.vectors+0x64): relocation truncated to fit: R_AVR_13_PCREL against symbol __vector_25' defined in .text.__vector_25 section in core.a(HardwareSerial.cpp.o)

will compile: (Older Sketch)

  if (cm[0] == 0 && cm[1] == 0 && cm[2] > 0) goLeft();       //Left nothing, front nothing, right something - go left
      if (cm[2] == 0 && cm[1] == 0 && cm[0] > 0) goRight();      //Right nothing, front nothing, left something - go right

Won't compile: (Newer Sketch that is causing problem)

    if (cm[0] == 0 && cm[1] == 0 && cm[2] > 0) 
        {
              if (cm[2] > 50) goStraight();
                else goLeft();      
        }        
      if (cm[2] == 0 && cm[1] == 0 && cm[0] > 0) 
        {
              if (cm[0] > 50) goStraight();
                 else goRight();
        }

SurferTim is right!
Ubuntu 12.10 with rep. arduino 1.0.1

So i tried to install 1.0.4 like you said and now i get:

In file included from Ok1280Err2560_11.ino:8:
/home/skurt/sketchbook/libraries/dallasTemperatureControl/DallasTemperature.h:64: error: expected `)' before ‘’ token
/home/skurt/sketchbook/libraries/dallasTemperatureControl/DallasTemperature.h:222: error: ISO C++ forbids declaration of ‘OneWire’ with no type
/home/skurt/sketchbook/libraries/dallasTemperatureControl/DallasTemperature.h:222: error: expected ‘;’ before ‘
’ token
Ok1280Err2560_11:11: error: ‘OneWire’ does not name a type
Ok1280Err2560_11:13: error: ‘oneWire’ was not declared in this scope

Do i need something more than just the arduino downloads?
I downloaded the arduino-1.0.4-linux32.tgz and extracted it to a "arduino-1.0.4"-folder and ran the "arduino" file.
Sorry for being a noob! :slight_smile:

Now you have a problem with something in this file:
/home/skurt/sketchbook/libraries/dallasTemperatureControl/DallasTemperature.h
Can you post that file? Maybe I can see what the problem is.

Where have you installed your libraries? I have mine in ~/sketchbook/libraries/ and your sketch compiles without problems (for any board selected).

@SurferTim, the file is attached.

The path is:
/home/skurt/sketchbook/libraries/dallasTemperatureControl/DallasTemperature.h

My user is called "skurt". Should the sketchbook folder be directly under "home"?

DallasTemperature.h (6.64 KB)

xxxx:
@SurferTim, the file is attached.

The path is:
/home/skurt/sketchbook/libraries/dallasTemperatureControl/DallasTemperature.h

My user is called "skurt". Should the sketchbook folder be directly under "home"?

No but for the IDE to find the library properly the specific library folder name used must match the .h and .cpp file names used, so:

/home/skurt/sketchbook/libraries/DallasTemperature/DallasTemperature.h

Lefty

Fixed what retrolefty said.

Result:

In file included from Ok1280Err2560_11.ino:8:
/home/skurt/sketchbook/libraries/DallasTemperature/DallasTemperature.h:64: error: expected `)' before ‘’ token
/home/skurt/sketchbook/libraries/DallasTemperature/DallasTemperature.h:222: error: ISO C++ forbids declaration of ‘OneWire’ with no type
/home/skurt/sketchbook/libraries/DallasTemperature/DallasTemperature.h:222: error: expected ‘;’ before ‘
’ token
Ok1280Err2560_11:11: error: ‘OneWire’ does not name a type
Ok1280Err2560_11:13: error: ‘oneWire’ was not declared in this scope

Try adding "#include <OneWire.h>" to your sketch. It appears you library may no longer find "OneWire.h". Do you know where that is?

http://playground.arduino.cc/Learning/OneWire

@SurferTim
Should i add it again?? This is my code:

#include <UTFT.h>
#include <UTouch.h>
#include <stdlib.h>
#include <OneWire.h>
#include <MemoryFree.h>
#include <SD.h>
#include <DallasTemperature.h>


OneWire oneWire(9);

DallasTemperature sensors(&oneWire);


// Declare which fonts we will be using
extern uint8_t BigFont[];

// Uncomment the next two lines for the Arduino 2009/UNO
//UTFT        myGLCD(ITDB24D,19,18,17,16);   // Remember to change the model parameter to suit your display module!
//UTouch      myTouch(15,10,14,9,8);

// Uncomment the next two lines for the Arduino Mega
UTFT        myGLCD(ITDB32S, 38,39,40,41);   // Remember to change the model parameter to suit your display module!
UTouch      myTouch(6,5,4,3,2);





float TempC;

String sSett[9],sSettIni[9];

String S;

float fSett[9];







void ScrClr()
{myGLCD.clrScr(); myGLCD.setFont(BigFont);}





float StrToFloat(String str)
{
      char carray[str.length() + 1]; //determine size of the array
      str.toCharArray(carray, sizeof(carray)); //put str into an array
      return atof(carray);
}



String FloatToStr(float f, int l, int d )
{
      String S;
      char zCA[20];
      dtostrf(f,l,d,zCA);
      //zCA+=0;
      S=zCA;
      
      return S;
}








void setup()
{
 
  
  myGLCD.InitLCD(); 
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);
 
  
  Serial.begin(9600);  
  
  sensors.begin();        // Compiles ok if commented
    
  TempC=23.4;  

  S=FloatToStr(TempC,4,1);    // Compiles ok if commented
  
  myGLCD.print(S+"'C",LEFT,224);
    
  S="1.23";
  fSett[1]=StrToFloat(S);  // Compiles ok if commented
    
}





void loop()
{
  
    
 
    ScrClr();   // also compiles if i comment this line
      

 
}

Your code does not know where the library is located.

I think HazardsMind may be right. Here are the two lines causing your problem.

// Line 64
  DallasTemperature(OneWire*);

// and line 222
  OneWire* _wire;

Both refer to OneWire, and it does not seem to know where that library is.