Advertisement
alexarcan

MS_HW1_Mihai

Mar 15th, 2016
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.79 KB | None | 0 0
  1. #4
  2. import RPi.GPIO as GPIO
  3. import time
  4. GPIO.setmode(GPIO.BOARD)
  5.  
  6. GPIO.setup(12,GPIO.IN, pull_up_down=GPIO.PUD_UP)
  7. GPIO.setup(16,GPIO.OUT)
  8.  
  9. pwm = GPIO.PWM(16, 50)
  10. pwm.start(10)
  11. i=0
  12. rise=1
  13. while True:
  14.     if(rise==1):
  15.         print i
  16.         pwm.ChangeDutyCycle(i)
  17.         i=i+10
  18.         time.sleep(0.5)
  19.         if(i==100):
  20.             rise=0
  21.     if(rise==0):
  22.         pwm.ChangeDutyCycle(i)
  23.         i=i-10
  24.         time.sleep(0.5)
  25.         if(i==0):
  26.             rise=1
  27.        
  28.    
  29. GPIO.cleanup()
  30. #--------------------
  31. import RPi.GPIO as GPIO #ex1/2
  32. import time
  33. import threading
  34.  
  35. GPIO.setmode(GPIO.BOARD)
  36. GPIO.setup(12,GPIO.IN,pull_up_down=GPIO.PUD_UP)
  37.  
  38. GPIO.setup(16,GPIO.OUT)
  39.  
  40. led_on = False
  41. def button_pressed(pin):
  42.         print 'Button was pressed connected at the pin %s' % pin
  43.         global led_on
  44.         if not led_on:
  45.                 print 'Led is on'
  46.                 GPIO.output(16,GPIO.HIGH)
  47.                 led_on = True
  48.         else:
  49.                 print 'Led is off'
  50.                 GPIO.output(16,GPIO.LOW)
  51.                 led_on = False
  52.  
  53. def detect_event():
  54.         GPIO.add_event_detect(12,GPIO.RISING,button_pressed,bouncetime=200)
  55.  
  56. try:
  57.         print 'Start listening...'
  58.         th = threading.Thread(target=detect_event)
  59.         th.start()
  60.         while True:
  61.                 time.sleep(1) # listening 20 seconds for events to be fired
  62.                 print "Press ctrl+c to stop"
  63. finally:
  64.         print 'Stop listening.'
  65.         th.join()
  66.         GPIO.cleanup()
  67.  
  68. #-------------------------------------------------------------------------------------------
  69.  
  70. import RPi.GPIO as GPIO #ex4
  71. import time
  72. import threading
  73.  
  74. GPIO.setmode(GPIO.BOARD)
  75. GPIO.setup(12,GPIO.IN,pull_up_down=GPIO.PUD_UP)
  76.  
  77. GPIO.setup(16,GPIO.OUT)
  78.  
  79. led_on = False
  80. pwm_value = 0.0
  81. pwm = GPIO.PWM(16,50)
  82. pwm.start(10)
  83. reverse = False
  84. def button_pressed(pin):
  85.         global pwm_value
  86.         global pwm
  87.         global reverse
  88.         if not reverse:
  89.                 pwm_value += 10
  90.         else:
  91.                 pwm_value -= 10
  92.         if pwm_value == 100 or pwm_value == 0:
  93.                 reverse = not reverse
  94.         pwm.ChangeDutyCycle(pwm_value)
  95.         print "Current intensity in percent: %s" % pwm_value
  96.  
  97. def detect_event():
  98.         GPIO.add_event_detect(12,GPIO.RISING,button_pressed,bouncetime=200)
  99.  
  100. try:
  101.         print 'Start listening...'
  102.         th = threading.Thread(target=detect_event)
  103.         th.start()
  104.         while True:
  105.                 time.sleep(1) # listening 20 seconds for events to be fired
  106.                 print "Press ctrl+c to stop"
  107. finally:
  108.         print 'Stop listening.'
  109.         th.join()
  110.         GPIO.cleanup()
  111.  
  112.  
  113. #-------------------------------------------------------------------------------------------
  114.  
  115. import RPi.GPIO as GPIO #ex3
  116. import time
  117. import threading
  118.  
  119. GPIO.setmode(GPIO.BOARD)
  120. GPIO.setup(12,GPIO.IN,pull_up_down=GPIO.PUD_UP)
  121.  
  122. GPIO.setup(16,GPIO.OUT)
  123.  
  124. led_on = False
  125. pwm_value = 0.0
  126. pwm = GPIO.PWM(16,100)
  127. pwm.start(50.0)
  128. def button_pressed(pin):
  129.         global pwm_value
  130.         global pwm
  131.         pwm_value += 1
  132.         if pwm_value > 100:
  133.                 pwm_value = 1.0
  134.         pwm.ChangeFrequency(pwm_value)
  135.         print "Current intensity in percent: %s" % pwm_value
  136.  
  137. def detect_event():
  138.         GPIO.add_event_detect(12,GPIO.RISING,button_pressed,bouncetime=200)
  139.  
  140. try:
  141.         print 'Start listening...'
  142.         th = threading.Thread(target=detect_event)
  143.         th.start()
  144.         while True:
  145.                 time.sleep(1) # listening 20 seconds for events to be fired
  146.                 print "Press ctrl+c to stop"
  147. finally:
  148.         print 'Stop listening.'
  149.         th.join()
  150.         GPIO.cleanup()
  151.  
  152.  
  153.  
  154. #-------------------------------------------------------------------------------------------
  155.  
  156. import RPi.GPIO as GPIO
  157. import time
  158. import threading #ex5
  159. import sys
  160.  
  161. GPIO.setmode(GPIO.BOARD)
  162. GPIO.setup(12,GPIO.IN,pull_up_down=GPIO.PUD_UP)
  163.  
  164. GPIO.setup(16,GPIO.OUT)
  165.  
  166. if len(sys.argv) != 3:
  167.         raise Exception("YOU ARE BAD")
  168.  
  169. led_on = False
  170. pulses = int(sys.argv[1])
  171. times = int(sys.argv[2])
  172.  
  173. def button_pressed():
  174.         global times
  175.         global pulses
  176.         global led_on
  177.         while True:
  178.                 if pulses:
  179.                         GPIO.output(16,GPIO.HIGH)
  180.                         time.sleep(times)
  181.                         GPIO.output(16,GPIO.LOW)
  182.                         time.sleep(times)
  183.                         pulses = pulses - 1
  184.  
  185.                 else:
  186.                         break
  187.  
  188.  
  189. try:
  190.         print 'Start listening...'
  191.         th = threading.Thread(target=button_pressed)
  192.         th.start()
  193.         while True:
  194.                 time.sleep(1) # listening 20 seconds for events to be fired
  195.                 print "Press ctrl+c to stop"
  196. finally:
  197.         print 'Stop listening.'
  198.         th.join()
  199.         GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement