Advertisement
alexarcan

MS_HW1_Ralu

Mar 15th, 2016
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None | 0 0
  1. Ex 1
  2. import RPi.GPIO as GPIO
  3. import time
  4. GPIO.setmode(GPIO.BOARD)
  5. GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP)   #button
  6. GPIO.setup(11,GPIO.OUT)
  7. GPIO.output(11,GPIO.LOW)
  8. button=0  #up
  9. light=0 #off
  10. while True:
  11.     if (button==0 and light==0):
  12.         if GPIO.input(12) == False:    #button is pressed
  13.             GPIO.output(11, GPIO.HIGH)
  14.             button=1
  15.             light=1
  16.     elif (button==1 and light==0):
  17.         if GPIO.input(12):
  18.             button=0
  19.     elif (button==0 and light==1):
  20.         if GPIO.input(12)==False:
  21.             GPIO.output(11, GPIO.LOW)
  22.             button=1
  23.             light=0
  24.     elif (button==1 and light==0):
  25.         if GPIO.input(12):
  26.             button=0
  27.     sleep(0.2)
  28. GPIO.cleanup()
  29.  
  30.  
  31. Ex 2
  32. import RPi.GPIO as GPIO
  33. import time
  34. GPIO.setmode(GPIO.BOARD)
  35. GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP)   #button
  36. GPIO.setup(11,GPIO.OUT)
  37. GPIO.output(11,GPIO.LOW)
  38. button=0  #up
  39. light=0 #off
  40. def callback_fct(pin):
  41.     if (button==0 and light==0):
  42.         if GPIO.input(12) == False:    #button is pressed
  43.             GPIO.output(11, GPIO.HIGH)
  44.             button=1
  45.             light=1
  46.     elif (button==1 and light==0):
  47.         if GPIO.input(12):
  48.             button=0
  49.     elif (button==0 and light==1):
  50.         if GPIO.input(12)==False:
  51.             GPIO.output(11, GPIO.LOW)
  52.             button=1
  53.             light=0
  54.     elif (button==1 and light==0):
  55.         if GPIO.input(12):
  56.             button=0
  57. GPIO.add_event_detect(12, GPIO.BOTH, callback=button_callback, bouncetime=100)
  58. try:
  59. time.sleep(300)
  60. finally:
  61. GPIO.cleanup()     
  62.  
  63. Ex 3
  64. import RPi.GPIO as GPIO
  65. import time
  66. GPIO.setmode(GPIO.BOARD)
  67. GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP)   #button
  68. GPIO.setup(11,GPIO.OUT)
  69. pwm = GPIO.PWM(11, 50)
  70. pwm.start(10)
  71. var=10
  72. while True:
  73.     if GPIO.input(12) == False
  74.         var=var+10
  75. pwm.ChangeDutyCycle(var)
  76.     sleep(0.2)
  77. GPIO.cleanup()     
  78.  
  79.  
  80. ex 4
  81.  
  82. import RPi.GPIO as GPIO
  83. import time
  84. GPIO.setmode(GPIO.BOARD)
  85. GPIO.setup(11,GPIO.OUT)
  86. pwm = GPIO.PWM(11, 50)
  87. pwm.start(0)
  88. while True:
  89.     for i in range(0,  103, 3):
  90. pwm.ChangeDutyCycle(i)
  91. sleep(0.02)
  92.     for i in range(100, -3, -3):
  93.         pwm.ChangeDutyCycle(i)
  94.         sleep(0.02)
  95. GPIO.cleanup() 
  96.  
  97.  
  98. Ex 5
  99. import RPi.GPIO as GPIO
  100. import time
  101. GPIO.setmode(GPIO.BOARD)
  102. GPIO.setup(11,GPIO.OUT)
  103. pwm = GPIO.PWM(11, 50)
  104. pwm.start(0)
  105. var=0
  106. def my_func(no_pulses, no_sec):
  107. while var<no_pulses :
  108.         for i in range(0,  103, 3):
  109. pwm.ChangeDutyCycle(i)
  110. sleep(0.02)
  111.         for i in range(100, -3, -3):
  112.             pwm.ChangeDutyCycle(i)
  113.             sleep(0.02)
  114.         sleep(no_sec)
  115.         var=var+1
  116. my_func(4, 2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement