Maker Pro
Arduino

Programming Arduino Using Loops and the Map Function in BlocklyDuino

April 07, 2020 by Suketu Shah
Share
banner

In this tutorial, we are going to learn about FOR loop, WHILE loop, and the Map function of BlocklyDuino.

To further explore the FOR loop, WHILE loop, and Map function we will also utilize a potentiometer, a pulse width modulation signal, and a servo motor. We will do three exercises based on these topics.

A Quick Recap of Part 1

The previous article, How to Program an Arduino Using BlocklyDuino, began with instruction on installation and initial setup of the program. If you haven’t used BlocklyDuino before, I highly recommend you begin with that tutorial.

From there, we learned how to use digital input/outputs and IF-ELSE condition statements in BlocklyDuino. We performed an LED blinking test using the DigitalWrite and Delay blocks. Then, through a switch, we controlled (turned ON/OFF) an LED using the DigitalRead and IF-ELSE conditional blocks.

Fading and Brightening an LED using the FOR loop

In this exercise, we are going to change the intensity of an LED’s brightness, first low to high, then high to low, and so on. To achieve this, we have to change the intensity of LED at every step, i.e., after fixed intervals, like the ladder/step graph shown below.

Blockyduino_Loops_Functions_SS_MP_image25.png

So now the question is how are we going to change the intensity of LED? By using the AnalogWrite block!

The AnalogWrite block is located in the Input/Output tab of BlocklyDuino. It outputs different voltage levels per the specified PWM value entered by the user.

Blockyduino_Loops_Functions_SS_MP_image16.png

This block uses the concept of pulse width modulation (PWM). A PWM signal is a combination of analog and digital signals. For those who don’t know, the digital signal is a wave which can achieve only two voltage levels either high (1) or low (0), whereas the analog signal is one which can have an infinite number of voltage levels in the given range (e.g., 0V to 5V).

Blockyduino_Loops_Functions_SS_MP_image10.png

A visual of the difference between analog and digital signals

PWM is a combination of analog and digital, meaning that its nature of wave is similar to a digital signal but it can have an infinite number of voltage levels similar to an analog signal. The voltage generated by the PWM wave is called the average voltage. Let’s look at how the PWM wave exhibits this property. Start by taking a look at the graph below.

Blockyduino_Loops_Functions_SS_MP_image19.png

T is the total time taken to complete a single wave and Ton and Toff are the ON time and OFF time of the pulse, respectively. The ratio of Ton to total time is called the duty cycle.

Duty Cycle:

Blockyduino_Loops_Functions_SS_MP_image1.png

By changing the duty cycle and keeping the same frequency, we can generate different voltage levels.

There is also a formula to calculate the average voltage generated by the PWM wave. Remember that there is a difference between the average voltage and Vcc of Arduino.

Average Voltage = (DutyCycle/100)*Vcc

The image below shows PWM waves with different duty cycles and average voltage.

Blockyduino_Loops_Functions_SS_MP_image11.png

PWM waves with different duty cycles and average voltage.

With an understanding of the AnalogWrite block and PWM, next up is the FOR loop block.

The FOR loop block asks for two arguments viz start, and endpoints of count variable. It starts incrementing/decrementing counter variables from the set start point and goes up to the endpoint. So, code presented inside this loop wil; execute again and again until this variable is in the range of the start and endpoint. This block can be found in the Control tab of BlocklyDuino.

Blockyduino_Loops_Functions_SS_MP_image5.png

To give a better understanding, I have designed a flowchart of the FOR loop below.

Blockyduino_Loops_Functions_SS_MP_image4.png

Flowchart of the FOR loop

To achieve our task of LED fading and unfading, we are going to use both the AnalogWrite and FOR loop blocks.

First, we will set our start and endpoint of the FOR loop to 0 and 255 and inside the FOR loop we change the PWM value of the LED at every instant by using the i(count) variable. Once it reaches 255, it starts another FOR loop back at zero. Simultaneously, at every instant, we change the PWM of the LED.

Hardware connection and code are given below for your reference.

Blockyduino_Loops_Functions_SS_MP_image8.png
Blockyduino_Loops_Functions_SS_MP_image12.jpg

Copy the auto-generated Arduino code into Arduino IDE.

Compile the code and download it onto your Arduino board. Once the programming is done, observe the fading and brightening of the LED. Try to change the delay in the FOR loops and write in the comment section below what difference you observed!

Blockyduino_Loops_Functions_SS_MP_image13.gif

Reading the Potentiometer and turning ON/OFF LED Using the WHILE Loop

Let us first discuss the potentiometer. A potentiometer is simply a variable resistor that can be used to source variable voltage and current. The general diagram of the potentiometer is shown below.

Blockyduino_Loops_Functions_SS_MP_image27.png

A)

Blockyduino_Loops_Functions_SS_MP_image15.png

B)

A potentiometer has three terminals: the middle terminal is a slider and both end terminals are for power (Vcc & Gnd). The middle slider breaks the overall resistance of the potentiometer and creates two different resistors as shown in Figure B above. A 10kohm potentiometer means that the overall resistance between the two end terminals is 10kohm, but, with the help of the slider, we can generate variable resistance across any end terminal and slider.

Here we will use a potentiometer as a voltage divider. The image below how a potentiometer can be used as a voltage divider.

Blockyduino_Loops_Functions_SS_MP_image14.png

(A)

Blockyduino_Loops_Functions_SS_MP_image20.png

(B)

A common voltage divider (A) and a potentiometer as a voltage divider (B).

The voltage obtained at the second resistor end is calculated by the following formula:

Blockyduino_Loops_Functions_SS_MP_image3.png

We are going to read this voltage at the Arduino’s analog input pin and control LED. First, let's make the hardware connections.

Blockyduino_Loops_Functions_SS_MP_image22.png

The Arduino's ADC reads this voltage from the analog pin and returns a value in the range of 0-1023. We use the AnalogRead function block to read a value from the analog pin and the WHILE loop to monitor this reading. The WHILE block can be found from the Control tab and the AnalogRead block is located in the Input/Output tab.

Blockyduino_Loops_Functions_SS_MP_image6.png

WHILE loop

Blockyduino_Loops_Functions_SS_MP_image28.png

AnalogRead Function

We have to specify the correct analog pin number in the AnalogRead function. In the WHILE loop, we have to pass a condition block, which can be found in the Logic tab.

In the conditional block, the program has to pass two arguments for comparison and select the mode of comparison. The code written inside the WHILE loop will be executed again and again until the set condition is satisfied.

Blockyduino_Loops_Functions_SS_MP_image21.png

comparison block

Blockyduino_Loops_Functions_SS_MP_image30.png

comparison modes

The flowchart below shows a visual understanding of what we want to achieve.

Blockyduino_Loops_Functions_SS_MP_image23.png
Blockyduino_Loops_Functions_SS_MP_image26.jpg

Copy the auto-generated Arduino code into Arduino IDE

Compile the code and download it onto your Arduino board. Once programming is done, observe LED turning ON/OFF by moving the potentiometer.

Blockyduino_Loops_Functions_SS_MP_image24.gif

Reading the Potentiometer Value and Controlling a Servo Motor

In the previous section, we discussed potentiometers and how to read their value using the AnalogRead block. In this exercise, we are going to use this potentiometer value (0-1023) to set the position of a servo motor.

A servo motor has three pins: Vcc, Gnd, and PWM input. Connect all three pins to Arduino as shown in the diagram below.

Blockyduino_Loops_Functions_SS_MP_image9.png

Servo motors can be controlled using PWM pulses from an Arduino. The AnalogWrite function’s block range is 0-255, so we have to convert the potentiometer’s reading to the output PWM value range. For this, we use the Map function, which is an inbuilt function of Arduino. The Map block can be found in the Math tab.

Blockyduino_Loops_Functions_SS_MP_image29.png

We need to specify the expected output range (0-255). Here is the screenshot of the code:

Blockyduino_Loops_Functions_SS_MP_image7.jpg

Copy the auto-generated Arduino code into Arduino IDE

Compile the code and download it onto your Arduino board. Once programming is done, observe the servo motor’s position change.

Blockyduino_Loops_Functions_SS_MP_image17.gif

A Recap of What We Learned

In this tutorial, we discussed pulse width modulation signals and how to output a PWM signal from Arduino using the AnalogWrite block. By using the FOR loop, we changed the PWM value at every instant and controlled the intensity of the LED.

We also learned about the potentiometer and how to read its analog value using the AnalogRead block. We then monitored the potentiometer’s value using the WHILE loop and turned an LED ON and OFF based on a threshold potentiometer reading.

In the final exercise, we used both the AnalogRead and AnalogWrite blocks to read POT value and control a servo motor. We also learned about the Arduino’s built-in Map function and its uses.

I hope you enjoyed learning more about programming your Arduino using BlocklyDuino. If you have any difficulty while using Blockly, feel free to post in the comment section!

Related Content

Comments


You May Also Like