@@ -133,6 +133,33 @@ Run a function every time the button is pressed::
133133
134134 pause()
135135
136+ .. note ::
137+
138+ Note that the line ``button.when_pressed = say_hello `` does not run the
139+ function ``say_hello ``, rather it creates a reference to the function to
140+ be called when the button is pressed. Accidental use of
141+ ``button.when_pressed = say_hello() `` would set the ``when_pressed `` action
142+ to ``None `` (the return value of this function) which would mean nothing
143+ happens when the button is pressed.
144+
145+ Similarly, functions can be attached to button releases::
146+
147+ from gpiozero import Button
148+ from signal import pause
149+
150+ def say_hello():
151+ print("Hello!")
152+
153+ def say_goodbye():
154+ print("Goodbye!")
155+
156+ button = Button(2)
157+
158+ button.when_pressed = say_hello
159+ button.when_released = say_goodbye
160+
161+ pause()
162+
136163
137164Button controlled LED
138165=====================
@@ -165,6 +192,53 @@ Alternatively::
165192 pause()
166193
167194
195+ Button controlled camera
196+ ========================
197+
198+ Using the button press to trigger picamera to take a pitcure using
199+ ``button/when_pressed = camera.capture `` would not work because it requires an
200+ ``output `` parameter. However, this can be achieved using a custom function
201+ which requires no parameters::
202+
203+ from gpiozero import Button
204+ from picamera import PiCamera
205+ from datetime import datetime
206+ from signal import pause
207+
208+ button = Button(2)
209+ camera = PiCamera()
210+
211+ def capture():
212+ datetime = datetime.now().isoformat()
213+ camera.capture('/home/pi/%s.jpg' % datetime)
214+
215+ button.when_pressed = capture
216+
217+ pause()
218+
219+ Another example could use one button to start and stop the camera preview, and
220+ another to capture::
221+
222+ from gpiozero import Button
223+ from picamera import PiCamera
224+ from datetime import datetime
225+ from signal import pause
226+
227+ left_button = Button(2)
228+ right_button = Button(3)
229+ camera = PiCamera()
230+
231+ def capture():
232+ datetime = datetime.now().isoformat()
233+ camera.capture('/home/pi/%s.jpg' % datetime)
234+
235+ left_button.when_pressed = camera.start_preview
236+ left_button.when_released = camera.stop_preview
237+ right_button.when_pressed = capture
238+
239+ pause()
240+
241+
168242Traffic Lights
169243==============
170244
0 commit comments