Encontré este código para controlar un motor DC en python en raspberry pi pero me detecta un error en while este es el código :
import RPi.GPIO as IO # calling header file which helps us use GPIO’s of PI
import time # calling time to provide delays in program
IO.setwarnings(False) #do not show any warnings
x=0 #integer for storing the duty cycle value
IO.setmode (IO.BCM) #we are programming the GPIO by BCM pin numbers. (PIN35 as‘GPIO19’)
IO.setup(13,IO.OUT) # initialize GPIO13 as an output.
IO.setup(19,IO.IN) # initialize GPIO19 as an input.
IO.setup(26,IO.IN) # initialize GPIO26 as an input.
p = IO.PWM(13,100) #GPIO13 as PWM output, with 100Hz frequency
p.start(0) #generate PWM signal with 0% duty cycle
while 1: #execute loop forever
p.ChangeDutyCycle(x) #change duty cycle for changing the brightness of LED.
if(IO.input(26) == False): #if button1 is pressed
if(x<50):
x=x+1 #increment x by one if x<50
time.sleep(0.2) #sleep for 200ms
if(IO.input(19) == False): #if button2 is pressed
if(x>0):
x=x-1 #decrement x by one if x>0
time.sleep(0.2) #sleep for 200ms
y esta es la pagina de donde lo saque :
https://circuitdigest.com/microcontroll ... spberry-pi
me podrían decir que tengo que cambiar para que funcione o por que no lo hace por favor m_ _m gracias
me pueden ayudar con un código?
- cpa
- Pi Beta
- Mensajes: 159
- Registrado: 10 Mar 2017, 23:18
- Agradecido: 0
- Agradecimiento recibido: 1 vez
Pues precisamente es lo que tienes mal. Tienes que tabular/indentar el contenido que se encuentra dentro del bucle...
¿qué parte de código está dentro del "while 1:". Supongo que todo lo de abajo, ergo tendrás que tabularlo, si no, no se ejecutará nada, ya que asumirá que no hay nada en el bucle while...
¿qué parte de código está dentro del "while 1:". Supongo que todo lo de abajo, ergo tendrás que tabularlo, si no, no se ejecutará nada, ya que asumirá que no hay nada en el bucle while...
- cpa
- Pi Beta
- Mensajes: 159
- Registrado: 10 Mar 2017, 23:18
- Agradecido: 0
- Agradecimiento recibido: 1 vez
Prueba así:
Si programas en Windows, utiliza el Notepad++, que el sólo te marca el contenido de condicionales e iterativos. Si programas en Linux hay también muchas herramientas de edición sencillas de código... En mac ni idea...
Código: Seleccionar todo
import RPi.GPIO as IO # calling header file which helps us use GPIO’s of PI
import time # calling time to provide delays in program
IO.setwarnings(False) #do not show any warnings
x=0 #integer for storing the duty cycle value
IO.setmode (IO.BCM) #we are programming the GPIO by BCM pin numbers. (PIN35 as‘GPIO19’)
IO.setup(13,IO.OUT) # initialize GPIO13 as an output.
IO.setup(19,IO.IN) # initialize GPIO19 as an input.
IO.setup(26,IO.IN) # initialize GPIO26 as an input.
p = IO.PWM(13,100) #GPIO13 as PWM output, with 100Hz frequency
p.start(0) #generate PWM signal with 0% duty cycle
while 1: #execute loop forever
p.ChangeDutyCycle(x) #change duty cycle for changing the brightness of LED.
if(IO.input(26) == False): #if button1 is pressed
if(x<50):
x=x+1 #increment x by one if x<50
time.sleep(0.2) #sleep for 200ms
if(IO.input(19) == False): #if button2 is pressed
if(x>0):
x=x-1 #decrement x by one if x>0
time.sleep(0.2) #sleep for 200ms
- cpa
- Pi Beta
- Mensajes: 159
- Registrado: 10 Mar 2017, 23:18
- Agradecido: 0
- Agradecimiento recibido: 1 vez
Pero, ¿qué error te da exactamente?, porque es que con los datos que aportas es imposible poder ayudarte. Aparentemente el código está bien, salvo que tengas problemas con las librerías que importa...