Python button and LED control - explanation to the challenges Pro Preview

These challenges will throw you in the deep water, get prepared and read carefully.

To access the full video please subscribe to FLLCasts.com

Subscribe

  • #835
  • 09 Apr 2018

Read carefully all the python code in ledButtonDemo.py

Try to understand while loops in that example. In the previous ledPulseDemo.py there were for loops that repeat fixed amount of times. 
While loops repeat until a condition is met. 

 

Create a program, that waits for a button to starts and then the led blinks

The GPIO.input() function is what reads the button and True means that the button is pressed. That put in a while loop is what creates a wait-for-button functionality.

Name the program waitButton.py

 

Create a program that makes the LED pulse up faster than in the demo

Here we expect you to open the previous demo and merge codes together.
One of the ways to achieve speed is to create a bigger step of increase in the for loop with the help of a third number.

Hint: in the line

for x in range(max_light, 0, -1):                # this is a loop with 60 repetitions, from 60 to 1, counting backwards

the number -1 is the step of change in each repetition.

Name the program ledPulseUpFast.py

 

Create a program that makes the LED pulse down faster than in the demo

This task is the same as the other one, but here we want you to change the pause between the repetitions in the loop, thus making the loop faster.

Name the program ledPulseDownFast.py

 

Each button press increases a counter with 10 and LED lights with that counter

That is task is quite a mouthful if you are not careful. Remeber:

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 59

    x = x + 1                        # we must increase x manually, in order to leave the loop

 

Name the program ledPulseUpButtonPress.py

 

Courses and lessons with this Tutorial

This Tutorial is used in the following courses and lessons

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