1

I am attempting to read multiple Reflective IR Sensors continuously until each one is blocked. I am using an Arduino running Standard Firmata and Pythons Pyfirmata library.

Every time I try a standard read without printing the result in a while loop I am getting a 15 to 30 second delay (I cannot figure out why):

Example1 - works but with random unexplained delay:

Sensor1 = board.get_pin('a,0,i') #analogue, pin 0, input mode

while Sensor1.read() != 0: #Sensor defaults to 0.6 V but when blocked 0.0v

   Sensor1.read()

Example2 - works but I now have a bunch of sensor read outputs:

Sensor1 = board.get_pin('a,0,i') #analogue, pin 0, input mode

while Sensor1.read() != 0: #Sensor defaults to 0.6 V but when blocked 0.0v

   Sensor1.read()

   print(Sensor1.read()) # this prints a bunch of read outputs

For some reason when I add the print(Sensor1.read()) I will get an immediate response when the sensor is blocked. But if I remove this portion of code to eliminate the garbage output I get an unexplained time delay in between when the sensor is blocked and when it is recognized by the code and moves on. What I would like to do is constantly read the sensor without printing that read and get an immediate response of breaking the while loop once the sensor is blocked and produces the 0.0v. I believe that I also have the option of suppressing the print outputs for these while loops but I want to know if there is an alternative? Thank you so much for reviewing this question and thanks a million for any help!

  • why do you read a light barrier ( a switch) with an analog input? are you sure that the value is always 0.0000? – Piglet Oct 23 '19 at 06:31
  • I was getting an object type error when attempting to run it through the digital inputs. But, I think I overcame that with the analog and might merit looking back into the digital input method.... I will update with more information once tested :).... thanks for the help – user12260192 Oct 23 '19 at 18:03

2 Answers2

0

The read() might not always be exact 0. Maybe use a threshold value, for example:

while Sensor1.read() > 10:

Another solution would be to use interrupts and set flags, so you don't have to do the polling (and could put the device to sleep).

Danny_ds
  • 11,201
  • 1
  • 24
  • 46
  • Thank you! So the Sensor will return a value of .60 ~ .80 while not blocked but once the sensor is blocked it returns a true zero of 0.00 and the sensor is only blocked for an instant and the objective would then be to activate the next sensor and have it trip almost instantly as well. Again, the code works perfectly fine with the print but delays without the print. I really dont understand why the threshold would matter because the true 0.00 works great when printing the read. But unfortunately when I am not printing I have no visibility into what is happening (because it is not printing). – user12260192 Oct 23 '19 at 15:46
  • I may try using a flag... with an if statement within the loop and maybe that will help... I will update once I have further information :) thanks for the help – user12260192 Oct 23 '19 at 15:49
  • @user12260192 When you use print() you do an extra read(). For testing try an extra read() without print() and a print() without an extra read() (i.e. save the value of the last read and print that) to see what's happening. – Danny_ds Oct 24 '19 at 10:36
0

I know i'ts an old topic, but I ran into this problem with pyfirmata a few days ago, and today I solved it simply by printing a new line with the end= parameter, it creates a new line, but the following prints stays on the same line.

Sensor1 = board.get_pin('a,0,i')

while Sensor1.read() != 0:
   Sensor1.read()
   print('', end='')
Sapo
  • 1