## Menu project

This is the place for queries that don't fit in any of the other categories.

### Menu project

Hello,
So I am creating a python script that is a menu with sub menus that take data from sensors, there are 16 sensors:
S1, S2, S3, S4, S5, S6, S7, S8, S9, S10, S11, S12, S13, S14, S15 and S16.
Since the data needs to clear I want to take the average value of five sensor data like:

Code: Select all
`S1TA = (S1A + S1B + S1C + S1D + S1E) / 5`

Lets call this project A:

I know that would be the script to get it to work but I don't know how to set it up where the the first value is read as S1A and the second value S1B. So I need to know how to set up this program.

Project B:
I need menus to deal with add a values of weight such as: 5, 10, 15, 25, 50, 100, 500, 1000 and 5000; I have created this:

Code: Select all
`   while ans2:      print("=============================================")      print("==== Adding weight on the polymer Bridge ====")       print("=============================================")      print(""   I.   Add 1 gram:   II.   Add 5 grams:   III.   Add 10 grams:   IV.   Add 25 grams:   V.   Add 50 grams:   VI.   Add 100 grams:   VII.   Add 500 grams:   VIII.   Add 1 kiligram:   IX.   Add 5 kiligrams:   X.   Add 10 kiligrams:   XII.   Exit to main menu: \n "")   ans2 = input("How much do you wish to add?")         if ans2 == "1"            X1 = 1            WeightTotal + X1 = WeightTotal         print("Totals Weight on the bridge is now XT grams")         ans22 = input ("Please press enter to to take Voltages:")            if ans21 == "Enter"               elif ans!="":               print("\n I am sorry I cannot do that")         if ans2 == "2"            X2 = 5            WeightTotal + X2 = WeightTotal         print("Totals Weight on the bridge is now XT")         ans22 = input ("Please press enter to to take Voltages:")            if ans22 == "Enter"            elif ans!="":               print("\n I am sorry I cannot do that")         if ans2 == "3"            X3 = 10            WeightTotal + X3 = WeightTotal         print("Totals Weight on the bridge is now XT grams")         ans23 = input ("Please press enter to to take Voltages:")            if ans23 == "Enter"               elif ans!="":               print("\n I am sorry I cannot do that")         if ans2 == "4"            X4 = 25            WeightTotal + X4 = WeightTotal         print("Totals Weight on the bridge is now XT grams")         ans14 = input ("Please press enter to to take Voltages:")            if ans14 == "Enter"               elif ans!="":               print("\n I am sorry I cannot do that")         if ans2 == "5"            X4 = 50            WeightTotal + X5 = WeightTotal         print("Totals Weight on the bridge is now XT grams")         ans25 = input ("Please press enter to to take Voltages:")            if ans25 == "Enter"               elif ans!="":               print("\n I am sorry I cannot do that")         if ans2 == "6"            X6 = 100            WeightTotal + X5 = WeightTotal         print("Totals Weight on the bridge is now XT grams")         ans26 = input ("Please press enter to to take Voltages:")            if ans16 == "Enter"               elif ans!="":               print("\n I am sorry I cannot do that")         if ans2 == "7"            X7 = 500            WeightTotal + X5 = WeightTotal         print("Totals Weight on the bridge is now XT grams")         ans27 = input ("Please press enter to to take Voltages:")            if ans27 == "Enter"               elif ans!="":               print("\n I am sorry I cannot do that")         if ans2 == "8"            X8 = 1000            WeightTotal + X5 = WeightTotal         print("Totals Weight on the bridge is now XT grams")         ans28 = input ("Please press enter to to take Voltages:")            if ans18 == "Enter"               elif ans!="":               print("\n I am sorry I cannot do that")         if ans2 == "9"            X9 = 5000            WeightTotal + X9 = WeightTotal         print("Totals Weight on the bridge is now XT grams")         ans29 = input ("Please press enter to to take Voltages:")            if ans29 == "Enter"`

So than I want it to do project A, and take the average value of five interactions of each sensor.

Which than I want it to save the data in a new line of txt after each addition of weight like this:

Code: Select all
`Weighttotal, S1TA, S2TA, S3TA, S4TA, S5TA, S6TA, S7TA, S8TA, S9TA, S10TA, S11TA, S12TA, S13TA, S14TA, S15TA, S16TA`

However, I do not know how to save this data into a txt file nor do I know how to exit the menu.

Part two is gong to be the same but instead of adding weights it removing weights. Thank you for your help ahead of time.
Last edited by Mekire on Mon Mar 31, 2014 9:17 am, edited 3 times in total.
Reason: Attempted to fix the formatting.
Deltafee

Posts: 14
Joined: Mon Mar 31, 2014 12:04 am

### Re: Menu project

Hello Deltafee! What you describe can probably be easily done in python, even though I did not understand every detail exactly.

However I am not sure about your question now exactly.

Would you like somebody to write the script for you, or did you already try for yourself, and have some code to show, and specific questions to ask?

From the code you show, I understand this is no python, yet. There would be lots to comment, but it may be easier for you to read a python tutorial first.

Then what I also see is a lot of repetition. I assume you have not heard about python functions yet, but they will definitely help you in time.
Due to the reasons discussed here we are moving to python-forum.net on October 1, 2016.

This forum will be closed. Please create an account at the new site to continue discussion.

IRC://irc.freenode.net/python-forum
Kebap

Posts: 689
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

### Re: Menu project

okay so you caught me, this is kind of my first time programing python. So basically I need help creating a menu that would allow me to add the weights mentioned and take a average of the sensor data and put that into a txt file like the one mentioned.
Deltafee

Posts: 14
Joined: Mon Mar 31, 2014 12:04 am

### Re: Menu project

So I guess the first place to start is:

This is my program to get values from my sensors:
Code: Select all
`while True:        # read the analog pin (temperature sensor LM35)        read_adc0 = readadc(adc0, SPICLK, SPIMOSI, SPIMISO, SPICS)        read_adc1 = readadc(adc1, SPICLK, SPIMOSI, SPIMISO, SPICS)        read_adc2 = readadc(adc2, SPICLK, SPIMOSI, SPIMISO, SPICS)        read_adc3 = readadc(adc3, SPICLK, SPIMOSI, SPIMISO, SPICS)        read_adc4 = readadc(adc4, SPICLK, SPIMOSI, SPIMISO, SPICS)        read_adc5 = readadc(adc5, SPICLK, SPIMOSI, SPIMISO, SPICS)        read_adc6 = readadc(adc6, SPICLK, SPIMOSI, SPIMISO, SPICS)        read_adc7 = readadc(adc7, SPICLK, SPIMOSI, SPIMISO, SPICS)        read_adc8 = readadc(adc8, SPICLK, SPIMOSI, SPIMISO, SPICS)        read_adc9 = readadc(adc9, SPICLK, SPIMOSI, SPIMISO, SPICS)        read_adc10 = readadc(adc10, SPICLK, SPIMOSI, SPIMISO, SPICS)        read_adc11 = readadc(adc11, SPICLK, SPIMOSI, SPIMISO, SPICS)        read_adc12 = readadc(adc12, SPICLK, SPIMOSI, SPIMISO, SPICS)        read_adc13 = readadc(adc13, SPICLK, SPIMOSI, SPIMISO, SPICS)        read_adc14 = readadc(adc14, SPICLK, SPIMOSI, SPIMISO, SPICS)        read_adc15 = readadc(adc15, SPICLK, SPIMOSI, SPIMISO, SPICS)        # convert analog reading to millivolts = ADC * ( 3300 / 1024 )        millivolts1 = read_adc0 * ( 3300.0 / 1024.0)        millivolts2 = read_adc1 * ( 3300.0 / 1024.0)        millivolts3 = read_adc2 * ( 3300.0 / 1024.0)        millivolts4 = read_adc3 * ( 3300.0 / 1024.0)        millivolts5 = read_adc4 * ( 3300.0 / 1024.0)        millivolts6 = read_adc5 * ( 3300.0 / 1024.0)        millivolts7 = read_adc6 * ( 3300.0 / 1024.0)        millivolts8 = read_adc7 * ( 3300.0 / 1024.0)        millivolts9 = read_adc8 * ( 3300.0 / 1024.0)        millivolts10 = read_adc9 * ( 3300.0 / 1024.0)        millivolts11 = read_adc10 * ( 3300.0 / 1024.0)        millivolts12 = read_adc11 * ( 3300.0 / 1024.0)        millivolts13 = read_adc12 * ( 3300.0 / 1024.0)        millivolts14 = read_adc13 * ( 3300.0 / 1024.0)        millivolts15 = read_adc14 * ( 3300.0 / 1024.0)        millivolts16 = read_adc15 * ( 3300.0 / 1024.0)        time.sleep(5)`

so my problem is I want this while loop to spit out five iterations of these values and take the average value of each millivolts so I would have and than post it to a txt file looking like this:

Average millivolt1, Average millivolt2, Average millivolt3, Average millivolt4, Average millivolt5, Average millivolt6, Average millivolt7, Average millivolt8, Average millivolt9, , Average millivolt10, Average millivolt11, Average millivolt12, Average millivolt13, Average millivolt14, Average millivolt15
Last edited by Mekire on Tue Apr 01, 2014 5:26 am, edited 1 time in total.
Reason: Code tags.
Deltafee

Posts: 14
Joined: Mon Mar 31, 2014 12:04 am

### Re: Menu project

Well, I don't know what you are specifically doing but based on your last post I''ll say this; you almost never want numbered variable names in python. You want to use lists instead.

All of your adc# variables should instead be values in a list (we'll call that list adcs in the code below).

Then your previous code would be something like:
Code: Select all
`read_adcs = [readadc(adc, SPICLK, SPIMOSI, SPIMISO, SPICS) for adc in adcs]millivolts = [read_adc*( 3300.0/1024.0) for read_adc in read_adcs]`

That's it.

-Mek
New Users, Read This
• Use code tags when posting code.
• Include any errors with your post (in code tags).
• Describe your problem; not your chosen solution.
• Make examples the minimum length to demonstrate your issue.

Mekire

Posts: 1711
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

### Re: Menu project

Okay, I'll try to explain everything ':(

I am using a raspyberry pi which is connected to a analog to digital converter, which has 16 values; so to read the values of each pin on both chips require code like this:

read_adc1 = readadc(0, 18, 23, 24, 25)
read_adc2 = readadc(1, 18, 23, 24, 25)
read_adc3 = readadc(2, 18, 23, 24, 25)
read_adc4 = readadc(3, 18, 23, 24, 25)
read_adc5 = readadc(4, 18, 23, 24, 25)
read_adc6 = readadc(5, 18, 23, 24, 25)
read_adc7 = readadc(6, 18, 23, 24, 25)
read_adc8 = readadc(7, 18, 23, 24, 25)

read_adc9 = readadc(0, 18, 23, 24, 8)
read_adc10 = readadc(1, 18, 23, 24, 8)
read_adc11 = readadc(2, 18, 23, 24, 8)
read_adc12 = readadc(3, 18, 23, 24, 8)
read_adc13 = readadc(4, 18, 23, 24, 8)
read_adc14 = readadc(5, 18, 23, 24, 8)
read_adc15 = readadc(6, 18, 23, 24, 8)
read_adc16 = readadc(7, 18, 23, 24, 8)

(Just fyi I don't know how to make this a list)
What I would like to do is to take the average of five iterations of each readout, so for example I am going to use read_adc1. I want it to do a while loop and take values every 10 seconds, five times and then take the average value of all five of them. Then I want it to post on my screen so I can test it.

So just to help you understand the pi is acting as 16 voltmeters with these chips, the reason I am doing this is because I want to create a txt file that is in matrix format; which the first column is the total weight added/removed to something. So I after we get this part done, I would appreciate your help in making a way to tell the program that I wish to add or remove a certain amount of weight. Just so we are on the same page that something that I am adding a remove weight from is a polymer bridge which will have 16 laser diodes lase through the polymer and 16 photodiodes on the opposite of the laser diodes which, they then will be connect to the analog to digital chip. What I am using the matrix txt file as a way to reduce me having to write down everything, so I can take the txt file and upload the data directly to matlab and use that to calculate things. This is why I have a main menu that has the options to name the txt file, add weight, remove weight, calibrate and exit; which the add weight and remove weight have submenus to add or remove certain amount of laboratory weights. Then once I have entered the amount of weight that I have added or removed it then calculates the value of the analog to digital chips, puts the amount of weight that's suppose to be there in the first column and then each read_adc in consecutive order in each column.

That's what I want to do, but I figure starting on the basic thing first taking the average of 5 read_adc every ten seconds. Sorry if its a bit wordy, I hope that helps you understand what I am doing. I would have stayed on the pi forums but I can't ask python programing question on there, so that's why I am here.
Deltafee

Posts: 14
Joined: Mon Mar 31, 2014 12:04 am

### Re: Menu project

No, it is no problem, you are indeed welcome here. Just maybe you should look into some python basics, like lists, or functions, because they may make your life so much easier:

Code: Select all
`def read_values():  millivolts = [0] * 16 # init list with values zero   for i in range(8):    millivolts[i] = readadc(i, 18, 23, 24, 25) * ( 3300.0 / 1024.0)    millivolts[i + 8] = readadc(i, 18, 23, 24, 8) * ( 3300.0 / 1024.0)  return millivoltswhile True:  print read_values()  time.sleep(5)`

Once you get just the right logic down, you can then worry about writing the output to the file instead of your screen.
Due to the reasons discussed here we are moving to python-forum.net on October 1, 2016.

This forum will be closed. Please create an account at the new site to continue discussion.

IRC://irc.freenode.net/python-forum
Kebap

Posts: 689
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

### Re: Menu project

So, after reading some articles; I created a function R()
Code: Select all
`def R():    for i in range(8):    R[i] = 0    R[i + 8] = 0`

Basically I created this so that I have 16 Rs, I am not sure if this is good idea yet. But to go through my thought process I then created a while loop that starts at 1 and goes until five, that sets each readout equal to its corresponding R. So basically to summarizes my thoughts:
Code: Select all
`Readout1 = R1Readout2 = R2...Readout16 = R16`

I am not sure if this the correct way of thinking, but the reason is that I want to have 16 Rs which then I can create a while looping which adds a nth term to each R so again to summarizes:
Code: Select all
`R11, R12, R13, R14, R15`

So to do this I created this while loop:
Code: Select all
`n = 1while (n < 6):    print read_values() = R()[n]       n = n + 1    time.sleep(5)`

So that's my idea of the programing, because what I want to do is create a list for each Rin. that way I can use the average value function of lists; to summarize what I want to do is:
Code: Select all
`listi = [Ri1, Ri2, Ri3, Ri4, Ri5];`

But I am not sure if this is correct.
Last edited by stranac on Tue Apr 15, 2014 9:32 am, edited 1 time in total.
Reason: Corrected quote tags to code tags
Deltafee

Posts: 14
Joined: Mon Mar 31, 2014 12:04 am

### Re: Menu project

Good thing you started looking into loops and functions. Now one thing about this forum, please use the "code" button when you quote your code, not the normal "quote" button. It will be more readable.

How about this for your last ideas:

Code: Select all
`R = []for n in range(6):    R.append() = read_values()    time.sleep(5)print R`

In there:
* I fixed the handling of your list-of-lists, to match your ideas more, and maybe actually work in python
* I changed the while loop into a for loop for saving 2 of their 3 lines
* I added a print command, which I can recommend for debugging and seeing if the ideas match the code.

Please try and run your code yourself then you can see whether you are correct, then you can also paste any error messages here you may receive and can't solve yourself.
Due to the reasons discussed here we are moving to python-forum.net on October 1, 2016.

This forum will be closed. Please create an account at the new site to continue discussion.

IRC://irc.freenode.net/python-forum
Kebap

Posts: 689
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

### Re: Menu project

Well thank you, I was planing to do that this weekend but my pi die so I am going to run it at my college on Monday.
Deltafee

Posts: 14
Joined: Mon Mar 31, 2014 12:04 am

### Re: Menu project

For the code:
Code: Select all
`R = []for n in range(6):    R.append() = read_values()    time.sleep(5)print R`

How do I have it take the average of the lists? Would it be like?
Code: Select all
`def R_Average ()sum (R) / len(R)`
Deltafee

Posts: 14
Joined: Mon Mar 31, 2014 12:04 am

### Re: Menu project

so I tried your code:
Code: Select all
`R = []for n in range(6):    R.append() = read_values()    time.sleep(5)print R`

I edited it so it looks like this:
Code: Select all
`def read_values_mV():  millivolts = [0] * 16 # init list with values zero  for i in range(8):    millivolts[i] = readadc(i, SPICLK, SPIMOSI, SPIMISO, SPICS1) * ( 3300.0 / 1024.0)    millivolts[i + 8] = readadc(i, SPICLK, SPIMOSI, SPIMISO, SPICS2) * ( 3300.0 / 1024.0)  return millivoltsR = [] ## make a list with elements recorded every one secondfor n in range(5):  R.append() = read_values_mV()time.sleep(5)print R`

and I get this error message:
File "./TestBridge.1.py", line 88
R.append() = read_values_mV()
SyntaxError: can't assign to function call
Deltafee

Posts: 14
Joined: Mon Mar 31, 2014 12:04 am

### Re: Menu project

So this code work pretty well:
Code: Select all
`def read_values_mV():  millivolts = [0] * 16 # init list with values zero  for i in range(8):    millivolts[i] = readadc(i, SPICLK, SPIMOSI, SPIMISO, SPICS1) * ( 3300.0 / 1024.0)    millivolts[i + 8] = readadc(i, SPICLK, SPIMOSI, SPIMISO, SPICS2) * ( 3300.0 / 1024.0)  return millivolts`

and I like how it outputs in a list like this:

[a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p]

what I was trying to do with the R function was set it up so I can do this with the readout lists:

First: [a1, b1, c1, d1, e1, f1, g1, h1, i1, j1, k1, l1, m1, n1, o1, p1]
Second: [a2, b2, c2, d2, e2, f2, g2, h2, i2, j2, k2, l2, m2, n2, o2, p2]
Third: [a3, b3, c3, d3, e3, f3, g3, h3, i3, j3, k3, l3, m3, n3, o3, p3]
Fourth: [a4, b4, c4, d4, e4, f4, g4, h4, i4, j4, k4, l4, m4, n4, o4, p4]
Fifth: [a5, b5, c5, d5, e5, f5, g5, h5, i5, j5, k5, l5, m5, n5, o5, p5]

and then add each column together

[(a1+a2+a3+a4+a5), (b1+b2+b3+b4+b5), (c1+c2+c3+c4+c5), (d1+d2+d3+d4+d5), (e1+e2+e3+e4+e5), (f1+f2+f3+f4+f5), (g1+g2+g3+g4+g5), (h1+h2+h3+h4+h5), (i1+i2+i3+i4+i5), (j1+j2+j3+j4+j5), (k1+k2+k3+k4+k5), (l1+l2+l3+l4+l5), (m1+m2+m3+m4+m5), (n1+n2+n3+n4+n5), (o1+o2+o3+o4+o5), (p1+p2+p3+p4+p5)]

and then divide each column by five so I get a list like this:

[(1/5)(a1+a2+a3+a4+a5), (1/5)(b1+b2+b3+b4+b5), (1/5)(c1+c2+c3+c4+c5), (1/5)(d1+d2+d3+d4+d5), (1/5)(e1+e2+e3+e4+e5), (1/5)(f1+f2+f3+f4+f5), (1/5)(g1+g2+g3+g4+g5), (1/5)(h1+h2+h3+h4+h5), (1/5)(i1+i2+i3+i4+i5), (1/5)(j1+j2+j3+j4+j5), (1/5)(k1+k2+k3+k4+k5), (1/5)(l1+l2+l3+l4+l5), (1/5)(m1+m2+m3+m4+m5), (1/5)(n1+n2+n3+n4+n5), (1/5)(o1+o2+o3+o4+o5), (1/5)(p1+p2+p3+p4+p5)]

I apologize that I made it kind of difficult.
Deltafee

Posts: 14
Joined: Mon Mar 31, 2014 12:04 am

### Re: Menu project

Deltafee wrote:Yes, it is pretty close. Of course, you need to watch out for proper syntax and indentation, and actually returning values, etc.
Code: Select all
`def average(r):  return sum(r) / len(r)`

Just want to be explicit about integer (floor) division here too....
Due to the reasons discussed here we are moving to python-forum.net on October 1, 2016.

This forum will be closed. Please create an account at the new site to continue discussion.

IRC://irc.freenode.net/python-forum
Kebap

Posts: 689
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Return to General Coding Help

### Who is online

Users browsing this forum: No registered users and 5 guests