Simple Class doesn't work, also problem delay inside for.

Hello, i am trying to create modular like classes for different usages of my arduinos, so it becames easy to switch between functions.

Here is the code of the LM35 thermometer reader, so i can read the last value of the instance or ask for a new one. Good?, NO!

For some reason it gets stuck within a range of values, kinda only modifying the LSB’s. Also i tried to get the average(mean) of a few readings, but the delay(10) function crashes it all.

Thanks in advence.

class termometreLM35{
  int iteracions=3;
  public:
  int sensorPin= A2;
  float ultimsMillivolts;
  float ultimsGraus; 
  int ultimValor;
  termometreLM35(){
    grausActuals();
  }
  float grausActuals(){
    int value;
    for(int i=0; i<iteracions; i++){
      value = analogRead(sensorPin);
    }
    ultimValor = value;
    ultimsMillivolts  = (value / 1023.0) * 5000;
    ultimsGraus = ultimsMillivolts / 10; 
    return ultimsGraus;
  }
};

Instancing:

termometreLM35 niFredNiCalor;

Read value:

niFredNiCalor.grausActuals()

Unusable code because of delay function

class termometreLM35{
  int iteracions=3;
  public:
  int sensorPin= A2;
  float ultimsMillivolts;
  float ultimsGraus; 
  int ultimValor;
  termometreLM35(){
    grausActuals();
  }
  float grausActuals(){
    int value =0;
    for(int i=0; i<iteracions; i++){
      value += analogRead(sensorPin);
	  delay(10);
    }
	value = (value / iteracions);
    ultimValor = value;
    ultimsMillivolts  = (value / 1023.0) * 5000;
    ultimsGraus = ultimsMillivolts / 10; 
    return ultimsGraus;
  }
};
    int value;
    for(int i=0; i<iteracions; i++){
      value = analogRead(sensorPin);
    }

What is the point of reading the sensor more than once, when it is only the last value that matters?

    return ultimsGraus;

What is the point of returning a public class member from this function?

  termometreLM35(){
    grausActuals();
  }

Why does the constructor call grausActuals()?

Read value:

niFredNiCalor.grausActuals()

That call does nothing useful, since you don’t save the returned value.

PaulS:
Why does the constructor call grausActuals()?

To update the value.

PaulS:
What is the point of returning a public class member from this function?

To spare lines of code, so you can put it in a single line.

PaulS:
What is the point of reading the sensor more than once, when it is only the last value that matters?

It was meant to do the average of multiple readings to get more accuracy, but, you are right, by now it's useless.

PaulS:
That call does nothing useful, since you don't save the returned value.

It's just an example to show you the calling method, its a 600+ lines ino file, just sparing the big bulk. The actual line is:
paquetResposta.setBytes34((int)(niFredNiCalor.grausActuals()*100));
This line sets the value into another class for processing.

Regards

To update the value.

Why is that necessary? There should be NO public variables in your class. All data that the class has should be returned ONLY by calling a method in the class that returns a private value.

To spare lines of code, so you can put it in a single line.

Complete nonsense. If you were trying to reduce lines of code, you wouldn't have the function return a value.

It's just an example to show you the calling method, its a 600+ lines ino file

That's about 580 lines too many to demonstrate that a class is, or is not, working properly.

By the way, the convention in most places is for class names to start with an upper case letter. Keypad, Serial, Keyboard, etc.

PaulS:
Why is that necessary? There should be NO public variables in your class. All data that the class has should be returned ONLY by calling a method in the class that returns a private value.

So what's the point of public members?

PaulS:
Complete nonsense. If you were trying to reduce lines of code, you wouldn't have the function return a value.

So, guess a way in a single line to do muliple calls to multiple methods better than: funcion1(function2(example_var))

PaulS:
That's about 580 lines too many to demonstrate that a class is, or is not, working properly.

There are about 15 classes, but i'm not gonna post the entire bulk here for practical reasons. The problem is in THIS one.

PaulS:
By the way, the convention in most places is for class names to start with an upper case letter. Keypad, Serial, Keyboard, etc.

Good, it's true, thanks. But are you gonna guess why the returned value of stays about the same within calls to grausActuals()?

So what's the point of public members?

They make life easy for lazy programmers. Professional programmers rarely have public members.

So, guess a way in a single line to do muliple calls to multiple methods better than: funcion1(function2(example_var))

There is nothing wrong with the function returning a value. The value that it returns should be a local variable. There is no reason to have most of your variables as public members of the class.

But are you gonna guess why the returned value of stays about the same within calls to grausActuals()?

I hate guessing games, but if I had to guess I would guess that the value stays about the same because the temperature stays about the same. Or, that there is some other code, or hardware, that is interfering.

That is why you should have a sketch that uses only ONE library and expects only one piece of hardware connected - so you can isolate the problems. "United, we stand; divided, we fall". Well, you want your bugs divided.

Thanks for the answer.

The temperature does not stay the same, already tested.

PaulS:
That is why you should have a sketch that uses only ONE library and expects only one piece of hardware connected

The sad answer is i am creating a multifunctional rf device, that uses relay control, distance/motion detection, and temperature.

Altrough, the LM35 examples works fine.

:o

Any idea?

Any idea?

Yes, but you've rejected that idea.

You REALLY need to determine if the problem is with the hardware, the library, the interaction with other hardware, or the interaction with other libraries.

Pull all the hardware except the LM35. Create a sketch that does nothing more than create an instance of the class and, in loop(), periodically calls the get temperature method and print the result to the serial port.

If that doesn't work, you have far less hardware, and far less code, to deal with.

If it DOES work, add the other hardware, but NO more software, back, one piece at a time. If you get all the hardware attached, and the simple sketch still works, you've learned something. If not, you've learned something else.

Actually, there is a spi bus device, the relays, and the lm35. This device allegedly supports this.


Image:

Anyways, if i put a delay(10); inside the for loop it doesn't boot at all, with or without ANY hardware. :o :o

Will try the rest today later.

Anyways, if i put a delay(10); inside the for loop it doesn't boot at all, with or without ANY hardware. :o :o

Are you still (incorrectly) calling the grausActuals() method from the constructor?

The grausActuals() assumes that the hardware, including the timers, are initialized when the constructor is called. That is NOT a valid assumption.