"void value not ignored as it ought to be" error

Hi. So recently, I decided to make a smart coaster that measured the temperature of my tea/coffee, and if it was below a certain temperature, it would heat up the coaster bed with a 5v element.

I was writing the code for this and verifying it when I got the error message "void value not ignored as it ought to be" and I've searched it up and can't get an adequate answer.

I'm using the website IDE Arduino create because the standard IDE is completely broken on my computer. Im running Mac OS Sierra 10.12.6 and using an arduino pro micro. Please help.

P.S I'm not great at coding so apologies for the sloppy code.
////////////////////////////////
const int optimalTemp = 50 ;

int tempPin = A2;

int elementPin = 5;

void setup() {

pinMode(elementPin, OUTPUT);

analogReference(INTERNAL);

}

void loop() {

int coasterTemp = getAverageTemp();

if (coasterTemp < optimalTemp){
digitalWrite(elementPin, HIGH);
delay(1000);

}
else{
digitalWrite(elementPin, LOW);
delay(1000);
}

}

void getTemp()
{

float tempC;
int reading;

reading = analogRead(tempPin);
tempC = reading / 9.31;

return tempC;

}

void getAverageTemp()
{

int reading1 = getTemp();

int reading2 = getTemp();

int reading3 = getTemp();

float meanTemp = (reading1 + reading2 + reading3) / 3;

return meanTemp;

}
////////////////////////////////

void getTemp()
{
  
  float tempC;
  int reading;
  
  reading = analogRead(tempPin);
  tempC = reading / 9.31;
  
  return tempC;
  
}

You promised the compiler that you wouldn't return a value, then you went right ahead and returned a value.
Are your pants a trifle warm?

Please remember to use code tags when posting code

How did I promise the compiler that I wouldn't return something, what bit is that in? I told you I'm bad at coding. I genuinely don't know. Thanks for helping though.

Your functions getTemp() and getAverageTemp() are both declared as "void" which means you can't return a value, yet you do.

So make them both floats, since that's what you're trying to return.

float getTemp()
{
.
.


float getAverageTemp()
{
.
.
void getTemp()
{

That's where you promised.

It should be

float getTemp()
{

THANK YOU SOOO MUCH, I didn't know about the void/float beginning to functions thank you.

This may help.

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags:

[code]

[color=blue]// your code is here[/color]

[/code]

Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into logical sections is fine but large spaces for no reason or random blank lines just make for more scrolling when we're trying to read your code.

When you encounter an error you'll see a button on the right side of the orange bar "Copy error messages". Click that button. Use code tags when you paste the error.

You should add hysteresis to your control code to avoid excessive cycling of the heating element.

Also, consider replacing this code:

  int reading1 = getTemp();
  int reading2 = getTemp();
  int reading3 = getTemp();
  float meanTemp = (reading1 + reading2 + reading3) / 3;

with a for loop.

gfvalvo:
You should add hysteresis to your control code to avoid excessive cycling of the heating element.

Also, consider replacing this code:

  int reading1 = getTemp();

int reading2 = getTemp();
  int reading3 = getTemp();
  float meanTemp = (reading1 + reading2 + reading3) / 3;



with a [for loop](https://www.arduino.cc/en/Reference/For).

or don't bother...

reading the same sensor literally milliseconds between readings is unlikely to return anything more useful than a single read...

1 Like