While loop issue with multiple conditions

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
707a
 
Posts: 8
Joined: Thu Jun 25, 2020 1:36 pm

While loop issue with multiple conditions

Post by 707a »

Hello All,

I'm having an issue with a simple While loop and I don't understand why. I have two triggers to start my sequence ("pe" a Pull.UP input and "fob" a Pull.DOWN input). Either one will work on its own, but when I try using an OR neither work. I'm still very much a novice and have a lot to learn, but I've used multiple AND and OR conditions in While loops before without any issue. I'm not sure what the difference is.

Any help would be greatly appreciated.





My issue is about halfway down and starts at the "# Start signal" comment. while pe.value: and while not fob.value work, but combining them like while pe.value or not fob.value: doesn't work.

Code: Select all

import time
import board
import neopixel
import digitalio
import pulseio
import pwmio
from audiocore import WaveFile
from adafruit_motor import servo
from audiomp3 import MP3Decoder
from digitalio import DigitalInOut, Direction, Pull

try:
    import urandom as random
except ImportError:
    import random

try:
    from audioio import AudioOut
except ImportError:
    try:
        from audiopwmio import PWMAudioOut as AudioOut
    except ImportError:
        pass  # not always supported by every board!


#IO
pe = digitalio.DigitalInOut(board.D11)                              # Photo Eye to Start 
pe.direction = Direction.INPUT
pe.pull = Pull.UP

fob = digitalio.DigitalInOut(board.D6)                              # Remote Control to Start
fob.direction = Direction.INPUT
fob.pull = Pull.DOWN

pwm = pulseio.PWMOut(board.D4, duty_cycle= 2 ** 15, frequency=50)   # Configures pin A3 to be PWM
my_servo = servo.Servo(pwm)                                         # Configures "pwm" to be a servo, my_servo

vib1 = pulseio.PWMOut(board.D12, duty_cycle=0, frequency=5000)      # Configures pin D12 to be PWM


#NeoPixels
numpix = 32                                                         # Number of NeoPixels
pixpin = board.D5                                                   # Pin where NeoPixels are connected
pixels = neopixel.NeoPixel(pixpin, numpix, brightness=0.0)

red = (255, 0, 0)
off = (0, 0, 0)

colors = [
    [255, 40, 0],
    [255, 30, 0],
    [255, 10, 0],
    [255, 0, 0],
]
embedded_led = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.0) # Embedded NeoPixel in the Feather
embedded_led[0] = (red)         

# Audio
mp3files = ["fire1.mp3", "fire2.mp3"]
# You have to specify some mp3 file when creating the decoder
mp3 = open(mp3files[0], "rb")
decoder = MP3Decoder(mp3)
audio = AudioOut(board.A0)

# Main loop
while True:

    for filename in mp3files:
        decoder.file = open(filename, "rb")
        audio.play(decoder)                                 # Starts playing the audio file
        print("playing", filename)

    # This allows you to do other things while the audio plays!
    t = time.monotonic()
    while time.monotonic() - t < 0:                         # "0" prohibits playing at startup
        pass

    audio.pause()                                           # Audio file is puased instantly
    print("Waiting for trigger")
    
    while pe.value:                                         # Start signal
        pass                                                
#    while not fob.value:
#        pass
#    while pe.value or not fob.value:                                         
#        pass   
#    while not fob.value or pe.value:
#        pass

    for angle in range (0, 80, 1):                          # Moves servo from 0° to 80° one degree at a time
        my_servo.angle = angle                              # Counts by 1 and moves to position
        time.sleep(.05)                                     # Speed (0.05 works well)
        print(angle)                                        # Angle Readout

    audio.resume()                                          # Audio file resumes playing
    vib1.duty_cycle = 30000                                 # Vibration motor starts
    pixels.fill(red)                                        # Turns all the neopixels red
    for x in range(33):                                     # The higher the range the longer the cycle
            rest = [0.01, 0.02, 0.03, 0.04, 0.05, 0.06,
            0.07, 0.08, 0.09]                               # Used for the random flicker
            c = random.randint(0, len(colors) - 2)          # Choose random color index
            pixelnum = random.randint(0, numpix - 2)        # Choose random pixel
            pixels[pixelnum] = colors[c]                    # Set pixel to color
            for i in range(1, 5):
                pixels.brightness = i / 5.0                 # Ramp up brightness
                pixels.write()
                time.sleep(random.choice(rest))             # Random flicker
            for i in range(5, 1, -1):
                pixels.brightness = i/ 2 / 5.0              # Ramp down brightness
                pixels.write()
                time.sleep(random.choice(rest))             # Random flicker

    pixels.fill(off)                                        # Turns off all the neopixels
    vib1.duty_cycle = 0                                     # Vibration motor stops
    while audio.playing:
        pass

    for angle in range (80, 0, -1):                         # Moves servo from 80° to 0° negative one degree at a time
        my_servo.angle = angle                              # Counts by 1 and moves to position
        time.sleep(.05)                                     # Speed
        print(angle)                                        # Angle Readout

 
Hardware:
Feather M4 Express PID: 3857
Adalogger FeatherWing RTC + SD PID: 2922
Simple RF M4 Receiver - 315MHz Momentary Type PID: 1096
Sharp GP2Y0D805Z0F PID: 3025

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: While loop issue with multiple conditions

Post by dastels »

So you want to spin until there's a trigger?

pe is True when not triggering, and fob is False when not triggering, so to spin while neither is triggered, you want:

Code: Select all

while pe.value and not fob.value:
    pass
Have a look at https://learn.adafruit.com/binary-boole ... lean-logic for a bit more background.

Dave

User avatar
707a
 
Posts: 8
Joined: Thu Jun 25, 2020 1:36 pm

Re: While loop issue with multiple conditions

Post by 707a »

Thanks Dave,

Of course, that makes perfect sense. I appreciate the quick response (and the link).


Cheers,

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”