Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #4
- import RPi.GPIO as GPIO
- import time
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(12,GPIO.IN, pull_up_down=GPIO.PUD_UP)
- GPIO.setup(16,GPIO.OUT)
- pwm = GPIO.PWM(16, 50)
- pwm.start(10)
- i=0
- rise=1
- while True:
- if(rise==1):
- print i
- pwm.ChangeDutyCycle(i)
- i=i+10
- time.sleep(0.5)
- if(i==100):
- rise=0
- if(rise==0):
- pwm.ChangeDutyCycle(i)
- i=i-10
- time.sleep(0.5)
- if(i==0):
- rise=1
- GPIO.cleanup()
- #--------------------
- import RPi.GPIO as GPIO #ex1/2
- import time
- import threading
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(12,GPIO.IN,pull_up_down=GPIO.PUD_UP)
- GPIO.setup(16,GPIO.OUT)
- led_on = False
- def button_pressed(pin):
- print 'Button was pressed connected at the pin %s' % pin
- global led_on
- if not led_on:
- print 'Led is on'
- GPIO.output(16,GPIO.HIGH)
- led_on = True
- else:
- print 'Led is off'
- GPIO.output(16,GPIO.LOW)
- led_on = False
- def detect_event():
- GPIO.add_event_detect(12,GPIO.RISING,button_pressed,bouncetime=200)
- try:
- print 'Start listening...'
- th = threading.Thread(target=detect_event)
- th.start()
- while True:
- time.sleep(1) # listening 20 seconds for events to be fired
- print "Press ctrl+c to stop"
- finally:
- print 'Stop listening.'
- th.join()
- GPIO.cleanup()
- #-------------------------------------------------------------------------------------------
- import RPi.GPIO as GPIO #ex4
- import time
- import threading
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(12,GPIO.IN,pull_up_down=GPIO.PUD_UP)
- GPIO.setup(16,GPIO.OUT)
- led_on = False
- pwm_value = 0.0
- pwm = GPIO.PWM(16,50)
- pwm.start(10)
- reverse = False
- def button_pressed(pin):
- global pwm_value
- global pwm
- global reverse
- if not reverse:
- pwm_value += 10
- else:
- pwm_value -= 10
- if pwm_value == 100 or pwm_value == 0:
- reverse = not reverse
- pwm.ChangeDutyCycle(pwm_value)
- print "Current intensity in percent: %s" % pwm_value
- def detect_event():
- GPIO.add_event_detect(12,GPIO.RISING,button_pressed,bouncetime=200)
- try:
- print 'Start listening...'
- th = threading.Thread(target=detect_event)
- th.start()
- while True:
- time.sleep(1) # listening 20 seconds for events to be fired
- print "Press ctrl+c to stop"
- finally:
- print 'Stop listening.'
- th.join()
- GPIO.cleanup()
- #-------------------------------------------------------------------------------------------
- import RPi.GPIO as GPIO #ex3
- import time
- import threading
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(12,GPIO.IN,pull_up_down=GPIO.PUD_UP)
- GPIO.setup(16,GPIO.OUT)
- led_on = False
- pwm_value = 0.0
- pwm = GPIO.PWM(16,100)
- pwm.start(50.0)
- def button_pressed(pin):
- global pwm_value
- global pwm
- pwm_value += 1
- if pwm_value > 100:
- pwm_value = 1.0
- pwm.ChangeFrequency(pwm_value)
- print "Current intensity in percent: %s" % pwm_value
- def detect_event():
- GPIO.add_event_detect(12,GPIO.RISING,button_pressed,bouncetime=200)
- try:
- print 'Start listening...'
- th = threading.Thread(target=detect_event)
- th.start()
- while True:
- time.sleep(1) # listening 20 seconds for events to be fired
- print "Press ctrl+c to stop"
- finally:
- print 'Stop listening.'
- th.join()
- GPIO.cleanup()
- #-------------------------------------------------------------------------------------------
- import RPi.GPIO as GPIO
- import time
- import threading #ex5
- import sys
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(12,GPIO.IN,pull_up_down=GPIO.PUD_UP)
- GPIO.setup(16,GPIO.OUT)
- if len(sys.argv) != 3:
- raise Exception("YOU ARE BAD")
- led_on = False
- pulses = int(sys.argv[1])
- times = int(sys.argv[2])
- def button_pressed():
- global times
- global pulses
- global led_on
- while True:
- if pulses:
- GPIO.output(16,GPIO.HIGH)
- time.sleep(times)
- GPIO.output(16,GPIO.LOW)
- time.sleep(times)
- pulses = pulses - 1
- else:
- break
- try:
- print 'Start listening...'
- th = threading.Thread(target=button_pressed)
- th.start()
- while True:
- time.sleep(1) # listening 20 seconds for events to be fired
- print "Press ctrl+c to stop"
- finally:
- print 'Stop listening.'
- th.join()
- GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement