this is my code the I am currently writing for a robot in my university project. This code works, however the loop will constantly print statements every second and I would like it to only print when I change the input condition (break the if condition), so it wouldn't keep on printing. Is there anyway to fix this? Thanks for the help in advance.
PS: this is in python 2.7 (I think)
#!/usr/bin/env python
# import modules
import RPi.GPIO as GPIO
import time
import pygame
import os
# def init():
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# motor a
GPIO.setup(17, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
# motor b
GPIO.setup(22, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
p1 = GPIO.PWM(27, 100)
p2 = GPIO.PWM(24, 100)
p1.start(0)
p2.start(0)
pygame.init()
while pygame.joystick.get_count() == 0:
print ('Waiting for joystick count =%i ' % pygame.joystick.get_count())
time.sleep(10)
pygame.joystick.init()
joystick = pygame.joystick.Joystick(0)
joystick.init()
print ('initialized Joystick : %s' % joystick.get_name())
numaxes = joystick.get_numaxes()
numbuttons = joystick.get_numbuttons()
try:
while True:
pygame.event.pump()
UpdateMotors = 0
# Move forwards
if 0.01 < joystick.get_axis(1) <= 0.25:
print ('moving backward with 25% speed')
# init()
GPIO.output(17, True)
GPIO.output(18, False)
p1.ChangeDutyCycle(25)
#GPIO.cleanup()
GPIO.output(22, False)
GPIO.output(23, True)
p2.ChangeDutyCycle(25)
elif 0.25 < joystick.get_axis(1) <= 0.5:
print ('moving forward with 50% speed')
# init()
GPIO.output(17, True)
GPIO.output(18, False)
p1.ChangeDutyCycle(50)
#GPIO.cleanup()
GPIO.output(22, False)
GPIO.output(23, True)
p2.ChangeDutyCycle(50)
elif 0.5 < joystick.get_axis(1) <= 0.75:
print ('moving backward with 75% speed')
# init()
GPIO.output(17, True)
GPIO.output(18, False)
p1.ChangeDutyCycle(75)
#GPIO.cleanup()
GPIO.output(22, False)
GPIO.output(23, True)
p2.ChangeDutyCycle(75)
Aucun commentaire:
Enregistrer un commentaire