0

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?

  • I know it's because I am trying to return a value while inside a void loop ... how do you know this? – jsotola Jul 21 at 22:39
  • From researching the problem I've come across several threads that mention having a "void" means you cannot have a return value. Please correct me if I'm wrong – Ali the Dazzling Jul 21 at 23:27
  • that is correct, however that would be the value returned by loop() ... loop() never exits, therefore it is not returning to any caller – jsotola Jul 21 at 23:42
  • I see.. thanks! – Ali the Dazzling Jul 22 at 0:56
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 Jul 21 at 23:29
  • Probably, however, it depends on the implementation of setVoltage what actually happens. – Michel Keijzers Jul 22 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.