Python Forum
Threading and appending arrays - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Threading and appending arrays (/thread-29250.html)



Threading and appending arrays - hairem - Aug-24-2020

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:
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()
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.