Pi Pico Project Enhancement

Please consider the following Project Enhancement:

For projects that have a sensor, consider capturing the data from the sensor to a file. For example, for the Laser Sight project, the code was modified to write the Aimed Time, the date and the time to file. This should encourage users to spend more time with a project to accumulate data to analyze, e.g., determine their average Aim Time or plot the Aim Time over Date and Time. The code for the Laser Sight Project:

from machine import Pin,ADC,PWM

from time import sleep

import utime as time

rtc = machine.RTC()

timestamp=rtc.datetime()

timestring="%04d-%02d-%02d %02d:%02d:%02d"% (timestamp[0:3] + timestamp[4:7])


photo = 0              # ADC0 multiplexing pin is GP26

LaserPin = 15          #Red laser transmitter

Led_R = PWM(Pin(11))

Led_G = PWM(Pin(12))

Led_B = PWM(Pin(13))

Led_R.freq(2000)  

Led_G.freq(2000)  

Led_B.freq(2000)  


def setup():

   global Laser

   global photo_ADC

   photo_ADC = ADC(photo)

   Laser = Pin(LaserPin,machine.Pin.OUT)

   Laser.value(0)


def loop():

   aim_time = 0

   while True: 

       Laser.value(1)

       Led_R.duty_u16(0)

       Led_G.duty_u16(65535)

       Led_B.duty_u16(0)


       print("time:","{:.1f}".format(aim_time))




       sleep(0.1)

       aim_time += 0.1

       if photo_ADC.read_u16() < 15000:    # Hit by a red laser, the light flashes

           print("Aimed:")

           print ("{:.1f}".format(aim_time))    

           file = open ("laser sight.txt", "a")

           print(type(file))

           results = "Aim time: " + str("{:.1f}".format(aim_time)) +" (in seconds) " + "Date and Time: " + timestring + "\n"

           file.write(results)

           file.close()

           aim_time = 0

           for i in range(10):

               Led_R.duty_u16(65535)      # red light

               Led_G.duty_u16(0)

               Led_B.duty_u16(0)

               sleep(0.1) 

               Led_R.duty_u16(0)          

               Led_G.duty_u16(0)

               Led_B.duty_u16(65535)      # blue light

               sleep(0.1)

           sleep(2)



import os

os.listdir()

if __name__ == '__main__':

   setup()

   loop() 

Sign In or Register to comment.