Python Forum
(Python help) Change in logic not breaking 'while' loop?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
(Python help) Change in logic not breaking 'while' loop?
#1
Hello, I am currently a beginner at Python coding thus I apologise if there is a basic/obvious solution to this. I want to code a program that runs 2 motors forwards simultaneously for 3 seconds, then backwards for 3 seconds. In addition, I've added a photoelectric sensor (WT160-F172) that will act as a collision detector for the motors. I can only get the sensor to stop the motors and continue running forwards (how i want it to work) when the loop is infinite (no time.sleep) . When i make it the finite loop i want (3 seconds front and 3 second back), i do not know how to get the sensor to function during the loop, the sensor only affects the motors between two said finite loops. I'm on python 3 and raspberry pi 3.

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(12,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(15,GPIO.OUT)

try:
	while True:
		print("Sensor Value:")
		value = GPIO.input(7)
		print(value)
		if (value == 0):
			print("Move forward")
			GPIO.output(11,False)
			GPIO.output(12,True)
			GPIO.output(13,True)
			GPIO.output(15,False)
			time.sleep(3)
			print("Move backward")
			GPIO.output(11,True)
			GPIO.output(12,False)
			GPIO.output(13,False)
			GPIO.output(15,True)
			time.sleep(3)
		elif (value == 1):
			print("Obstacle detected")
			GPIO.output(11,False)
			GPIO.output(12,False)
			GPIO.output(13,False)
			GPIO.output(15,False)
except KeyboardInterrupt:
	pass
finally:
	GPIO.cleanup()
There is evidently a wrong usage of the loop functions in this code thus any help would be appreciated.
Reply
#2
Better way (untested):
import RPi.GPIO as GPIO
import time

def init():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(7,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
    GPIO.setup(11,GPIO.OUT)
    GPIO.setup(12,GPIO.OUT)
    GPIO.setup(13,GPIO.OUT)
    GPIO.setup(15,GPIO.OUT)

def move_forward():
    print("Move forward")
    GPIO.output(11,False)
    GPIO.output(12,True)
    GPIO.output(13,True)
    GPIO.output(15,False)
    time.sleep(3)

def move_backward():
    GPIO.output(11,True)
    GPIO.output(12,False)
    GPIO.output(13,False)
    GPIO.output(15,True)
    time.sleep(3)

def start()
    init()
    try:
        while True:
            move_forward()
            move_backward()
    except KeyboardInterrupt:
        pass
    finally:
        GPIO.cleanup()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ChromeDriver breaking Python script genericusername12414 1 218 Mar-14-2024, 09:39 AM
Last Post: snippsat
  Openpyxl module breaking my code EnderSM 5 986 May-26-2023, 07:26 PM
Last Post: snippsat
  Multiply and Addition in the same loop statement with logic. joelraj 2 999 Feb-02-2023, 04:33 AM
Last Post: deanhystad
  Python Logic Error rmfooty 3 904 Dec-05-2022, 01:44 PM
Last Post: rmfooty
  logic python JunkBoy 5 1,507 Aug-01-2022, 05:13 AM
Last Post: JunkBoy
  breaking out of nested loops Skaperen 3 1,175 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  How to use += after breaking for loop? Frankduc 2 1,446 Dec-31-2021, 01:23 PM
Last Post: Frankduc
  How to fix while loop breaking off raphy11574 3 2,126 Oct-12-2021, 12:56 PM
Last Post: deanhystad
  breaking a large program into functions, not acting as expected Zane217 9 2,932 Sep-18-2021, 12:37 AM
Last Post: Zane217
  Need to run 100+ Chrome’s with Selenium but can’t get past ~15 without breaking imillz 0 1,322 Sep-04-2021, 04:51 PM
Last Post: imillz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020