For your reference and self-check.

To access the full video please subscribe to FLLCasts.com
- #837
- 09 Apr 2018
While-loops and for-loops
For your reference: for loops are used when you know how many iterations you want to do before hand.
While-loops have condition and have unknown number of itereations.
Avoid forcing a for-loop to act as a while loop:
for x in range(0, max_light): if GPIO.input(buttonPin) == True:x = x + 1# increase x, that stops the loop to reach the exit conditionprint("LED light at %d%%" % (x)) front_lights.start(x) sleep(0.05) #eof for x
It is bad idea to modify x inside of the loop, as it is the loop counter, and furthermore, it does not work. Try that example for yourself to see.
Create a program, that waits for a button to starts and then the led blinks
from time import sleep import RPi.GPIO as GPIO GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) frontLightsPin = 18 frequency = 50 max_light = 60 buttonPin = 26 GPIO.setup(frontLightsPin, GPIO.OUT) front_lights = GPIO.PWM(frontLightsPin, frequency) GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # this part is new, GPIO 26 is set to input; # PUD_DOWN means that the second end of the button goes to 3V3 pin (e.g 17) while GPIO.input(buttonPin) == False: # while is a loop until a condition becomes not True; 'until pressed' in this case print("I am waiting...") sleep(0.3) #eof while front_lights.start(max_light) sleep(2.0) front_lights.stop() GPIO.cleanup()
Create a program that makes the LED pulse up faster than in the demo
from time import sleep # we need sleep function from time library import RPi.GPIO as GPIO # GPIO library to control pins GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) # BCM means we will use GPIO numbers, not pin numbers frontLightsPin = 18 # GPIO 18 frequency = 50 max_light = 100 GPIO.setup(frontLightsPin, GPIO.OUT) # set GPIO 18 in output mode, the led is there front_lights = GPIO.PWM(frontLightsPin, frequency) # PWM = pulse width modulation with 50Hz (blinks per second) for x in range(0, max_light, 7): # this is a loop with 60 repetitions; from 0 to 59 (it's complicated) print("LED light at %d%%" % (x)) # %d means a digit or number added to the message; %% means a %-sign front_lights.start(x) sleep(0.05) #eof for x # eof means "end of" and that helps knowing where a loop ends GPIO.cleanup()
Create a program that makes the LED pulse down faster than in the demo
from time import sleep # we need sleep function from time library import RPi.GPIO as GPIO # GPIO library to control pins GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) # BCM means we will use GPIO numbers, not pin numbers frontLightsPin = 18 # GPIO 18 frequency = 50 max_light = 100 GPIO.setup(frontLightsPin, GPIO.OUT) # set GPIO 18 in output mode, the led is there front_lights = GPIO.PWM(frontLightsPin, frequency) # PWM = pulse width modulation with 50Hz (blinks per second) for x in range(max_light, 0, -1): # this is a loop with 60 repetitions; from 0 to 59 (it's complicated) print("LED light at %d%%" % (x)) # %d means a digit or number added to the message; %% means a %-sign front_lights.start(x) sleep(0.01) #eof for x # eof means "end of" and that helps knowing where a loop ends GPIO.cleanup()
Each button press increases a counter with 10 and LED lights with that counter
A simple straightforward solution to get the job done. For a better solution, see below.
from time import sleep import RPi.GPIO as GPIO GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) frontLightsPin = 18 frequency = 50 max_light = 61 buttonPin = 26 GPIO.setup(frontLightsPin, GPIO.OUT) front_lights = GPIO.PWM(frontLightsPin, frequency) GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # this part is new, GPIO 26 is set to input; # PUD_DOWN means that the second end of the button goes to 3V3 pin (e.g 17) x = 0 # x will be used for counting, therefore it is called counter while x < max_light: # the condition is x to become more than 61 if GPIO.input(buttonPin) == True: x = x + 10 print("LED light at %d%%" % (x)) # %d means a digit or number added to the message front_lights.start(x) sleep(0.500) # prevent fast increase and wait for release #eof while x
Advanced and elegant solution to the last task
This is how it should be done at a higher level, and we put it here for your reference. It is not required for your students to implement it that way.
from time import sleep import RPi.GPIO as GPIO GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) frontLightsPin = 18 frequency = 50 max_light = 61 buttonPin = 26 GPIO.setup(frontLightsPin, GPIO.OUT) front_lights = GPIO.PWM(frontLightsPin, frequency) GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) x = 0 while x < max_light: if GPIO.input(buttonPin) == True: x = x + 10 print("LED light at %d%%" % (x)) while GPIO.input(buttonPin) == True: sleep(0.010) # wait for release; that makes the loop react to single button press front_lights.start(x) sleep(0.010) # give the CPU some time for multitasking #eof while x GPIO.cleanup()
Courses and lessons with this Tutorial
This Tutorial is used in the following courses and lessons

Perfect STEM course. Module 1 - Smart Car with Raspberry PI
Disassemble a remote control car. Change the brain of the car with a smart computer like Raspberry Pi. Build a smart device with artificial intelligence that you could control from your phone and that could freely navigate itself in the real world and on the Internet. Use your hands. Develop programs for your robot and your phone. Be curious and invent.
The perfect course lives up to its name. You move through the content, we check it and return feedback to you.
In the end, you should be able to better understand how to program and design smart devices that would make the world a better place. For everybody age 12+, 16+, 21+, 35+, etc. The hardware costs about 150$. It changes through the years so it might take some time for you to find it as Raspberry PI, motor drivers, power banks and h-bridges.. they change. Note that the course was designed to be led by a teacher with a decent knowledge in embedded hardware and software. This, on the other hand should not stop you, if you are curious.
- 118
- 42:47
- 136

Create Python programs using the button to control the car LEDs
These challenges will throw you in the deep water, get prepared and read carefully.
- 2
- 0
- 5
- 3d_rotation 0