Pages: [1]   Go Down
Author Topic: function, Does void x() need  (Read 994 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  #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;
    }}
   
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: April 11, 2012, 01:20:44 pm by KentaGrace » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
God Member
*****
Karma: 10
Posts: 646
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24298
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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;"
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24298
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
God Member
*****
Karma: 10
Posts: 646
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24298
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Did you miss the part after the OR in my post?
No I didn't miss your incorrect "return void;"
Logged

"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.

New Jersey
Offline Offline
Faraday Member
**
Karma: 48
Posts: 3393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
God Member
*****
Karma: 10
Posts: 646
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. 
Logged

UK
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A 'typo' would be "return viod;", surely?  smiley-wink
Logged

Per Arduino ad Astra

Pages: [1]   Go Up
Jump to: