I am relatively new to arduino and I have the Mega 2560 board. However, I recently encountered this problem:
"Error compiling for board Arduino/Genuino Mega or Mega 2560"
I made sure in Tools that it is the correct board, however, the problem still appears. I also tried to reinstall the IDE, but it did not help.
Nothing would fix it. Please help me troubleshoot this problem. Thanks.
That hints to some error(s) in your secret sketch.
I will not guess a solution. Good luck.
I'm going to ask you to post some additional information that might help us to identify the problem.
Please do this:
- When you encounter an error, you'll see a button on the right side of the orange bar in the Arduino IDE: Copy error messages (or the icon that looks like two pieces of paper at the top right corner of the black console window in the Arduino Web Editor). Click that button.
- Open a forum reply here by clicking the Reply button.
- Click the
</>icon on the post composer toolbar. This will add the forum's code block markup (```) to your reply to make sure the error messages are correctly formatted.
- Press Ctrl+V. This will paste the compilation output into the code block.
- Move the cursor outside of the code block markup before you add any additional text to your reply.
- Click the Reply button to post the output.
Arduino: 1.8.10 (Mac OS X), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"
/var/folders/76/2830y9d144l2_vcf2spgwhk40000gp/T//cc16kk0f.ltrans0.ltrans.o: In function global constructors keyed to 65535_0_SR04_Example.ino.cpp.o.1738': <artificial>:(.text.startup+0x64): undefined reference to SR04::SR04(int, int)'
/var/folders/76/2830y9d144l2_vcf2spgwhk40000gp/T//cc16kk0f.ltrans0.ltrans.o: In function main': <artificial>:(.text.startup+0x1d6): undefined reference to SR04::Distance()'
collect2: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino/Genuino Mega or Mega 2560.
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Hey Whandall, what is my secret sketch?
Who knows?
Something artificial with a missing reference to some distance sensor?
Is there a possible solution to this error?
Do you not think the forum would need to see the code, before suggesting a solution ?
This is the code, for the Ultrasonic Sensor:
#include "SR04.h"
#define TRIG_PIN 12
#define ECHO_PIN 11
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;
void setup() {
Serial.begin(9600);
delay(1000);
}
void loop() {
a=sr04.Distance();
Serial.print(a);
Serial.println("cm");
delay(1000);
}
The most common cause for this type of error is people trying to fix a "No such file or directory" error by hunting down on the internet and then installing only a.h file matching the filename mentioned in the error. That usually won't work because you need to install the full library, which includes source files where the definitions are in addition to the header file that provides the declaration.
So now it's time for you to tell us about this SR04.h file.
@philip_l_mega2560, your topic has been moved to a more suitable location on the forum.
I downloaded SR04.h from arduino libraries. This is the actual code:
#ifndef SR04_H
#define SR04_H
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
//#include "pins_arduino.h"
#include <inttypes.h>
#define PULSE_TIMEOUT 150000L // 100ms
#define DEFAULT_DELAY 10
#define DEFAULT_PINGS 5
class SR04 {
public:
/**
- Constructor
- Ultrasonic sensor SR04, four connections pins
- VCC, ECHO, TRIGGER, GND
-
- \param echoPin digital INPUT-Pin for measuring distance
- \param triggerPin if 10us high a trigger signal is generated from
-
SR04 - \return void
*/
SR04(int echoPin, int triggerPin);
/**
- Do a measurment for this sensor. Return distance as long
- in centimenter
- \return long distance in centimeter
*/
long Distance();
/**
- Do count measurents and calculate the average.
- To avoid defilement from ow/high peaks, min/max values
- are substracted from the average
- \param wait delay between measurements, default = DEFAULT_DELAY/ms
- \param count number of measurements, default DEFAULT_PINGS
- \return long distance in centimeter
**/
long DistanceAvg(int wait=DEFAULT_DELAY, int count=DEFAULT_PINGS);
/**
- Do only a ping. Get result with methode getDistance()
- \param keine
*/
void Ping() ;
/**
- return latest distance. Methode Ping() should be called before
- \param keine
- \return Distanz in Zentimeter
*/
long getDistance();
private:
/**
- Do the measurement calculation and return result in centimeter
- SR04 measure echo time to obstacle and return way.
-
- Sound travels with 340m/sec
-
- Example: Obstace 100cm away from SR04. Measure time is 100cm to
- obstacle and 100cm return = 200cm
-
- 1sec = 1000ms = 1.000.000uS
- 1.000.000 / 340 = Distance in microseconds for 100cm
- 2941uS fuer 100cm = 5882 uS fuer 200cm
- duration / 5882 * 100 = distance in cm
*/
long MicrosecondsToCentimeter(long duration);
long _currentDistance;
int _echoPin, _triggerPin;
long _duration, _distance;
bool _autoMode;
};
#endif
Please post a link to where you downloaded it from.
Please provide a detailed description of what you did with it after you downloaded it.
Those are only the declarations. You are missing the source code, thus the error.
To properly install a library, inside the IDE, go to Sketch -> Include Library -> Manage Libraries...
This will open up the library manager, search for SR04 and you will get several matches for this library. Click on the one you are using and click "install".
After installation, you can look at the examples that came with the library (File->Examples->...) and use those as a starting point for your project.
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.