The clang-tidy tool used by the language server in Arduino IDE 2.x even provides a nice hint about this:
Sorry for the late reply. I guess there is a limit to how many times you can reply in a day. This is where everything is at after I tried more troubleshooting.
The main program is below.
#include "ACS712.h"
void setup()
{
create_amp_meter(A1);
Serial.begin(9800);
}
void loop()
{
Serial.println(read_amp_meter());
}
The ACS712.h file looks like
#ifndef ACS712_H
#define ACS712_H
#include <Arduino.h>
int currentPin, tripPin, sensitivity, adcValue, offsetVoltage;
double adcVoltage, currentValue, amp_trip_value;
void create_amp_meter(int);
double read_amp_meter();
bool amp_trip();
#endif
and the ACS712.ccp file is
#include "ACS712.h"
/* Create the amp meter.
*/
void create_amp_meter(int amp_pin)
{
sensitivity = 66;
adcValue= 0;
offsetVoltage = 2500;
adcVoltage = 0;
currentValue = 0;
amp_trip_value = 15; // The amp you want the system to trip at.
currentPin = amp_pin;
}//------------------------------------------------------------------------------
/* Read the amp meter and do the math to get the amp value.
*/
double read_amp_meter()
{
adcValue = analogRead(currentPin);
adcVoltage = (adcValue / 1024.0) * 5000;
currentValue = ((adcVoltage - offsetVoltage) / sensitivity);
return currentValue;
}//------------------------------------------------------------------------------
/* Check to see if the trip point has been reached. This function is
* ment to trip if the amp over shoot the amp_trip value. This
* is to prevent burning up the heating coil if something goes wrong.
*/
bool amp_trip()
{
double reading = read_amp_meter();
if (reading > amp_trip_value)
return true;
return false;
}
The full error message is
sketch/simple_amp.ino.cpp.o (symbol from plugin): In function `setup':
(.text+0x0): multiple definition of `amp_trip_value'
sketch/ACS712.cpp.o (symbol from plugin):(.text+0x0): first defined here
sketch/simple_amp.ino.cpp.o (symbol from plugin): In function `setup':
(.text+0x0): multiple definition of `currentValue'
sketch/ACS712.cpp.o (symbol from plugin):(.text+0x0): first defined here
sketch/simple_amp.ino.cpp.o (symbol from plugin): In function `setup':
(.text+0x0): multiple definition of `adcVoltage'
sketch/ACS712.cpp.o (symbol from plugin):(.text+0x0): first defined here
sketch/simple_amp.ino.cpp.o (symbol from plugin): In function `setup':
(.text+0x0): multiple definition of `offsetVoltage'
sketch/ACS712.cpp.o (symbol from plugin):(.text+0x0): first defined here
sketch/simple_amp.ino.cpp.o (symbol from plugin): In function `setup':
(.text+0x0): multiple definition of `adcValue'
sketch/ACS712.cpp.o (symbol from plugin):(.text+0x0): first defined here
sketch/simple_amp.ino.cpp.o (symbol from plugin): In function `setup':
(.text+0x0): multiple definition of `sensitivity'
sketch/ACS712.cpp.o (symbol from plugin):(.text+0x0): first defined here
sketch/simple_amp.ino.cpp.o (symbol from plugin): In function `setup':
(.text+0x0): multiple definition of `tripPin'
sketch/ACS712.cpp.o (symbol from plugin):(.text+0x0): first defined here
sketch/simple_amp.ino.cpp.o (symbol from plugin): In function `setup':
(.text+0x0): multiple definition of `currentPin'
sketch/ACS712.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino Mega or Mega 2560.
You're defining variables like amp_trip_value in the .h file. That's wrong. They should only be declared in the .h and defined in the .cpp. If you don't know the difference, see: https://www.cprogramming.com/declare_vs_define.html.
I checked out the link but am still confused. From the latest code in post #22 I believe they are declared in only the h and defined in the constructor function of the cpp file. Is that wrong or should the int val be in the cpp file?
The variables should be DECLARED in the .h file:
extern int currentPin, tripPin, sensitivity, adcValue, offsetVoltage;
extern double adcVoltage, currentValue, amp_trip_value;
and DEFINED in the .cpp file:
int currentPin, tripPin, sensitivity, adcValue, offsetVoltage;
double adcVoltage, currentValue, amp_trip_value;
Thank you,
Are those variables ever going to be used outside of the .cpp file?
I do not plan to. Does that change the code if I do?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.