1

I'm developing a datalogger using a Raspberry Pi 3B+. I have 2 separate square wave signals connected to two separate GPIO pins and some code to record and store the date and time of each transition of the wave (low-high/high-low). My code waits for a signal to be received and then runs the rest of the script. However if one of the two signals stops then the program stops running for both GPIO signals. Therefore I'm wondering how I can pause (then wait for a signal to be received again) my script for one GPIO pin when the signal is stopped on that pin, but keep running the script on the other GPIO pin which has the signal. My code is shown below. Thanks

import RPi.GPIO as GPIO
import datetime
from datetime import timedelta, datetime
import time
from time import sleep
import csv
from csv import writer
import signal


datestring = datetime.strftime(datetime.now(), '%Y_%m_%d-%H_%M_%S')

def my_callback(channel):
     data_writer.writerow([GPIO.input(26), datetime.now()])      # writes the detected edge event to a CSV file with columns of rise/fall and the date & time of detection

def my_callback2(channel):
     data_writer_second.writerow([GPIO.input(40), datetime.now()]) 


if __name__ == '__main__':

    GPIO.setmode(GPIO.BOARD)       # set up GPIO numbering (BOARD = physical pins on Pi)
    GPIO.setup(26, GPIO.IN)     # set physical pin 26 as input
    GPIO.setup(40, GPIO.IN)

# create CSV file to write edge detection data to called Datalogger, with data on new line
    with open('/media/pi/KINGSTON/FGS/Datalogger_' + datestring + '.csv', 'w', newline='') as Datalogger, open('/media/pi/KINGSTON/FGS 2/DataloggerFGS2_' + datestring + '.csv', 'w', newline='') as Data_logger:
        data_writer = writer(Datalogger)
        data_writer_second = writer(Data_logger)

        data_writer.writerow(["Edge type (0 = Falling)", "Date & Time"])       # create headers in CSV file
        data_writer_second.writerow(["Edge type (0 = Falling)", "Date & Time"])



        GPIO.add_event_detect(26, GPIO.BOTH, callback=my_callback, bouncetime=5)     # edge detection function
             # detects both rise and fall in signal on pin 26, then runs my callback
        GPIO.add_event_detect(40, GPIO.BOTH, callback=my_callback2, bouncetime=5)

        signal.pause()


        while True:
     # main program loop here - loops detection of edges then runs my callback

          time.sleep(0.0001)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy