Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Ex 1
- import RPi.GPIO as GPIO
- import time
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP) #button
- GPIO.setup(11,GPIO.OUT)
- GPIO.output(11,GPIO.LOW)
- button=0 #up
- light=0 #off
- while True:
- if (button==0 and light==0):
- if GPIO.input(12) == False: #button is pressed
- GPIO.output(11, GPIO.HIGH)
- button=1
- light=1
- elif (button==1 and light==0):
- if GPIO.input(12):
- button=0
- elif (button==0 and light==1):
- if GPIO.input(12)==False:
- GPIO.output(11, GPIO.LOW)
- button=1
- light=0
- elif (button==1 and light==0):
- if GPIO.input(12):
- button=0
- sleep(0.2)
- GPIO.cleanup()
- Ex 2
- import RPi.GPIO as GPIO
- import time
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP) #button
- GPIO.setup(11,GPIO.OUT)
- GPIO.output(11,GPIO.LOW)
- button=0 #up
- light=0 #off
- def callback_fct(pin):
- if (button==0 and light==0):
- if GPIO.input(12) == False: #button is pressed
- GPIO.output(11, GPIO.HIGH)
- button=1
- light=1
- elif (button==1 and light==0):
- if GPIO.input(12):
- button=0
- elif (button==0 and light==1):
- if GPIO.input(12)==False:
- GPIO.output(11, GPIO.LOW)
- button=1
- light=0
- elif (button==1 and light==0):
- if GPIO.input(12):
- button=0
- GPIO.add_event_detect(12, GPIO.BOTH, callback=button_callback, bouncetime=100)
- try:
- time.sleep(300)
- finally:
- GPIO.cleanup()
- Ex 3
- import RPi.GPIO as GPIO
- import time
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP) #button
- GPIO.setup(11,GPIO.OUT)
- pwm = GPIO.PWM(11, 50)
- pwm.start(10)
- var=10
- while True:
- if GPIO.input(12) == False
- var=var+10
- pwm.ChangeDutyCycle(var)
- sleep(0.2)
- GPIO.cleanup()
- ex 4
- import RPi.GPIO as GPIO
- import time
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(11,GPIO.OUT)
- pwm = GPIO.PWM(11, 50)
- pwm.start(0)
- while True:
- for i in range(0, 103, 3):
- pwm.ChangeDutyCycle(i)
- sleep(0.02)
- for i in range(100, -3, -3):
- pwm.ChangeDutyCycle(i)
- sleep(0.02)
- GPIO.cleanup()
- Ex 5
- import RPi.GPIO as GPIO
- import time
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(11,GPIO.OUT)
- pwm = GPIO.PWM(11, 50)
- pwm.start(0)
- var=0
- def my_func(no_pulses, no_sec):
- while var<no_pulses :
- for i in range(0, 103, 3):
- pwm.ChangeDutyCycle(i)
- sleep(0.02)
- for i in range(100, -3, -3):
- pwm.ChangeDutyCycle(i)
- sleep(0.02)
- sleep(no_sec)
- var=var+1
- my_func(4, 2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement