datalogger.py 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. # datalogger.py
  2. # Logs the data from the acceleromter to a file on the SD-card
  3. import pyb
  4. # creating objects
  5. accel = pyb.Accel()
  6. blue = pyb.LED(4)
  7. switch = pyb.Switch()
  8. # loop
  9. while True:
  10. # wait for interrupt
  11. # this reduces power consumption while waiting for switch press
  12. pyb.wfi()
  13. # start if switch is pressed
  14. if switch():
  15. pyb.delay(200) # delay avoids detection of multiple presses
  16. blue.on() # blue LED indicates file open
  17. log = open('/sd/log.csv', 'w') # open file on SD (SD: '/sd/', flash: '/flash/)
  18. # until switch is pressed again
  19. while not switch():
  20. t = pyb.millis() # get time
  21. x, y, z = accel.filtered_xyz() # get acceleration data
  22. log.write('{},{},{},{}\n'.format(t,x,y,z)) # write data to file
  23. # end after switch is pressed again
  24. log.close() # close file
  25. blue.off() # blue LED indicates file closed
  26. pyb.delay(200) # delay avoids detection of multiple presses