I'm trying to assign the output of my function (which adjusts a DAC output) to a variable so I can print and display it in my serial monitor. Here's the code
#include <Wire.h>
#include <Adafruit_MCP4725.h>
Adafruit_MCP4725 dac;
int volza;
void setup() {
Serial.begin(38400);
Serial.println("Hello!");
dac.begin(0x60);
}
void loop() {
volza = dac.setVoltage(1095, 1); // THIS IS THE PROBLEM
Serial.println(volza);
}
I am receiving an error because of the commented line. I think it's because I am trying to return a value while inside a void loop. How do you fix this?
1 Answer 1
The setVoltage
method has the following prototype:
void setVoltage( uint16_t output, bool writeEEPROM );
This means it does not return a value.
If you expect to return 1095, why not write:
volza = 1095;
setVoltage(volza, 1);
Serial.println(volza);
Some background information:
Assume you want to have the library a getVoltage
method, than this is possible (you have to make a copy in your own project of the file and add the method. However, I'm not sure how easy it is to get the value from the EEPROM. If it is not easy, there is some programming work to do. Another solution is to store the value in memory, but that means some extra bytes from the scarce free SRAM. Also, in theory the content could be changed already (if another MCU would have access to the EEPROM too).
-
Great, thank you so much for the clarification. The "1095" I have is just a dummy example, I plan on having an analog input actually adjust this value, therefore I was interested in reading it by assigning it to a variable. Would you say I could use an analogRead instead of trying to assign it to a variable?Ali the Dazzling– Ali the Dazzling2019年07月21日 23:29:17 +00:00Commented Jul 21, 2019 at 23:29
-
Probably, however, it depends on the implementation of setVoltage what actually happens.Michel Keijzers– Michel Keijzers2019年07月22日 10:48:42 +00:00Commented Jul 22, 2019 at 10:48
I know it's because I am trying to return a value while inside a void loop
... how do you know this?loop()
... loop() never exits, therefore it is not returning to any caller