You will need:
4 complete sets of LED’s and wires (see last lesson)
The first thing you will need to is connect your negative sides of the wire to any ground cable that is not taken, like 14, 9, 20, or 25.
The next thing you will need to do is to connect the positive sides of the LED(s) to 11, 12, 13, and 33.(pleas see chart)
What you will need to do next is to open a new file in text editor.
The first thing you will need to type is your shebang line.
1 |
#!/usr/bin/python |
The next thing you will need is to create a variable, a variable is a name assigned to mean a number or thing or a … anything. For example (Fish = .05). Fish is an example, you would usually say what its doing in a short way, we need the amount of time to blink… blinkTime. after that you put = 1.
1 |
blinkTime = 1 |
That kind of variable is called a scalar variable, a variable with one name.
sometimes we need a variable that means a list of things, an array variable.
We need to make a list of the Gpio pin numbers that we are going to blink. Those are pin numbers 11, 12, 13, and 33.Those numbers represent the positive wires we are using. An array is enclosed in square brackets []. If i made an array of my three favorite coolers, it would be [“green”,”black”,”blue”] –Two things to note:
- In an array valus in the list are separated by commas
- If a variable is a phrase or a word, it must be contained in quotes, if its a number, no quotes.
For are code we need to make a variable called blinkPins, and then the array.
1 |
blinkPins = [11,12,13,33] |
Next we need to be able to control the GPIO with python, also you need time functions.
1 2 3 |
<code>import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) |
Just like last time
Now lets use are array for something fun:
the “for” command – the for command does the same thing over and over again for each item in an array. The for command assigns a special variable that changes for each item in an array list. Last time we set up a single pin on our GPIO, this time we will set up four of them with our array.
The first time our for loop runs, it will use the first number on our list, 11. it will then run 12, then 13, then 33. If we wrote this out in code without an array or without for, it would take at least four times as much code.
so lets type in our first “for” loop:
1 2 |
for p in blinkPins: GPIO.setup(p, GPIO.OUT) |
Above we say for p in blinkPins, p will be the variable for each item in the blinkPins array. The lines that start with a tab is the lines that will repeat until for has finished with our array.
“GPIO.setup(p, GPIO.OUT)”is the same command we ran last time, but this time it will run four all for of our blinking pins, and not just once.
To finish up and just like the first loop we will link through blink pins again. The first command inside our second loop will turn on the power to the LED bulb each time then, the time sleep command will sleep for the number of seconds in blinkTime, or 1 second. Then, we turn the LED off again, and wait one more second before going to the next step of the loop or finishing.
1 2 3 4 5 6 7 8 9 |
for p in blinkPins: GPIO.output(p,GPIO.HIGH) time.sleep(blinkTime) GPIO.output(p,GPIO.LOW) time.sleep(blinkTime) |
Lastly, you tell, python to stop using GPIO so other prosseces can run if you leave this out you will get an error next time you run it.
1 2 3 |
GPIO.cleanup() |
1 |