Hi there,
Lately I've been trying to fix this rather nasty issue I'm having...
I would like to do some basic maths in structs with floats, the problem is that it isn't allowing me to do so.
Here's the code:
//Lichtsturing
int pinVoor = A0;
int pinAchter = A1;
int pinLinks = A2;
int pinRechts = A3;
typedef struct zijde{
float waarde;
float waardeRelatiefVoor;
float waardeRelatiefAchter;
float waardeRelatiefLinks;
float waardeRelatiefRechts;
} zijde;
void setup(){
Serial.begin(9600);
}
void loop(){
Lichtrijder();
}
void Lichtrijder(){
zijde voor = {analogRead(A0), 1.0, analogRead(A0)/analogRead(A1), analogRead(A0)/analogRead(A2), analogRead(A0)/analogRead(A3)};
//zijde achter = {analogRead(A1), analogRead(A1)/analogRead(A0), 1.0, analogRead(A1)/analogRead(A2), analogRead(A1)/analogRead(A3)};
//zijde links = {links, analogRead(A2)/analogRead(A0), analogRead(A2)/analogRead(A1), 1.0, analogRead(A2)/analogRead(A3)};
//zijde rechts = {rechts, analogRead(A3)/analogRead(A0), analogRead(A3)/analogRead(A1), analogRead(A3)/analogRead(A2), 1.0};
Serial.println(voor.waardeRelatiefAchter);
//Serial.println(achter.waardeRelatiefAchter);
//Serial.println(links.waardeRelatiefLinks);
//Serial.println(rechts.waardeRelatiefRechts);
//Serial.println(links.waardeRelatiefVoor);
Serial.println("dsad");
delay(5000);
}
Quite some code is disabled since I'm debugging it, or at least trying to I guess...
By using relative resistances in LRD's I want to pinpoint the location of the light source and send the "vehicle" in that direction.
The problem is, however, trying to do maths in the struct will result in "whole" numbers. I don't think I need to explain that further.
I can't put variables in it, since it'll say the variable has already been called. Creating a function returning analogRead will also fail.
The last possible option might be pointers, but that is a stab in the dark with a low chance of success...
Does any of you have any idea?
What a stupid question actually, of course you do.
Edit: 10 seconds after posting this I had en epiphany and I fixed it. God. Damnit.
Well, at least that worked...
I fixed it like this
zijde voor = {voorkant(), 1.0, voorkant()/achterkant(), voorkant()/linkerkant(), voorkant()/rechterkant()};
float voorkant(){
return analogRead(A0);
}
float achterkant(){
return analogRead(A1);
}
float linkerkant(){
return analogRead(A2);
}
float rechterkant(){
return analogRead(A3);
}
voorkant/voor = front
achterkant/achter = back
linkerkant/links = left
rechterkant/rechts = right