I am trying to run a thread that will read in voltages while an adjustment is made over time to obtain an average voltage, but it doesn't look like the array in the voltage reading is working and I am not sure what I am doing wrong and could really use a second set of eyes. Here's part of my program:
I want the voltage collection to occur while the output from the Function Warm_UP is running. I am starting here for a larger project but I think I am just missing something. I am using global variable to both trigger the events of the thread as well as pass variable between the thread and the Warm_UP function, I know this is not considered good practice but it seemed like an easy way to do things for now.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
c = [] status = 0 b = 0.0 def Warm_UP(): global status start = time.time() NOW = start while (NOW - start) < 300 : setpoint = random.randint( 122 , 4090 ) dac.raw_value = setpoint status = 1 print ( str ( round (setpoint * ( 5.00 / 4096 ), 4 )) + " :current setpoint" ) print ( str ( round (adc.read_adc_difference( 3 , gain = GAIN) * 0.0001875 , 4 ))) print (c) time.sleep( 60 ) NOW = time.time() status = 3 time.sleep( 1 ) print ( "average: " + str (b)) time.sleep( 5 ) dac.raw_value = 0 def Average(): global status global b global c a = [] while status = = 1 : reading = (adc.read_adc_difference( 3 , gain = GAIN) * 0.0001875 ) a.append(reading) c = np.array(a) while status = = 3 : print (c) b = np.average(c) print (b) while status = = 0 : print ( "waiting" ) time.sleep( 0.5 ) if __name__ = = "__main__" : x = threading.Thread(target = Average) x.start() Warm_UP() x.stop() |