diff --git a/read_arduino.py b/read_arduino.py index 7f98b90..52c92b7 100644 --- a/read_arduino.py +++ b/read_arduino.py @@ -32,7 +32,15 @@ def read(): # open the file and add a line of header to it, then close f = open(file_name, "a", newline="", encoding="utf-8") writer = csv.writer(f) - header = ['Time', 'Resistance'] + # headers based on number of ports used + if len(sensor_ports) == 1: + header = ['Time', 'Resistance'] + if len(sensor_ports)== 2: + header = ['Time', 'Resistance1', 'Resistance2'] + if len(sensor_ports) == 3: + header = ['Time', 'Resistance1', 'Resistance2', 'Resistance3'] + if len(sensor_ports) == 4: + header = ['Time', 'Resistance1', 'Resistance2', 'Resistance3', 'Resistance4'] writer.writerow(header) f.close() while True: @@ -40,9 +48,14 @@ def read(): # take only the nonzero indices, and truncated to two decimal places to "filter" out some hardware errors dat_sel = np.trunc((np.take(dat_list, sensor_ports) / 1000) * 10**2) / 10**2 r_arr = np.take(refRes, sensor_ports) * (v_in / dat_sel - 1) # *2 <-- change with actual formula for ammonia concentration + # created a new array to convert resistance values to sci notation + r_arr2 = np.empty(len(r_arr), dtype=object) + for i in range(len(r_arr)) : + a = np.format_float_scientific(r_arr[i], precision = 2) + r_arr2[i] = a # write + export values as .csv format # converted resistance values in array to scientific notation - dat = f", ".join(np.insert(np.format_float_scientific(r_arr.astype(str)), 0, datetime.now().strftime('%H:%M:%S'))) + dat = f", ".join(np.insert(r_arr2.astype(str), 0, datetime.now().strftime('%H:%M:%S'))) #np.format_float_scientific() print(dat) f = open(file_name, "a", newline="", encoding="utf-8") f.write(dat + '\n') @@ -55,7 +68,15 @@ def read(): # open the file and add a line of header to it, then close f = open(file_name, "a", newline="", encoding="utf-8") writer = csv.writer(f) - header = ['Time', 'Resistance'] + # headers based on number of ports used + if len(sensor_ports) == 1: + header = ['Time', 'Resistance'] + if len(sensor_ports)== 2: + header = ['Time', 'Resistance1', 'Resistance2'] + if len(sensor_ports) == 3: + header = ['Time', 'Resistance1', 'Resistance2', 'Resistance3'] + if len(sensor_ports) == 4: + header = ['Time', 'Resistance1', 'Resistance2', 'Resistance3', 'Resistance4'] writer.writerow(header) f.close() while controller.isOpen(): @@ -70,9 +91,14 @@ def read(): # take only the nonzero indices, and truncated to two decimal places to "filter" out some hardware errors dat_sel = np.trunc((np.take(dat_list, sensor_ports) / 1000) * 10**2) / 10**2 r_arr = np.take(refRes, sensor_ports) * (v_in / dat_sel - 1) # *2 <-- change with actual formula for ammonia concentration + # created a new array to convert resistance values to sci notation + r_arr2 = np.empty(len(r_arr), dtype=object) + for i in range(len(r_arr)) : + a = np.format_float_scientific(r_arr[i], precision = 2) + r_arr2[i] = a # write + export values as .csv format # converted resistance values in array to scientific notation - dat = f", ".join(np.insert(np.format_float_scientific(r_arr.astype(str)), 0, datetime.now().strftime('%H:%M:%S'))) + dat = f", ".join(np.insert(r_arr2.astype(str), 0, datetime.now().strftime('%H:%M:%S'))) print(dat) f = open(file_name, "a", newline="", encoding="utf-8") f.write(dat + '\n')