Whats this thing?

So if I understand correctly when the code is in print(measureVal), it executes the void print loop and inside that loop float result is the same as measureVal, because of : print(measureVal); But the thing I don't understand is what is the float readTemp(int pin) {... } Like it's not a loop, it looks like a variable. How can it not be inside a loop. And what the actual freaking donut does this thing do? thanks void setup() { Serial.begin(9600); } void loop() { float measureVal=readTemp(A0); print(measureVal); delay(1000); } float readTemp(int pin) { int reading=analogRead(pin); float voltage=(5.reading)/1023.; float temperature=(voltage-0.5)100; return temperature; } void print(float result) { Serial.print("Temp: "); Serial.println(result); }
70 Replies
MaderDash
MaderDash6h ago
Ok start with the basics algebra. What is this called: x=10+y @TheStudier
TheStudier
TheStudierOP6h ago
that is a equation or is it
MaderDash
MaderDash6h ago
By the way you have been at this long enough to have learnt all of this. YES. so what would the value of x be?
TheStudier
TheStudierOP6h ago
I have had some breaks
TheStudier
TheStudierOP6h ago
so who knows
MaderDash
MaderDash6h ago
Right what if we did this: x=10+2
TheStudier
TheStudierOP6h ago
12!
MaderDash
MaderDash6h ago
Ok what IF. we had NO x. So we had this: [blank]=10+2 blank means there is n9thing there FYI
TheStudier
TheStudierOP6h ago
the variable is 12 idk
MaderDash
MaderDash6h ago
right but there is no place to PUT the SUM, so then nothing happends. Make sence?
TheStudier
TheStudierOP6h ago
yes
MaderDash
MaderDash6h ago
ok NOW lets connect this what is this:
void(){

}
void(){

}
TheStudier
TheStudierOP6h ago
looop
MaderDash
MaderDash6h ago
no.
TheStudier
TheStudierOP6h ago
damn that is a void without a name im not the bigtest bulb
MaderDash
MaderDash6h ago
its a "function", same as: x=10+2
TheStudier
TheStudierOP6h ago
oh
MaderDash
MaderDash6h ago
ok so what does the word "void" represent?
TheStudier
TheStudierOP6h ago
x no idea
MaderDash
MaderDash6h ago
Look it up, get a dictonary
TheStudier
TheStudierOP6h ago
the function does not return a value
MaderDash
MaderDash6h ago
well thats TRUE, but void is emptiness:
No description
TheStudier
TheStudierOP6h ago
oh so that what you meant
MaderDash
MaderDash6h ago
OK so now you know what a function is. And now you know what a void is, lets move forward.
void loop() {
float measureVal=readTemp(A0);
print(measureVal);
delay(1000);
}
void loop() {
float measureVal=readTemp(A0);
print(measureVal);
delay(1000);
}
what does THIS function return?
TheStudier
TheStudierOP6h ago
nothing?
MaderDash
MaderDash6h ago
BINGO OK now what if we did this:
void theStudier() {
int x = 2 + 10;
return(x);
}
void theStudier() {
int x = 2 + 10;
return(x);
}
TheStudier
TheStudierOP6h ago
does it return x? hmm but where
MaderDash
MaderDash6h ago
well it will TRY to return x. but as its returing it into a VOID,, what do you feel is going to happen to x ?
TheStudier
TheStudierOP6h ago
the x will be never seen again
MaderDash
MaderDash6h ago
RIGHT so what would we need to do to be able to get X out of this function? remember in this example X will alwaysbe an int value
TheStudier
TheStudierOP6h ago
write it outside the loop xd
MaderDash
MaderDash6h ago
no, because there is no LOOP
TheStudier
TheStudierOP6h ago
oh sorry forgot
MaderDash
MaderDash6h ago
have you ever played catch? with a ball and a friend?
TheStudier
TheStudierOP6h ago
sure i can throw good
MaderDash
MaderDash6h ago
ok so you toss the ball to your friend, they catch it and throw it back and you catch it right?
TheStudier
TheStudierOP6h ago
sometimes that happens tes yes*
MaderDash
MaderDash6h ago
What would happen IF you tossed the ball to your friend, but your friend VANISHED into NOTHINGNESS [void]
TheStudier
TheStudierOP6h ago
The ball would just fly out there not return
MaderDash
MaderDash6h ago
BINGO, so what is needed to "catch" the ball, back and forth?
TheStudier
TheStudierOP6h ago
TWO people
MaderDash
MaderDash6h ago
looking at this again.
void theStudier() {
int x = 2 + 10;
return(x);
}
void theStudier() {
int x = 2 + 10;
return(x);
}
we are CLEARLY making a ball. X And we are CLEARLY trying to TOSS the ball. return(x); But something needs to change in this function for the ball to GO somewhere. Think a few minutes of what we need to change, Ill BRB
TheStudier
TheStudierOP6h ago
take out the void?
MaderDash
MaderDash6h ago
And replace void with?
TheStudier
TheStudierOP6h ago
some variable i guess variable type indicator
MaderDash
MaderDash6h ago
Great
TheStudier
TheStudierOP6h ago
pleaes
MaderDash
MaderDash6h ago
What type of varable are we TOSSING.
TheStudier
TheStudierOP6h ago
INT
MaderDash
MaderDash6h ago
Ok so fix the example function.
TheStudier
TheStudierOP6h ago
inr theStudier() { int x = 2 + 10; return(x); } ?
TheStudier
TheStudierOP5h ago
so float measure val =readTeamp(A0). and then float readTemp(int pin) does stuff to and return the value
MaderDash
MaderDash5h ago
and as its a float, what type of value is it "TOSSING"
TheStudier
TheStudierOP5h ago
floating value, pretty easy to toss as its already floating
MaderDash
MaderDash5h ago
right. So now we go the the secondpart of the game of "catch". You have tossed the int to your friend, but now YOU need to be ready to receive something back. [in this example YOUR the function inr theStudier() so How can we get ready to CATCH a value as a function. We learnt how to TOSS, but catching.
TheStudier
TheStudierOP5h ago
int theStudier(someting here?)
MaderDash
MaderDash5h ago
So reformat that example function to receive 2 values, add the values, then return the sum of the two values please. Fix this:
void theStudier() {
int x = 2 + 10;
return(x);
}
void theStudier() {
int x = 2 + 10;
return(x);
}
Take your time if you must
TheStudier
TheStudierOP5h ago
void theStudier(int sausage, int IhaveToRun) { int x = sausage, IhaveToRun; return(x); } ?
MaderDash
MaderDash5h ago
close, your not adding them currently.
TheStudier
TheStudierOP5h ago
+ not ,
MaderDash
MaderDash5h ago
and there is another error. Dont guess, think it THROUGH
TheStudier
TheStudierOP5h ago
but should i change the void
MaderDash
MaderDash5h ago
You know the answer to this. If not then review this convo'
TheStudier
TheStudierOP5h ago
int theStudier(int sausage, int IhaveToRun) { int x = sausage,+IhaveToRun; return(x); }
MaderDash
MaderDash5h ago
Ok add it to your code, and use it., see if it will work. BTW if you realy need to go, thats fine, you can come back to this convo when ever.
TheStudier
TheStudierOP5h ago
no I have time
MaderDash
MaderDash5h ago
Remember your goal is to LEARN, so IF you have issues, or if you dont get the results you expect try and work through it. I have given you all the tools you should need to acomplish this oal.

Did you find this page helpful?