Offline
Newbie
Karma: 0
Posts: 38
|
 |
« on: April 11, 2012, 01:10:19 pm » |
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 #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
Newbie
Karma: 0
Posts: 24
|
 |
« Reply #1 on: April 11, 2012, 01:17:26 pm » |
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
Newbie
Karma: 0
Posts: 38
|
 |
« Reply #2 on: April 11, 2012, 01:24:05 pm » |
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
Newbie
Karma: 0
Posts: 24
|
 |
« Reply #3 on: April 11, 2012, 01:30:57 pm » |
Here's a question, where does the Elapsed.h come from, as in, what library are you using?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 6
Posts: 413
|
 |
« Reply #4 on: April 11, 2012, 01:36:14 pm » |
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
Faraday Member
Karma: 19
Posts: 3117
|
 |
« Reply #5 on: April 11, 2012, 01:42:46 pm » |
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
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #6 on: April 11, 2012, 01:43:33 pm » |
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.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 24
|
 |
« Reply #7 on: April 11, 2012, 01:53:30 pm » |
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
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #8 on: April 11, 2012, 01:56:00 pm » |
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.
|
|
|
|
Gosport, UK
Offline
Faraday Member
Karma: 19
Posts: 3117
|
 |
« Reply #9 on: April 11, 2012, 02:00:11 pm » |
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
Sr. Member
Karma: 6
Posts: 413
|
 |
« Reply #10 on: April 11, 2012, 02:46:06 pm » |
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
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #11 on: April 11, 2012, 02:48:41 pm » |
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.
|
|
|
|
New Jersey
Offline
Edison Member
Karma: 26
Posts: 2450
|
 |
« Reply #12 on: April 12, 2012, 06:22:03 am » |
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
Sr. Member
Karma: 6
Posts: 413
|
 |
« Reply #13 on: April 12, 2012, 01:26:12 pm » |
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
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #14 on: April 12, 2012, 01:40:55 pm » |
A 'typo' would be "return viod;", surely? 
|
|
|
|
|
Logged
|
Per Arduino ad Astra
|
|
|
|
|