from machine import Pin,ADC import time import json b = 0 # To keep track of sample counts passed, initially 0 no_of_samples = 241 # Maximum is 241. sample_tp = 100 #in milliseconds led = Pin(25,Pin.OUT) pot = ADC(Pin(26)) but2rec = Pin(15,Pin.IN) label_list = [] data_list = [] but_pressed = False def create_json(dali,lali): print("Recording over.") chart_data = { "backgroundColor": "white", "width": 800, "height": 300, "format": "png", "chart":{ "type":"line", "data":{ "labels":lali, "datasets":[{"label":'LDR_data',"data":dali,"fill":"false","borderColor":"blue", "borderWidth":"1","pointRadius":1.3,"pointBorderColor":"green"}] } } } with open("adc_data.json","w") as datafile: json.dump(chart_data,datafile) print("JSON file created.") loop = True while loop: if but2rec.value(): but_pressed = True print(but_pressed) led.off() time.sleep_ms(10) while but_pressed and b<=no_of_samples-1: pot_val = pot.read_u16() r_pot_val = round(pot_val/100) led.on() b+=1 data_list.append(r_pot_val) label_list.append(b) print(r_pot_val) time.sleep_ms(sample_tp) led.off() if not b<=no_of_samples-1: create_json(data_list,label_list) loop = False