Performing float maths in struct [Solved]

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

Good ! :slight_smile:

Sometimes when having a problem explaining it great detail to someone who does not understand a thing (which is why you have to explain it in detail) will get your mind in the right state so you actually look at your problem with a fresh point of view. I, and my friends, call this "Explain it to the TeddyBear".

In this case it was the forum. Good you put the answer there, too. The last bit you could (and something far far to few people do) is to change the subject heading of the first post, including something like "[Solved]".

(edit fixed typos)

Instead of a bunch of functions that only call one other function, you could have used a cast or two, and some white space:

  zijde voor =
  {
    (float)analogRead(A0),
    1.0, 
    (float)analogRead(A0)/(float)analogRead(A1), 
    (float)analogRead(A0)/(float)analogRead(A2), 
    (float)analogRead(A0)/(float)analogRead(A3)
  };