432111
432111

Reputation: 11

How can I log the exact entry and exit point of a sensor?

I am trying to log the entry of a sensor and then log exactly when the sensor stops detecting. This sensor outputs the value 0 when there is a detection and 1 when there isn't.

I have it so when the code detects a value of 0 it logs that and goes into a while loop. This while loop had a check inside of it to break when there is a value of 1. This way the while loop runs as long as the sensor has a detection (0) and breaks only when the sensor detection ends (1). Before breaking it logs a code so that we now have the entry and exit.

while time.time() - timeBefore < trialDuration - rewardSize: # trialDuration-rewardSize = 20-1 = 19 seconds; time.time()-timeBefore = elapsed time since reward delivery
        print('in outer loop')
        print(GPIO.input(sensorPins[2]))
        if GPIO.input(sensorPins[2]) == 0:
            print('detected')
            print(GPIO.input(sensorPins[2]))
            centrePortFlag = True
            writer.writerow([trialNum, 2, time.time() - startTime])
            print(GPIO.input(sensorPins[2]))
            while time.time() - timeBefore < trialDuration - rewardSize:
                print('in inner loop')
                print(GPIO.input(sensorPins[2]))
                if GPIO.input(sensorPins[2]) == 1:
                    print('broken')
                    print(GPIO.input(sensorPins[2]))
                    writer.writerow([trialNum, 20, time.time() - startTime])
                    break
        if GPIO.input(sensorPins[0]) == 0:
            rewardPortFlag = True
            writer.writerow([trialNum, 0, time.time() - startTime])
            while time.time() - timeBefore < trialDuration - rewardSize:
                if GPIO.input(sensorPins[0]) == 1:
                    writer.writerow([trialNum, 10, time.time() - startTime])
                    break

This is the example of one of such detection loops I have.

Instead of it logging a simple entry and exit, I'm getting constant pairs of 0 and 10 being logged whenever there is a detection.

Trial#,Sensor#,Time
1,0,2.080930709838867
1,10,2.081132411956787
1,0,2.0815231800079346
1,10,2.08174729347229
1,0,2.0821220874786377
1,10,2.0823538303375244
1,0,2.08274245262146
1,10,2.0829689502716064
1,0,2.0833628177642822
1,10,2.0835955142974854
1,0,2.083984613418579
1,10,2.084211826324463
1,0,2.0846076011657715
1,10,2.0848374366760254
1,0,2.0852298736572266
1,10,2.085453987121582
1,0,2.085850477218628
1,10,2.086080312728882
1,0,2.086472988128662
1,10,2.0866963863372803
1,0,2.087092161178589
1,10,2.087322473526001
1,0,2.0877139568328857
1,10,2.0879476070404053
1,0,2.0883352756500244
1,10,2.0885632038116455
1,0,2.088956594467163
1,10,2.08919095993042
1,0,2.0895795822143555
1,10,2.0898053646087646
1,0,2.090200424194336
1,10,2.09043288230896
1,0,2.0908238887786865
1,10,2.0910470485687256
1,0,2.091442823410034
1,10,2.0916738510131836
1,0,2.092064380645752
1,10,2.092289447784424

Upvotes: 0

Views: 32

Answers (0)

Related Questions