Python Forum
Threading and appending arrays
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Threading and appending arrays
#1
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Concurrent futures threading running at same speed as non-threading billykid999 13 1,716 May-03-2023, 08:22 AM
Last Post: billykid999
  Tutorials on sockets, threading and multi-threading? muzikman 2 2,076 Oct-01-2021, 08:32 PM
Last Post: muzikman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020