6 years, 3 months ago.

How can I set RTC to readAnalog?

Hello everybody,

I am using a typical program in a NUCLEO STM32L0 to read an analog input from a temperature sensor and after that I convert the analog reading to a char using sprintf (this is because i would like to print several readings in the same vector). Two questions have arosen: How should I modify this code to save several readings in a char vector? How can I set the Real Time Clock (RTC) to carry out measurements every two hours for example?

I attach my code here. Thanks in advance

i

#include "mbed.h"

AnalogIn LM35(A0);
Serial pc(SERIAL_TX, SERIAL_RX);

int main() {
    
     float tempC,tempF,a[10],avg;
        int i;
    while(1) {
        
        for(i=0;i<10;i++)
        {
        a[i]=LM35.read();
        a[i]=a[i]*330;//to convert into Celsius
        
        wait(.2);
        char buffer [50];
        sprintf (buffer, "T= %s",a[i]);
        pc.printf("T=%s",a[i]);    
        }
    
    
    }
}

1 Answer

6 years, 3 months ago.

You might have to clarify what you mean by "How should I modify this code to save several readings in a char vector". You seem to be using a char array called buffer[]. If you could give an example of what you need the final string to look like someone can probably help better. The function sprintf() works just like printf so you can give it multiple arguments if you want. If the array a[] were fully populated you could write the first three float values with two decimal places, separated by commas as follows:

sprintf(buffer, "%.2f,%.2f,%.2f",a[0],a[1],a[2]);

The easiest way to handle the RTC question is to just poll the RTC value every so often, say every 1 second. With the time_t (aka integer in seconds) it returns it is very easy to keep track of when 2 hours has gone by. Should be able to use the RTC to measure passage of time like this without worrying about actually setting the RTC to the correct time.

Accepted Answer