# 3-step Adjustable Light | MindPlus Coding Kit for Arduino Started Tutorial E07

In the making of breathing light in the previous section, we achieved the effect of fade-in and fade-off through function calls. In this section, we will use buttons to control the LED module to achieve multi-step brightness feature. Multi-step control is often used in car’s air-conditioning keys, desk lamp, stage lighting, sound system, etc.

1. What is a PWM output?

2. Make a 3-step adjustable light

#### Key Points Analysis

Do you feel familiar with PWM? In the making of the breathing light in the previous section, we

have used the PWM output

PWM is the abbreviation of “Pulse Width Modulation”. It can convert the digital output waveform into an equivalent analog waveform.

PWM mechanism: by adjusting the duty cycle of the pulse square wave in each cycle, without changing the pulse square wave period, in order to achieve an equivalent analog voltage output.

PWM Wave

Duty Cycle: refers to the time ratio of high level in a cycle.

Digital signal waveform and PWM modulation

Right now, you only need to have a general understanding of PWM. You don't need to dig into it yet, and will learn more in the future.

### Hands-On

Hardware connection

Connect the red LED to digital pin 10 on the UNO board. The button is connected to Digital Pin 2.

Hardware connection (LED-R-10, button-2)

Please match the colors when plugging

Programming

1. Write the program

First imagine the use scenario: the button is not pressed, the LED is off- > the first time the button is pressed-the LED is slightly brighter- > the second time the button is pressed - the LED is further brighter- > the third time the button is pressed-the LED is on highest brightness; press the button for the fourth time-the LED goes out ...

First, create a variable Count with an initial value of 0 (representing 0 step, off)

Have a look at the process, and we know we can use “if ... else”, the first form of a conditional.

Given condition is that the LED brightness range is 0 ~ 255 when we make a 3-step adjustable light by PWM. How to do it?

2. Reference program

3. Program effect

Press the button for the first time to turn on the LED, but the brightness is low; press it again to increase the brightness; press it for the third time, the LED will be the brightest. Another press will turn off the LED. Further presses will cycle those effects.

4. Program analysis

Imagine what would be if we neglect 0 step and set it to 3?

Essentially, computers don’t exist without mathematics. Von Neumann, crowned as “father of computers”, is himself one of the most prominent mathematicians in 20th century. At the very beginning, computers were made only to assist humans in complicated and time-consuming calculations. You might think that modern day computers all present User Interface, so they have nothing to do with mathematics. That is not the case. Computers are still executing instructions in the CPU. Every instruction is stored and parsed by binary numbers, like 0 and 1. This is the most basic knowledge in mathematics. Therefore, computers and mathematics are inseparable.

All this is the relationship between computers and mathematics, but what about computer science? The relationship between computer science and mathematics might be a little odd. Around twenty to thirty years ago, computer science was still only a branch of mathematics. Now, computer science has a wide range of research fields and a huge number of researchers, which in turn promotes the development of mathematics in many ways. In a sense, it can be said that the child has grown taller than his mother. But mother’s blood can still be found in child’s vein. This blood is the mathematical underpinning of computer science.

Another inter-discipline between modern computational science and mathematics is computational science or numerical analysis. However, majors of computer science are studying programming. What does programming have to do with mathematics? Let's talk about the potential impact that mathematics may have on future computer science majors.

1. Database data.

Computers are inseparable from data. Mathematics goes with data hand in hand. The background programs of an Internet service, database storage, high concurrency, and big data, all are related to many theorems and formulas in mathematics.

2. The data structure is largely the study of algorithm

The core technology of a company is often algorithms. Companies might be happy to opensource many other technologies they have, but core algorithms are always trade secrets. In job interviews, there are often tests on algorithms, many of which are data structures. Topics are often optimal path, binary tree, etc.

3. Geometry, Linear Algebra

3D video games and Photoshops are all based on space geometry. Even making a Sniper model move around in Dota game is related to mathematics.

It might seem that mathematics doesn’t play an important role in your path to a successful programmer. But it is vital when you crave for technological innovations. Mathematics isn’t necessary while learning programming, but knowledge of mathematics will only help you in writing better code.

### Further Exercise

Try making a 5-step adjustable light!