Overriding the analogRead Function

I am trying to use a simulator on my code and I am getting errors at compile time, when the simulator.h file is added.

I am assuming this is an override problem with the analogRead function.
The only H files I have included are my own where types and #defines are declared, along with function declarations.

I don't specifically add any other .h files as I am led to believe these are automatically included.
If I compile with just my own H file, then everything is OK, adding the Simulator.h file gives the following message.

 wiring_analog.c.o (symbol from plugin): In function `analogReference':
(.text+0x0): multiple definition of `analogReference'
C:\Users\xxx\AppData\Local\Temp\arduino-sketch-CBAA7F9A572B7541332F3CF92E74E441\sketch\Sola.ino.cpp.o (symbol from plugin):(.text+0x0): first defined here
wiring_analog.c.o (symbol from plugin): In function `analogReference':
(.text+0x0): multiple definition of `analog_reference'
C:\Users\xxx\AppData\Local\Temp\arduino-sketch-CBAA7F9A572B7541332F3CF92E74E441\sketch\Sola.ino.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status

Compilation error: exit status 1

I note that in Arduino.h you have
int analogRead(uint8_t pin);
and in the simulator.h you have
int analogRead(int pin);

I am presuming this is a redefinition of the read function ?

Well the compiler complains about multiple definition of analog_reference

Without the code it’s hard to say what’s wrong

Which is what I do not understand.

It gives no mention of where the problem is, in the code and given the code is very large, the only way I have had to determine where this is, is to comment out the analogRead() functions.

The function calls I use are pretty straight forward as follows
In the .h file which corresponds to the code I have

#define Bat_Bank1 A0

and in the main code, I have.

fBat_Bank1_Volts = analogRead(Bat_Bank1);  Reads the Battery Voltage via a divider

  fBat_Bank1_Volts = fVin(fBat_Bank1_Volts); Converts the divider Voltage to give a true reading.

Just in case it, for some reason, did not like the #define, I have even tried changing this one in isolation as a test as to X = analogRead(A0); // Where X is of type float

The same result is seen.

Post the code

Post the code.

I am not posting someone else's code.
You can find the simulator here and download for free. It gives you the simulator library file which you can peruse at your leisure.

Perhaps the issue lies with the simulator being unable to do the thing. What do the simulator people say about the simulator not being able to find the thing? When you open up the Arduino IDE and put your code into the Arduino IDE and select for the IDE to check the code does the Arduino IDE fail with the same not find the file issue?

Hi,
Why do you need a simulator?
How big is the code?

Can you please tell us your electronics, programming, arduino, hardware experience?

Can write your own code and test it in stages in a prototype board?

Tom... :smiley: :+1: :coffee: :australia:

Apparently I cannot post the link on here to the simulator. Which I thought I had originally done so people would have a reference to. No matter just search for xevro.be for the sim I am trying to use.
The reason for the simulator is that I can easily debug problems, which the IDE cannot do for some reason, or if it does then its over complicated. This is a multi MCU system so building the entire system would be costly. A lot of it is using common memory storage areas to transfer data between the MCU's

I do tend to agree that it may be the simulator code, but I was of course wondering if anyone else had come across this. I did submit a report to the writer of the code, but not received a reply as yet.

I program other MCU's so am used to debugging code in pro user friendly packages, something I am missing with Arduino, that I am just learning.

I skinned the code down, but prior to doing this I compiled the entire program without including the simulator h file and it all compiled fine.
Skinned down code here .h file then the .c file


#define Bat_Bank1  A0      // select the A/D input pin for the Voltage sensor

#define Relay1_pin4 4      //  Relay1_pin4
#define Relay2_pin5 5      //  Relay2_pin5
#define LedPin 13          // select the pin for the on board LED

float fBat_Bank1_Volts = 0;   //

#include <SimulatorProgram.h>
#include "sketch_apr30a.h"

float fVin(unsigned int uiConvert) {

	float fVolts;
	fVolts = uiConvert; //Change to a float

	fVolts = fVolts * (fVolts / 1023.0);  // uiConvert Input is 0V - 5V

	fVolts = (fVolts / 204.6) * 2.8;  //
	return fVolts;
}
/*---------------------------------------------------------------------------------------------------------------------------------------------*/

void setup() {

	// declare the LedPin as an OUTPUT:
	pinMode(LedPin, OUTPUT);
	// declare relay pins as OUTPUT
	pinMode(Relay1_pin4, OUTPUT);
	pinMode(Relay2_pin5, OUTPUT);
}

/*---------------------------------------------------------------------------------------------------------------------------------------------*/
void loop()
{
	// read the battery value from the sensors:
	fBat_Bank1_Volts = analogRead(Bat_Bank1);  //Read Battery Bank 1 Voltage
	fBat_Bank1_Volts = fVin(fBat_Bank1_Volts);

}

Tested your simple code in wokwi on line simulator. It compiles fine.

Your simulator might be to blame….

1 Like

Can you please tell us your electronics, programming, arduino, hardware experience?

Hardware design for a long time, ditto Software. Started with assembler, Pascal, C and various other languages. Utilised many processors Hitachi, Siemens and Microchip. University electronics and microprocessor engineering, many years ago when the best CPU at the time was an 80186.

Sad isn't it :slight_smile:

This doesn't enclose the contents of the header file.

It doesn't matter given its an example of the code that doesn't work. That was merely in the file and not really required for this example.

... then I think it matters.

Clearly not but I can easily fix that for you.

Thanks for testing that out and verifying it is more than likely the simulator.

I did get a reply back today stating the following "I think that is because you define the pin A0 and my library also defines that analog pin."

I will just try an #ifdef test, around my code to see if it works.

@latedev you may want to read

a7

Let’s not start this debate again !!!

There is an infinite number of functions that can be used to go back to a possible original value that generated an analogRead value as the ADC conversion is not a bijection and you loose information through sampling.

As long as you fit the guessed value in a correct bin you have a fair possible value.

lol I think these devices use a pretty simple ADC so the errors at the lowest voltage levels is small. Your more likely to have noise than to worry about accuracy at or near 0V.

If this were a more sophisticated ADC, than the number of steps on a linear 16 bit sample, it may become slightly more significant, but not that much to worry about and the formulae for calculating a given Voltage, would of course be more complicated.
I have no idea which type of ADC this MCU uses. Its usually a simple single slope(integrating) ADC but it could also be a dual slope ADC, either way when you look at these or any other type you are actually doing something with hardware and no matter how you wish to describe that hardware function, it is still a hardware function.
Different types all have one thing in common, the number of Voltage levels above 0V, so a 10 bit number has 1023 steps, you can ignore 0V given it is your base line. Simply put a single step is the range between 2 Voltages and its accuracy is directly proportional to the number of bits over its input Voltage range.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.