1

I'm trying to write a python code to control the voltage of the GPIO Pins. The idea behind it is that in a frequency of 1KHz if half of the time the pin is off and half of the time it is on than the voltage will be 50%.

I need to keep the while loop running while waiting for user input so that I can change the speed of the motor.

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)

# The higher the number for speed the slower the motor will rotate
def motor(speed):
    i = 0
    run = True

    # Runs until user tells it to stop
    while run == True:

        # Pins should vary at every second
        time.sleep(0.001)
        i += 1
        if i % speed == 0:
            GPIO.output(4, GPIO.HIGH)
        else:
            GPIO.output(4, GPIO.LOW)

        # This block is causing the issue
        # I need the loop to keep running while waiting for the input
        if i % speed * 10 == 0:
            com = int(input("Enter 0 to quit"))
            if com == -1:
                GPIO.output(4, GPIO.LOW)
                break

    GPIO.output(4, GPIO.LOW)


run = True;
while (run):
    speed = int(input("Select Speed"))

    if speed == 0:
        run = False
    else:
        motor(speed)

GPIO.cleanup()
Meet Taraviya
  • 869
  • 1
  • 8
  • 27
Bruno Illa
  • 11
  • 3

0 Answers0