В момента ресурсът е наличен само на английски

Solution to the challenges. Teachers note Pro Preview

For your reference and self-check.

Необходимо е да се абонирате за 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 condition
	
	print("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()

Курсове и занятия включващи този Урок

Този Урок е използван в следните курсове и занятия.

Image for Perfect STEM course. Module 1 - Smart Car with Raspberry PI
  • 118
  • 42:47
  • 136