Support Forum

Share your projects and post your questions

Register   or   Sign In
The Forum

Storing 0.0005 in memory

2384 Views - Created 26/01/2017

26/01/2017

Posted by:
Johnu

Johnu Avatar

Im new to python but getting there writing for my project but am stuck on using the memory storage on the RTC using the example code. What am I missing to store a number libe 0.0005

26/01/2017

Posted by:
Johnu

Johnu Avatar

Sorry - new to the forum as well

26/01/2017

Posted by:
andrew

andrew Avatar

Location:
United Kingdom

andrew Twitter  andrew Website  

Hi

The RTC memory example would only work with whole numbers as it saved the value as an integer. I have created a new python script which shows how you can save a number with decimal places to the RTC memory. You can view the file at https://github.com/abelectronicsuk/ABElectronics_Python_Libraries/blob/master/RTCPi/demos/demo_rtcmemory_double.py

To save a number to the memory you have to convert it into an array of bytes. As the number you want to save is not a whole number it would have to be saved as a double which contains 8 bytes. I created two functions double_to_array() and array_to_double() that you can use to convert the number from a double into an array of bytes and back again.

Hopefully, you can use this example to get your project working.

26/01/2017

Posted by:
Johnu

Johnu Avatar

Thanks Andrew I now understand that bit.

If I wanted to run both of those functions under one function call and pass the variable into it, is that possible?

I'm having problems passing variables between functions and in and out of functions

John

26/01/2017

Posted by:
andrew

andrew Avatar

Location:
United Kingdom

andrew Twitter  andrew Website  

Do you mean to combine the code from both functions into a single function or call both functions from another function? As the double_to_array() and array_to_double() functions take different data types as inputs, an array of bytes and a double, it would be difficult to make combine them together.

If you are having trouble passing variables then it may be worth reading this page on how functions work in python. The main cause of problems I normally find is trying to pass the wrong data type or the number of arguments into the functions.

Sign in to post your reply

Note: documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view, download Adobe Acrobat Reader or other PDF reading software for your computer or mobile device.