A
Arduino•2mo ago
TheStudier

Just a quick helping thing.

So I wanna multiply multiple variables with a variable, but is there a way I can do It easier than: var1multipltyVar; var2multiplyVar; vari3multiplyVar; ...... something like this: (var1,var2,var3)multiplyVar; So I have to change the multiplyVar only once. thanks
29 Replies
TheStudier
TheStudierOP•2mo ago
I know you can do this for setting pinModes with a for loop, but with variables idk.
MaderDash
MaderDash•2mo ago
@TheStudier you can make a function.
TheStudier
TheStudierOP•2mo ago
maths? you mean
MaderDash
MaderDash•2mo ago
So i am understanding you. You have a value. We will call it wart. And you have a bunch of varables, say a through t and you need them all to be multiplied by wart right?
TheStudier
TheStudierOP•2mo ago
indeed yes
MaderDash
MaderDash•2mo ago
Yes. So you make a function to do that Its like 3 lines of code
TheStudier
TheStudierOP•2mo ago
okay thanks
MaderDash
MaderDash•2mo ago
Is interger math ok?
TheStudier
TheStudierOP•2mo ago
the variables are floats
MaderDash
MaderDash•2mo ago
Ok. So u need float math. So you have a void setup. And a void loop right?
TheStudier
TheStudierOP•2mo ago
yep
MaderDash
MaderDash•2mo ago
Make a new function in betweene them.
TheStudier
TheStudierOP•2mo ago
void maths(); and I put maths(); in void loop Im actually wondering why I need to make a new function, cant the code to just void loop? Or does the function I made work differently to void loop
MaderDash
MaderDash•2mo ago
float multiplyer(float top, float bottom){
return(top * bottom);
}
float multiplyer(float top, float bottom){
return(top * bottom);
}
Now in your code just pass your varables into that function. And pull the data out of that function
TheStudier
TheStudierOP•2mo ago
hmm whose is better omg??
MaderDash
MaderDash•2mo ago
That should be fine
TheStudier
TheStudierOP•2mo ago
hmm float myFloat[6]; void thing() { for (int i = 0; i < 6; i++) { myFloat[i] = 22.0 / 7.0 * i; } } so what is the 22.0/7.0 how can i use code block
MaderDash
MaderDash•2mo ago
I thought the values were dynamic. Hence the function. Thats why I asked a chain of varables if its just a bunch of statics then its pointless just code the pre processor to set them ino
TheStudier
TheStudierOP•2mo ago
cpp float myFloat[6];

void thing() {
for (int i = 0; i < 6; i++) {
myFloat[i] = 22.0 / 7.0 * i;
}
}
so what is the 22.0/7.0
cpp float myFloat[6];

void thing() {
for (int i = 0; i < 6; i++) {
myFloat[i] = 22.0 / 7.0 * i;
}
}
so what is the 22.0/7.0
cpp oaky so does the original float change? okay
MaderDash
MaderDash•2mo ago
So its 3.14, then 6.28 ...... so just set the values
TheStudier
TheStudierOP•2mo ago
damn why cant arduino just have code like this
cpp pinMode(1,2,3,4,5,6,OUTPUT);
cpp pinMode(1,2,3,4,5,6,OUTPUT);
cpp
MaderDash
MaderDash•2mo ago
Because its bad form
TheStudier
TheStudierOP•2mo ago
okay
MaderDash
MaderDash•2mo ago
You can make a function to do that. 😉
TheStudier
TheStudierOP•2mo ago
Can you if those numers are variables? That what I asked, hopefully you undersoots, I mena that was my original question.
MaderDash
MaderDash•2mo ago
You can but then you slow cpp down Thats why what it takes python 37 days to do cpp does in 15 secconds. Is because of this exactly
TheStudier
TheStudierOP•2mo ago
okey thank you guys
MaderDash
MaderDash•2mo ago
Yw. Close thread please.
TheStudier
TheStudierOP•2mo ago
close! !close !how

Did you find this page helpful?