Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Writing to .CSV file on TrinketM0
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Writing to .CSV file on TrinketM0

by assistiveMatt on Tue Jul 21, 2020 5:02 pm

Hello,

I'm really enjoying playing around with the amazing TrinketM0.

I've embarked on a project to use the Trinket as a way of counting the number of times a switch is pressed and record that info along with the time in a .CSV file on the Trinket itself.

I've had some success following the adafruit temp recording tutorial https://learn.adafruit.com/adafruit-trinket-m0-circuitpython-arduino/circuitpython-storage but am having trouble adapting it for my own needs.

Below is my code, I suspect I'm making some beginners mistakes with my syntax. If someone could be so kind as to point out where I'm going wrong I'd really appreciate it.

Code: Select all | TOGGLE FULL SIZE
import time
import board
import digitalio
from digitalio import DigitalInOut, Direction, Pull

led = digitalio.DigitalInOut(board.D13)
led.switch_to_output()
led.direction = Direction.OUTPUT

switchCount = 0

switch = DigitalInOut(board.D2)
switch.direction = Direction.INPUT
switch.pull = Pull.UP

initial_time = time.monotonic()
 
try:
    with open("/log.csv", "a") as fp:
        while True:
            if switch.value:
                led.value = False
            else:
                current_time = time.monotonic()
                time_stamp = current_time - initial_time
                led.value = True
                fp.write("Step Count:", switchCount, ",", int(time_stamp)) # .csv file should add "Step Count" the number of times the switch has been pressed and a time stamp
                fp.flush()
            switchCount += 1 # add +1 to the switch count
            time.sleep(0.2)  # debounce delay
                       
except OSError as e: # this bit flashes the on board LED if there is an issue
    delay = 0.5
    if e.args[0] == 28:
        delay = 0.25
    while True:
        led.value = not led.value
        time.sleep(delay)
Attachments
matt_code_boot.py
boot file
(535 Bytes) Downloaded 1 time
matt_code.py
main file
(1.02 KiB) Downloaded 1 time

assistiveMatt
 
Posts: 2
Joined: Tue Jul 21, 2020 4:50 pm

Re: Writing to .CSV file on TrinketM0

by dastels on Tue Jul 21, 2020 6:51 pm

I see a few things at a glance:

- You need to debounce the switch - that kind of delay doesn't do anything for debouncing, it just sets the rate the switch is sampled at. Have a look at the adafruit_debouncer module. See https://learn.adafruit.com/debouncer-library-python-circuitpython-buttons-sensors for more information.

- You need to look for transitions of the switch value from True to False which indicates it being pressed rather than just whether it's currently pressed (the debouncer module will do that for you).

- You only want to increment the count when you see it having been pressed, not every time through the loop.

Dave

dastels
 
Posts: 3000
Joined: Tue Oct 20, 2015 3:22 pm

Re: Writing to .CSV file on TrinketM0

by assistiveMatt on Wed Jul 22, 2020 4:51 am

Thank you for getting back to me @dastels I'll be sure to improve the debounce using the info you provided.

I should have been more specific in my original post. The main issue I'm having is with the line:
Code: Select all | TOGGLE FULL SIZE
fp.write("Step Count:", switchCount, ",", int(time_stamp))


I want this line to add the words "Step Count:" then the variable 'switch count' followed by the time stamp. All of which in a separate cell within the CSV file.

How does fp.write work and what format should the line of code be written in?

Many thanks,

Matt

assistiveMatt
 
Posts: 2
Joined: Tue Jul 21, 2020 4:50 pm

Re: Writing to .CSV file on TrinketM0

by dastels on Wed Jul 22, 2020 8:32 am

Ah.

I would use String's format() function:

Code: Select all | TOGGLE FULL SIZE
fp.write("Step Count: {0}, {1}".format(switchCount, int(time_stamp)))


Dave

dastels
 
Posts: 3000
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.