katyp
Posts: 10
Joined: Sun May 07, 2017 1:40 pm
Location: London
Contact: Twitter

not sure how to round the valuesin my sense hat program

Tue Jun 12, 2018 2:52 pm

This is a program to make a log of data using the sense hat but the data in the CSV table has too many digits. I would like to round to just one decimal point but have tried round and rounding and can't seem to make it work. Help gratefully received. Also how do I build in a delay that creates an interval in the data readings rather than the writing of the data to file? THANK YOU


from sense_hat import SenseHat
from csv import writer
from datetime import datetime

sense = SenseHat()

#logging

def file_setup(filename):
header =['tem','pres','hum','datetime']
with open(filename,"w") as f:
f.write("'".join(str(value) for value in header) + "\n")



def get_sense_data():
sense_data = []

sense_data.append(sense.get_temperature())
sense_data.append(sense.get_pressure())
sense_data.append(sense.get_humidity())
sense_data.append(datetime.now())

return sense_data

#program



while True:
sense_data = get_sense_data()

with open('garden.csv', 'w', newline='') as f:
data_writer = writer(f)
data_writer.writerow(['temp','pres','hum', 'datetime'])
while True:
data = get_sense_data()
data_writer.writerow(data)

pcmanbob
Posts: 4090
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: not sure how to round the valuesin my sense hat program

Tue Jun 12, 2018 3:44 pm

First off please when posting code use code tags other wise your code is corrupted by the removal of the indentation.

Add [code] at the top

code goes here

and [/code] at the bottom.

so you use round like this

Code: Select all

f= 6.24567
print f
fr = round (f, 1)
print fr
which returns the following results

Code: Select all

[email protected]:~ $ python rex.py
6.24567
6.2
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

katyp
Posts: 10
Joined: Sun May 07, 2017 1:40 pm
Location: London
Contact: Twitter

Re: not sure how to round the valuesin my sense hat program

Tue Jun 12, 2018 4:04 pm

I am sorry about that. Thanks for that tip about posting code. I am still not sure where to put the round bit into the program and if you have time to advise in a bit more detail I would be very grateful. I am pretty new to this.

pcmanbob
Posts: 4090
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: not sure how to round the valuesin my sense hat program

Tue Jun 12, 2018 4:15 pm

If you repost your code using code tags and say which value you want to round I will try and help.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

User avatar
bensimmo
Posts: 2990
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: not sure how to round the valuesin my sense hat program

Tue Jun 12, 2018 5:29 pm

First get use to how the SenseHAT can collect data.
https://projects.raspberrypi.org/en/pro ... ata-logger

And a bit more advanced with the older version of that.
https://github.com/raspberrypilearning/ ... rksheet.md

User avatar
bensimmo
Posts: 2990
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: not sure how to round the valuesin my sense hat program

Tue Jun 12, 2018 5:40 pm

As for rounding, you can do it anywhere you like.

I'm assuming you don't know or haven't learned the basic python yet, but jumping in there and working out as you go.

You can round at

Code: Select all

sense_data.append(sense.get_temperature())
sense_data.append(sense.get_pressure())
...
With

Code: Select all

sense_data.append(round(sense.get_temperature(),1))
sense_data.append(round(sense.get_pressure(),1))
...
Being the easiest.


sense.get_temperature()
Gets the value, it is the number.
Normally you may see it as
Temperature = sense.get_temperature()
to give Temperature the reading value.
25.4343567
And
print( round(Temperature,1) )
would print out 25.4

But doing
print( round(sense.get_temperature,1) )
cut out that extra step.

All you are doing is changing print() to save.append()


Have a play with the print() and rounding() just printing numbers to the screen.

katyp
Posts: 10
Joined: Sun May 07, 2017 1:40 pm
Location: London
Contact: Twitter

Re: not sure how to round the valuesin my sense hat program

Wed Jun 13, 2018 1:27 pm

Thanks so much pcmanbob and ben simmo! I was beginning to get there with round(x , 1) but wasn't getting my punctuation/order right.
I am going to a beginners' Python evening class bensimmo so am learning bottom up but also as you say jumping in and just trying projects as well. I am making painful progress through struggle! I am so grateful for this very clear advice that helps me join up the dots.
In fact, I have now been able to extrapolate from this advice how work out how to express the date more simply as day/month/year hour/minute/second -

Code: Select all

sense_data.append(time.strftime('%m/%d/%Y %H:%M:%S'))

Return to “Beginners”

Who is online

Users browsing this forum: funstuff, W. H. Heydt and 35 guests