mainloop (How to control the curtain raise and down by code) - 2284238y/Team-1-Fernando-Martinez-YI-YI-LianSheng-Liu- GitHub Wiki

In this real-time project, the rising and falling of the curtain is achieved by a main loop code. Pulse width modulation is used to control the rising and falling of curtains. Different pulse widths correspond to different working conditions of curtains. And the pulse width change is achieved by changing the duty cycle.

In the main loop code, input signal and output signal, and PWM are controlled by the GPIO pins on the Raspberry Pi.

The some parts of the main loop code is below:

 GPIO.setmode(GPIO.BOARD)

 GPIO.setup(PIN,GPIO.IN)
 GPIO.setup(PWM_PIN, GPIO.OUT)
 GPIO.setwarnings(False)

 up = False
 down = False

 pwm = GPIO.PWM(PWM_PIN,50)
 pwm.start(14)

 def go_up():
     global up,down,pwm
     if up:
         print("Curtain is already up")
         return
     up = True
     down = False
     pwm.ChangeDutyCycle(14)
     print("Curtains should go up")

 def go_down():
     global up,down,pwm
     if down:
         print("Curtain is already down")
         return
     up = False
     down = True
     pwm.ChangeDutyCycle(2)
     print("Curtains should go down")

The whole code of the main loop can be found in the code.