0

New to CircuitPython (and programming) two break beam sensor
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

New to CircuitPython (and programming) two break beam sensor

by s_k on Sun Oct 06, 2019 5:40 pm

I am completely new to programming in CircuitPython. All I ever did before is a few short scripts within a database (the database app Panorama by ProVUE) that ran in response to a button or data entry. There may have been the occasional loop inside one of my scripts, but the script started, ran and then stopped. The CircuitPython "while True:" forever loop baffles me. My code is lifted and modified from "Welcome to CircuitPython", "Essentials of", "Push Buttons", "Servos" and "IR Break Beam Sensors".

Feather M4 Express, two IR break beam sensors, 1 micro servo. I want the servo to move to 180 degrees if *either* of the two IR break beam sensors are interrupted.

Under the "white True" loop if I comment out either of the two "if" statements then the respective irinsideA5 *or* iroutsideA6 "if" statement works. Breaking the respective IR sensor beam of the active "if" statement will cause the servo to move to the 180 degree position. But I cannot figure out the code to have *either* sensor when the beam is broken cause the servo to move to 180 degrees. Either nothing happens or the poor servo rattles and buzzes unless I break *both* beams. I have tried half a dozen variations of the code under the "while True:" loop without success. What am I missing? Please help. Thank you.

Code: Select all | TOGGLE FULL SIZE
# Write your code here :-)
# CircuitPython IO demo #1 - General Purpose I/O
import time
import board
import pulseio
from digitalio import DigitalInOut, Direction, Pull
from adafruit_motor import servo
# create a PWMOut object on Pin A2. # sk make it pin 3 for M4
pwm = pulseio.PWMOut(board.A3, duty_cycle=2 ** 15, frequency=50)

# Create a servo object, my_servo.
my_servo = servo.Servo(pwm)
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT

irinsideA5 = DigitalInOut(board.D5)  # For Feather M0 Express, M4 Express
irinsideA5.direction = Direction.INPUT
irinsideA5.pull = Pull.UP

iroutsideA6 = DigitalInOut(board.D6)  # For Feather M0 Express, M4 Express
iroutsideA6.direction = Direction.INPUT
iroutsideA6.pull = Pull.UP
# my_servo.angle = 0.06
# sk thought I might need  to "initialize" but not necessary?

while True:
    # We could also do "led.value = not switch.value"!
    if irinsideA5.value:
        led.value = True
        my_servo.angle = 179
        print(my_servo.angle)
    else:
        led.value = False
        my_servo.angle = 0.06
        print(my_servo.angle)
    if iroutsideA6.value:
        led.value = True
        my_servo.angle = 179
        print(my_servo.angle)
    else:
        led.value = False
        my_servo.angle = 0.06
        print(my_servo.angle)

    time.sleep(0.01)  # debounce delay

s_k
 
Posts: 18
Joined: Wed Oct 10, 2018 2:39 pm

Re: New to CircuitPython (and programming) two break beam se

by dastels on Sun Oct 06, 2019 8:02 pm

Try:
Code: Select all | TOGGLE FULL SIZE
if irinsideA5.value or iroutsideA6.value:
    led.value = True
    my_servo.angle = 179
    print(my_servo.angle)
else:
    led.value = False
    my_servo.angle = 0.06
    print(my_servo.angle)

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

Re: New to CircuitPython (and programming) two break beam se

by s_k on Tue Oct 08, 2019 10:31 pm

Thank you, dastels, for your reply. I tried your code suggestion. It eliminated the buzzing and twitching of the servo when it sat at 0 degrees, but the servo would still not move to the 180 degree position if just one or the other beam was broken. Only when both beams were broken simultaneously.

However, when I changed:
Code: Select all | TOGGLE FULL SIZE
if irinsideA5.value or iroutsideA6.value:

to:
Code: Select all | TOGGLE FULL SIZE
if irinsideA5.value and iroutsideA6.value:

then it worked as I hoped. No buzzing and twitching at the 0 degree position and breaking just one beam causes the servo to go to the 180 degree position. I do not know what made me replace "or" with "and". I was just flailing. It does not make sense. Doesn't the "or" in that line mean that either condition should suffice; and doesn't "and" mean that both conditions must be met? If anyone can explain or find some other mistake I have made I would be most grateful. Thank you.

s_k
 
Posts: 18
Joined: Wed Oct 10, 2018 2:39 pm

Re: New to CircuitPython (and programming) two break beam se

by dastels on Wed Oct 09, 2019 10:43 am

Sounds like the result of value is active low. The result of AND is True when both are True. Said another way: the result of AND is False when either/both are False.

Dave

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

Please be positive and constructive with your questions and comments.