Experiment

Don't worry if you make a mistake in coding. The output below the code will be a helpful message trying to get you back on track. We'll have lots of opportunity to improve or occasionally break the code. For example, can you find and fix the problem in the code below that prevents it from running? Python tries to tell us as concisely as it can, but it sometimes comes off a little terse. Fix the error and hit shift and enter to run it again.

If you aren't familiar with python syntax, compare the if statement below with the for and proc statements in the code above to see what is missing.


In [5]:
import datetime;

if((datetime.datetime.today().hour) > 12)
    print("good afternoon")
else:
    print("good morning")


  File "<ipython-input-5-559999fffc4f>", line 3
    if((datetime.datetime.today().hour) > 12)
                                             ^
SyntaxError: invalid syntax

Mousing around

What more can we do with the environment? How about making buttons to turn the LED on or off:


In [ ]:
import RPi.GPIO as GPIO
from ipywidgets import widgets
from IPython.display import display

print("using Raspberry Pi board pin numbers")
GPIO.setmode(GPIO.BOARD)
led_pin = 11

print("set up GPIO output channel")
GPIO.setwarnings(False)
GPIO.setup(led_pin, GPIO.OUT)

def led_on(btn):
    print('on')
    GPIO.output(led_pin, GPIO.HIGH)
    
def led_off(btn):
    print('off')
    GPIO.output(led_pin, GPIO.LOW)
    
on_btn = widgets.Button(description="On!")
on_btn.on_click(led_on)
off_btn = widgets.Button(description="Off!")
off_btn.on_click(led_off)
display(on_btn)
display(off_btn)

Watch the messages that appear under the code as you hit the buttons. You can change the messages if you want to. Edit the code above and hit shift and enter together to see how it works.

Go on to Learner lesson 3


In [ ]: