Go Down

Topic: function, Does void x() need (Read 1 time) previous topic - next topic

I have been struggling for hours and staring at the example, now my head is killing but I'm determined.
I manged to get a simple print a float number to serial but now I'm trying to get a simple timing function to Wait 5 secs before turning on the output and nothing seems to work. My code shows where i'm at. It's a breakdown of a large program that works ( don't ask me how ) but it's not using functions and I am running out of space on an 88a.
Could someone tell me if a void function needs return x; and what I am doing wrong
Code: [Select]
  #include <LiquidCrystal.h>
  #include <Elapsed.h>
 
  LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  const int button = 8;
  const int Relay = 10;
  int buttonPressed = 0;
 
 
  void setup()
  {
    lcd.begin(16,2);
    pinMode(button, INPUT);
    pinMode(Relay, OUTPUT);
  }
  static Elapsed t1;
 
  float MainBatt = 0;
  float MainVolt = 0;
 
  void loop(){
   MainBatt = analogRead(0);
   MainVolt = (MainBatt * 19.69) / 1024; 
  lcd.setCursor(0,0);
  lcd.print(MainVolt,1);
  int x; // if i put this inside the if statement it's not compiling ?
  if(MainVolt > 13.2)
  {
   
    x = RelayDelayOn();
    digitalWrite(Relay, HIGH);
  }
  }
 
  void RelayDelayOn(){
    int Pressed = false;
    int Activate = 0;
    int result;
    if(!Pressed && Activate != HIGH)
    {
      Activate = HIGH;
      Pressed = true;
      t1.reset();
    }
    if(Pressed && t1.intervalMs() > 5000)
    {
      Pressed = false;
      result = HIGH;  // I did have the digitalWrite here originally
      return result;
    }}
   

#1
Apr 11, 2012, 08:17 pm Last Edit: Apr 11, 2012, 08:20 pm by KentaGrace Reason: 1
Correct me if I'm wrong, as I'm new to programming, but if you want a return from a function, shouldn't you change void to the type of output you expect?  The way I understand it, the "void" at the beginning of a function says that the function is expected to return nothing.  Maybe if you change the void RelayDelayOn() into int RelayDelayOn()?

EDIT:

As an addendum, the more I think about it, the more this makes sense.  If the original function isn't expecting a return from the function it's calling, it will always return nothing, most likely even if you specify inside the called function to return a value.  Try changing the type of function from void to int, and see if that doesn't fix the problem.

I have tried all that and much more but i don't need it to return anything if the digitalWrite was in the function but that didn't work either.
Guess I'm going to have to start from basics and work up from simple functions.

The Arduino explantion and example is not much.

Here's a question, where does the Elapsed.h come from, as in, what library are you using?

Delta_G

If the function is supposed to return void, then it should not have a return statement.  Or should have one that says

return void;


If you want it to return result (an int) then the function needs to be declared as returning int and not void.

dxw00d

Quote
Or should have one that says

return void;


That wouldn't compile. You don't return a type, you return a value. If you attempt to put a return with a value into a void function, the compiler will object.

AWOL

Quote
If the function is supposed to return void, then it should not have a return statement

Wrong.
A void function can have a "return;"
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

It sounds like the problem might be in his use of the Elapsed library, as he's using that for his timer?  I'm not sure where to find the library to download and test, unfortunately, and the program will not compile without it.  However, I still believe that the function called needs to be of type int, to get a value returned to the main function, and so far the comments have not led me to believe otherwise, however, how would a return work inside a void, if the function is expected to have no return?

AWOL

It's a return from the function (subroutine).
You can put a return in "loop", and it will simply run "loop" from the top, since "loop" is called within "main" in an infinite loop.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

dxw00d

Quote
I still believe that the function called needs to be of type int, to get a value returned to the main function


You're right. In the code posted, result is an int, so if the OP wants to return that value, the function will need to be declared as int.

Delta_G


Quote
If the function is supposed to return void, then it should not have a return statement

Wrong.
A void function can have a "return;"


Did you miss the part after the OR in my post?

Or can just say return;

If you don't have a return statement in a void function it will compile just fine.  Try it.  No return necessary.

AWOL

Quote
Did you miss the part after the OR in my post?

No I didn't miss your incorrect "return void;"
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

wildbill

Pressed and activate get set afresh every time you call relayDelay, so you always call t1.reset. Make them global, or better, static. I'm not sure you need them both either, try it without activate. Put the digitalWrite back in the function and drop the return. Any better?

Delta_G


Quote
Did you miss the part after the OR in my post?

No I didn't miss your incorrect "return void;"


But that's not the part you quoted is it? 

Sorry for the typo, yes the void should not be there.  Happened when I started writing, "if you want to return void" and then editing.

My bad. 

Groove

A 'typo' would be "return viod;", surely?  ;)
Per Arduino ad Astra

Go Up