Small Breadboard with Resistor and LED



Schematic 

  led schemat




BCM GPIO# 2nd func pin#
pin# 2nd func BCM GPIO#
- +3V3 1
2 +5V -
GPIO2 SDA1 (I2C) 3
4 +5V -
GPIO3 SCL1 (I2C) 5
6 GND -
GPIO4 GCLK 7
8 TXD0 (UART) GPIO14
- GND 9
10 RXD0 (UART) GPIO15
GPIO17 GEN0 11
12 GEN1 GPIO18
GPIO27 GEN2 13
14 GND -
GPIO22 GEN3 15
16 GEN4 GPIO23
- +3V3 17
18 GEN5 GPIO24
GPIO10 MOSI (SPI) 19
20 GND -
GPIO9 MISO (SPI) 21
22 GEN6 GPIO25
GPIO11 SCLK (SPI) 23
24 CE0_N (SPI) GPIO8
- GND 25
26 CE1_N (SPI) GPIO7
(Models A and B stop here)
EEPROM ID_SD 27
28 ID_SC EEPROM
GPIO5 - 29
30 GND -
GPIO6 - 31
32 - GPIO12
GPIO13 - 33
34 GND -
GPIO19 - 35
36 - GPIO16
GPIO26 - 37
38 - GPIO20
- GND 39
40 - GPIO21




Code

#! /usr/bin/python

# Blink a LED
# Import the libraries we need
import RPi.GPIO as GPIO
import time

# Set the pin mode to board pin numbering
GPIO.setmode(GPIO.BOARD)
pin = 40

# Delay time
n = 1

# Set the pin to be an output
GPIO.setup(pin, GPIO.OUT)

i=0

# Loop turning it on and off
while i < 10:
    GPIO.output(pin, 1)
    time.sleep(n)
    GPIO.output(pin, 0)
    time.sleep(n)
    i = i + 1

# Set the pins back to default
GPIO.cleanup()



Don't take your LED circuit apart

Next session we will be adding to the the circuit board and will be doing more programming using the LED. Besides there is homework. See below

Homework

Purpose

Change the blink rate from the keyboard

We want to change the blink frequency by entering numbers on the keyboard. The problem is that if we use standard python input, everything, including the blinking, stops waiting for the input. We need a non blocking input.

This is provided by the libraries:

None blocking input code examples

See if any input is avilable

# Is any input available input = select.select([sys.stdin], [], [], 0)[0] # If so read the input if input: value = sys.stdin.readline().rstrip()

The "input =" line above essentially gets the read status from sys.stdin. Standard in is usually the keyboard.

This will return one of the following two choices:

When we have some input we need to read it. The sys.stdin.readline() does that. The rstrip() removes any trailing white space characters.

A program that changes its sleep time given keyboard input

The def is_number() creates a function that we will invoke (call) later to check if the user actually entered something that is a number. All input from the keyboard starts as characters, and if we want to use it as a number we need to convert it. If the user enters "W" and we try to convert it to a floating point number the program will normally exit with a frightening error message. The try/except captures this error and tells us we can't convert "W" to a number - so we move on.

Before we convert the input characters to a number later in the program, we call this function.

Again, the try: and except ... : statements set up what to do if the float() succeeds and what to do if it fails. If it succeeds the function returns True. If it fails the function returns False.


Note: The concepts in this code are intermediate to advanced. So if you are finding some of this hard to understand not to worry. We tossed you in the deep end, and we will toss you a floatation device if you ask for it.
#!/usr/bin/python3
 
import sys
import select
import time

# Function to return True if input string is a number else False.
def is_number(s):
    # Try to convert the input to a floating point numer.  If it works then True is returned.
    try:
        float(s)
        return True
    # If the conversion to floating point does not work return False.
    except ValueError:
        return False 

# Initialize sleep time and value
n = 1.0 
value = ""

# Loop until "q" is entered.
while value != "q":

    #Check if there is input.  If there is read it.
    input = select.select([sys.stdin], [], [], 0)[0]
    if input:
        value = sys.stdin.readline().rstrip()
        
        #if the input is a number use it.  Otherwise ignore it.
        if is_number(value):   # call the function.
            n = float(value)

    print (n)
    time.sleep(n)

Your Job:

Is to modify this program to add the LED blink functionality we covered in the Tuesday session so that by entering keyboard input you can change the speed of the blinking.

Help

If you get stuck or have questions please email me, Deid, at vicpi@drsol.com. I am pleased to answer questions.

Detailed Select Documentation

If you really want to know. And if you want even more do:

select(...)
    select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)
    
    Wait until one or more file descriptors are ready for some kind of I/O.
    The first three arguments are sequences of file descriptors to be waited for:
    rlist -- wait until ready for reading
    wlist -- wait until ready for writing
    xlist -- wait for an ``exceptional condition''
    If only one kind of condition is required, pass [] for the other lists.
    A file descriptor is either a socket or file object, or a small integer
    gotten from a fileno() method call on one of those.
    
    The optional 4th argument specifies a timeout in seconds; it may be
    a floating point number to specify fractions of seconds.  If it is absent
    or None, the call will never time out.
    
    The return value is a tuple of three lists corresponding to the first three
    arguments; each contains the subset of the corresponding file descriptors