Problems implementing an IF statement in a function declaration

Hi Guys, as stated I got some problems implementing an IF statement in a function declaration. I had it working before, but I got no idea what’s wrong now.

Here’s the code:

int number;
int sensorvalue;

void setup() {

Serial.begin(9600);

}

void loop() {

int sensorvalue = analogRead(A0);

value(0,333,1);
value(334,666,2);
value(667,1023,3);

Serial.print(sensorvalue);
Serial.print("\t");
Serial.println(number);

delay(200);

}

int value(int x, int y, int c)
{

if (sensorvalue > x && sensorvalue < y)
{
number = c;
}
}

So the goal of the code is to:
print the number 1 when sensorvalue is between 0 and 333.
print the number 2 when sensorvalue is between 334 and 666.
And print the number 3 when sensorvalue is between 667 and 1023.

I can’t see what’s wrong.

Could you please help?

I can't see what's wrong.

You've promised the compiler that function "value" will return an "int" value, but you broke that promise.

Please use code tags when posting code.

Thanks for the quick reply. How to fix it though? I'm fairly new at programming :)

Ps. I will use code tags next time!

You don't have or appear to want a return value, so specify void instead of int as the return type.

I actually want to print the ‘number’ that is defined by ‘int c’ in the function.
I adapted the code slightly, but it still won’t print the correct numbers…

 int number; 
int sensorvalue; 
int printvalue; 

void setup() {

Serial.begin(9600); 

}

void loop() {

int sensorvalue = analogRead(A0); 

printvalue = value(0,333,1);
printvalue = value(334,666,2);
printvalue = value(667,1023,3);

Serial.print(sensorvalue);
Serial.print("\t"); 
Serial.println(printvalue); 

delay(200); 

}


void value(int x, int y, int c)
{
  
 if (sensorvalue > x && sensorvalue < y)
   {
     number = c; 
     return number;  
 }
}

ah 'int value' instead of 'void value' i mean:)

Also tried this code, but won’t work either.

int number; 
int sensorvalue; 

void setup() {

Serial.begin(9600); 

}

void loop() {

int sensorvalue = analogRead(A0); 

value(0,333,1);
value(334,666,2);
value(667,1023,3);

Serial.print(sensorvalue);
Serial.print("\t"); 
Serial.println(number); 

delay(200); 

}

void value(int x, int y, int c)
{
  
 if (sensorvalue > x && sensorvalue < y)
   {
     number = c; 
     }
}

Either put

return number;

in the function or, as number is a global variable and you don't, therefore, need to return it, change the function type to void to indicate that nothing will be returned from it.

You will need to print the returned value or the number variable after the call. So either

Serial.println(value(0,333,1));

or

value(0,333,1);
Serial.println(number);

Appreciate the help, but still don’t got it to be honest!
basically wat I want to do is the following:

int number; 
int sensorvalue; 

void setup() {

Serial.begin(9600); 

}

void loop() {

int sensorvalue = analogRead(A0); 
Serial.println(sensorvalue); 

if (sensorvalue > 0 && sensorvalue < 333)
  {
   number = 1; 
  }

if (sensorvalue > 334 && sensorvalue < 666)
  {
   number = 2; 
  }

if (sensorvalue > 667 && sensorvalue < 1023)
  {
   number = 3; 
  }

Serial.println(number); 

delay(200); 

}

But then by using a function :slight_smile:

When you say the code doesn't work, what results are you seeing?

It might be as simple as changing these

number = 3;

to

number = '3';

so you are sending a character to the serial monitor instead of a non-printing character. Look at www.asciitable.com and see what character gets printed for decimal 3.

That last code I posted works perfectly, it prints 1 when the sensorvalue is between 0 and 333. Print 2 when the sensorvalue is between 334 and 666 and prints 3 when the sensorvalue is between 667 and 1023.

For the code I posted earlier, using the functions, it simply prints 0 being the number for all the sensorvalues…

the code I posted earlier is

int number; 
int sensorvalue; 

void setup() {

Serial.begin(9600); 

}

void loop() {

int sensorvalue = analogRead(A0); 
Serial.println(sensorvalue); 

value(0,333,1);
value(334,666,2);
value(667,1023,3);


Serial.println(number); 

delay(200); 

}

void value(int x, int y, int c)

{
  if (sensorvalue > x && sensorvalue < y)
   {
     number = c;  
     }
}

So to be clear, I want the effect of the code that works, but then by using a function :)

Try using 49, 50, 51 for 1,2,3 instead. And byte c instead of int c.

Still aint working .. :(

Then I'd suggest it's a problem with program flow. Try printing the results after each function call.

You have a global variable that you never assign a value to. You have a local variable that you never test. I'm sure that you can see that having two variables of the same name is stupid.

int sensorvalue = analogRead(A0);

This line of code is not going to do what you want.

CrossRoads:
Then I’d suggest it’s a problem with program flow.
Try printing the results after each function call.

Tried that, does not work either…

PaulS:
You have a global variable that you never assign a value to. You have a local variable that you never test. I’m sure that you can see that having two variables of the same name is stupid.

Is it not the same then? I tried testing the sensorvalues in the function itself, but that does not work either…

michinyon:

int sensorvalue = analogRead(A0);

This line of code is not going to do what you want.

This actually works:)

My answer and PaulS’s answer are actually the same answer, and they both work.