The Dwenguino is programmed in a textual programming language C++ within the Arduino IDE environment. However, for those who are insecure about starting with textual programming, a user-friendly graphical programming language is made available. It allows the user to acquire the different components of programming languages. One such language is Blockly. In this tutorial we get you started with graphical programming of the Dwenguino in Blockly.
- A Dwenguino board;
- A male to male, USB to mini-USB cable;
Installing the DwenguinoBlockly Plugin
DwenguinoBlockly is a Java plugin for Arduino IDE which is based on the Google Blockly project. We have adapted Blockly to make it easy to program the Dwenguino. If you want to use it, you will need to download DwenguinoBlockly first (it might take up to a minute before the download starts), and unzip it in the tools folder of your Arduino workspace. If the tools folder does not exist yet in your Arduino workspace you will need to make it. After installing Blockly you should be able to see it in the Tools menu of the Arduino IDE. Click on DwenguinoBlockly to start the graphical programming environment. It is a good idea to connect the Dwenguino before starting DwenguinoBlockly and choose Tools > Board > Dwenguino and connect to the Dwenguino by selecting the correct port.
When you open DwenguinoBlockly you should see a graphical programming environment with an new sketch in front and the available blocks on the left:
By dragging a block from the left to the sketch you can program your Dwenguino. In the menu Dwenguino you can find the Dwenguino compatible blocks. Similar to the Arduino sketch, we start with a program block which has a setup and loop block. The code in the setup segment is executed once when your program starts. The code in the loop block is repeated infinitely, until you stop the program. Now lets try and print a “Hello World!” message to the LCD with the LCD block:
Now we are ready to upload our program to our Dwenguino. Therefore, click on the red arrow button in the top right of your DwenguinoBlockly window. If you did not forget to connect and select the Dwenguino before starting DwenguinoBlockly, you will see the Hello World! message on your LCD!
A blinking LED example
As you know the Dwenguino has 9 programmable LEDs. One LED can be found on pin 13 which can be set with the Build-in LED State Block. To set the blinking frequency, we use the Delay block:
The other LEDs can be found on pins 32 to 39. With the LEDS block the eight LEDs can be controlled directly:
Note that we used a integer variable here to start a counting process.
You can connect easily two different kind of motors to your Dwenguino. The first type is a servo motor which you need to attach to the servo connector SERVO 1 or SERVO 2. Please connect the black wire of the servo to the minus (-) and the red wire to the plus (+). The third, coloured wire, should be connected to the leftover pin marked with a pulse. DwenguinoBlockly provides a servo block which can be dragged from the Dwenguino menu. You need to provide the pin the servo is connected to (chann#) and the angle the servo motor should be positioned in. If you want to control the servo on pin SERVO 1 you need to configure the channel (chann#) as a 1, for SERVO 2 it should be 2. The angle can be anything between 0 and 180 degrees. Here SERVO 1 is set to 90 degrees:
When you use the servo-block in DwenguinoBlockly, Pulse Width Modulation (PWM) will be used to set the servo-pin of your choice. Depending on the pulse width, the position of the servo motor will be set:
The second type of motor you can connect is a standard DC motor, which can be connected to blue motor connector (4 screw pins) pairwise marked with M1 and M2. The speed of the motor is controlled with a PWM signal.
The motor channel and speed can be set by using the Dc Motor block. The maximal speed that can be chosen is 255. This means that the motors will get the battery input voltage. In the following program two motors are controlled at maximal speed but into opposite directions:
Making sound with the buzzer
The Dwenguino has an onboard buzzer connected to pin 46 to make music. The frequency of the buzzer can be set with the Tone on pin block. This block takes two arguments: the pin to which the buzzer is connected (for the on board buzzer you can use the default Pin: BUZZER) and the tone frequency (for example 440 Hz which is a A4 note). To stop the tone you can use the No tone on Pin block. To play a tone for a certain amount of time you have to use a Delay block as shown here:
In the following program the tone is switched from 440 Hz to 220 Hz and vice versa every second:
The sonar sensor
If you connect a sonar sensor to the Dwenguino board, two of the connections are used to power the sensor, the other two connections make communication between the sensor and the board possible. The two communication pins are called trigger (trig#) and echo (echo#). If you know which pins on the board are connected to the trigger and echo pin on the sonar sensor, you can use the Sonar block to read the distance in cm. The following example shows how to read the distance from the sonar sensor, save it into a variable and display it on the LCD screen:
More advanced blocks
By default DwenguinoBlockly only shows a subset of all possible blocks, this makes starting to program a bit less overwhelming. However, when you feel more comfortable, you can switch to the Advanced mode through the dropdown in the top right corner:
When you switch to advanced mode, two new menus will appear in the block toolbox on the left: Functions and IO. Also, additional options will be available under the Variables and Math menus. Under the Math menu a new map block appears. This block allows you to map a value within a certain range to a value in another range. This can be useful when working with analog input or output (see later). Under the Variables menu we now have typed variables. And we can choose to set a local or a global variable:
The Functions menu allows us to create function with or without a return type:
And last but certainly not least, the IO menu adds blocks for analog and digital communication on every pin on the Dwenguino board.
Press the buttons
The easiest way to interact with your Dwenguino board is by using one of the five buttons: SW_C, SW_N, SW_E, SW_S and SW_W. In order to get the buttons working, you only need to use the DigitalRead PIN# block. The argument of the DigitalRead PIN# block determines from which digital pin you want to read. This can be one of the buttons (SW_C, SW_N, SW_E, SW_S and SW_W) or any other digital pin. After that you can evaluate whether a button is pressed or not with an if...else statement:
Notice we can change the layout of the if do block by clicking the blue gear icon in the top left of the block. This allows us to add else if and else statements to the if do block:
Analog to digital conversion
A lot of sensors are analog: the phototransistor, long range distance sensors of Sharp, the short range distance sensor. For a few overview how you can connect we refer to the sensors for robots tutorial. When the sensor is properly connected it should have one pin connected to one of the Dwenguino A/D pins (pin A0 to A5). You can then read the analog sensor with the AnalogRead PIN# block in which you have to select the Pin: (A0 toA5) to which your sensor is connected. In the following example, an analog sensor connected to pin A3 determines whether the LEDS will be on or off:
The Arduino code view
In advanced mode, a new Arduino tab appears at the top of the workspace:
Clicking this tab will show the Arduino C++ code generated from the blocks you put together. This can be used to show students the textual equivalent of the code they just wrote. This is a handy tool during the learning process, especially when you want to make the switch to textual programming:
You can not edit the textual code in this view, if you want to edit the code you should press the upload arrow button and continue editing the Arduino IDE.