Conrad 10213 Operation Manual

Page 1
Arduino Advent Calendar 2016
The programming of microcontrollers was previously only something for engineers and computer scientists. Thanks to clear hardware and easy­to-understand software, Arduino suddenly makes it possible for anyone to get into microcontroller technology.
The Name Arduino
Arduino comes from Italy and was named after the Italian King Arduino, who ruled until the year 1005 in Ivrea, where the Arduino manufacturer has its headquarters. Today, the favourite bar of Arduino developers Massimo Banzi and David Cuartielles there is named after King Arduino.
The Arduino platform now offers a wide variety of boards for various uses. The Arduino Uno is the board best suited for beginners and also one of the most high-performance Arduino boards.
This Advent calendar contains a hardware experiment every day for the Arduino Uno or compatible boards of another manufacturer. Some of the compatible boards have the advantage over the original Arduino Uno that they use a micro-USB connection and thus standard smartphone cables can be used. The original Arduino Uno, by contrast, has a USB Type B connection.
The experiments on this Advent calendar are programmed with Snap!. This programming language is based on Scratch, one of the easiest programming languages to learn ever.
Downloads for the Advent Calendar
You can find the programmes used at http://www.buch.cd for download. For this product, enter the code 10213-1 in the input field.
Breadboard
For the fast construction of electronic circuits without having to weld, a breadboard is on the Advent calendar on the first day. With it, electronic components can be plugged directly into a hole matrix.
On this breadboard, the outer long rows are all connected to each other via contacts (X and Y). These contact rows are often used as positive and negative terminal for power supply for the circuits. In the other contact rows, five contacts each (A through E and F through J) are connected across to each other, whereby there is a gap in the centre of the board. Thus, larger components can be plugged into the centre and wired in from outside.
LEDs
LEDs (LED = Light Emitting Diode) can be connected at the digital pins for light signals and light effects. For this purpose, a 220-ohm series resistor (red­red-brown) must be installed between the pin used and the anode of the LED, in order to limit the current flowing through and thereby prevent the LED from burning through. Plus, the series resistor also protects the digital output of the Arduino, because the LED offers almost no resistance in the flow direction and thus the pin could quickly be overloaded upon connection with earth. Connect the cathode of the LED to the earth wire of the Arduino.
In which direction do I connect the LED?
The two connection wires of an LED are of different lengths. The longer one is the positive terminal, the anode; the shorter is the cathode. Easy to see: The plus sign has one line more than the minus sign and thereby makes the wire visibly somewhat longer. Beyond that, most LEDs are flattened on the negative side, like a minus sign. Easy to remember: Cathode = short = edge.
RGB LEDs
A normal LED always lights up in only one colour. The RGB LEDs contained on the Advent calendar can selectively light up in multiple colours. They are, in principle, three LEDs with different colours installed in a transparent housing. Each of these three LEDs has its own anode, through which it is connected to a digital output. The cathode, which is connected to the earth wire, is present only once. Therefore an RGB LED has four connection wires.
The connection wires of the RGB LED are different lengths so that they can be clearly recognised. Unlike with normal LEDs, the cathode is the longest wire here.
RGB LEDs function like three individual LEDs and therefore also require three series resistors.
Page 2
Resistors and their colour codes
Resistors are used to limit current to sensitive electronic components and also as series resistors for LEDs. The unit of measurement for resistors is the ohm. 1,000 Ohm equals one kilo-ohm, abbreviated kOhm. 1,000 kOhm equals one mega-ohm, abbreviated MOhm. The letter omega is
also frequently used for the unit ohm.
The coloured rings on the resistors indicate the resistance value. With some practice, they are much easier to recognise than teeny tiny numbers that one can still find on very old resistors.
Most resistors have four such coloured rings. The first two coloured rings indicate the digits, the third a multiplier and the fourth the tolerance. This tolerance ring is usually in gold or silver, colours that do not appear on the first rings. Thus, the reading direction is always clear. The tolerance value itself plays hardly any role in digital electronics. The table shows the meanings of the coloured rings on resistors.
The direction in which a resistor is installed does not matter. With LEDs, on the other hand, the installation direction plays an important role.
Connection cables and jumper wire
The coloured connection cables have a thin wire plug on each end, with which they can be plugged into the socket strips of the Arduino and on the breadboard.
Later on, a jumper wire is included on the Advent calendar. With this wire, you create short connection bridges, with which contact strips on the breadboard are connected. Cut the wire with a small pair of side-cutting pliers to the suitable length depending on the experiment. In order to be better able to plug the wires into the breadboard, we recommend cutting them off at a slight angle so that a sort of wedge results. Strip the insulation from both ends to a length of about half a centimetre.
Precautions Never connect any random Arduino pins to each other and wait to see what happens. Not all Arduino pins can be freely programmed. Some are permanently set up for the power supply and other purposes. Some Arduino pins are directly connected to connections of the microcontroller; a short circuit can completely destroy the Arduino – at least theoretically. The Arduino boards are astonishingly stable against circuit faults. If one connects two pins together through an LED, a protective resistor must always be placed between them. Always use the 3.3 V pin for logical signals. The 5 V pin is used to supply power to external hardware. Here (almost) as much current can be drawn as the connected mains adapter supplies. However, this pin must not be connected to a digital input.
Day 1
Today on the Advent calendar
• Breadboard (SYB 46)
• Red LED
• 220-ohm resistor (red-red-brown)
• 2xconnection cables
Preparing the Arduino
In order to put the Arduino into operation, one needs:
• PC with Windows
• USB cable (Type B)
• Arduino IDE
The connection between the PC and Arduino occurs via a USB cable with the almost square Type B plug on one side. You do not need to buy this type of cable separately; virtually all modern printers use this plug type, and while you are experimenting with the Arduino, you are hardly going to print at the same time.
Don't connect the Arduino yet Do not connect the Arduino to the PC right away; rather, install the Arduino software first. Otherwise, there could be problems later with the driver installation.
Software installation in brief
For programming the Arduino, the manufacturer delivers a very clear development environment in which one can write the programmes, which
Page 3
are designated as Sketch by Arduino, in a programming language similar to C. This Arduino IDE also creates the connection between the PC and Arduino.
For everyone who does not have the Arduino ready to operate, here is the system installation in seven steps:
1 Download the Windows installer for the current Arduino IDE at www.arduino.cc/en/Main/Software or simply use the file arduino-1.6.8-
windows.exe from the downloads for the Advent calendar.
2 Install the Arduino IDE before you connect the Arduino to the PC for the first time. The necessary drivers are thereby automatically installed
with it and the Arduino is later recognised automatically. In doing so, make sure that in the dialogue field Installation Options , all check boxes are checked. Depending on your Windows configuration, a confirmation of the user account control is required.
3 After the installation, start the Arduino IDE and then connect the Arduino using a USB cable. The driver is now automatically installed and
simulates via USB a serial port to which the Arduino is connected.
4 After the driver is installed and not before, go to the menu of the Arduino IDE and select Tools/Port. In most cases, only a single serial port is
shown. Select the check box here.
5 Then, via the menu option Tools/Board, select the Arduino/Genuino Uno, if this was not automatically recognised.
6 In the menu, select File/Examples/Firmata/StandardFirmata. A new window of the Arduino IDE opens with the StandardFirmata sketch.
7 Click on the Upload icon in the top toolbar (the round symbol with the arrow pointing to the right). The software is now compiled and
transferred to the connected Arduino.
If you use Linux or Mac OS X: On the website www.arduino.cc/en/Guide/HomePage you will find installation notes for the Arduino IDE for these operating systems.
Lighting the LED
No separate program is needed for the first experiment. The Firmata test program switches an LED on and off and at the same time, thereby also tests whether StandardFirmata is functioning.
Components: 1xbreadboard, 1xred LED, 1x220-ohm resistor (red-red-brown), 2xconnection cable
Connect the LED as shown in the figure via a series resistor to Pin 5 of the Arduino.
Now start the programme firmata_test.exe from the Downloads for the Advent Calendar. In the Port menu, select the port used by the Arduino. Control elements for the pins of the Arduino will then appear.
The Firmata test programme causes the LED to light up.
Click on the Low button in the Pin 5 line; the LED switches on and the button switches to High. Click on it again; the LED switches off again.
For status indicators without additional hardware, the Arduino also has its own LED. This can be controlled via Pin 13. Click on the Low button in the Pin 13 line; this LED switches on off.
Switching off the Arduino The Arduino does not have an off switch. You need only pull the plug, and it switches off. The next time you switch it on, the last stored program starts automatically – thus in our case the StandardFirmata. The same thing happens when one presses the Reset button.
Day 2
Today on the Advent calendar
• Green LED
• 220-ohm resistor (red-red-brown)
• Connection cable
Alternating flashing light
A simple programme allows two LEDs to flash alternately.
Page 4
Components: 1xbreadboard, 1xred LED, 1x green LED, 2x220-ohm resistor (red-red-brown), 3xconnection cable
The programme
For the projects on the Advent calendar, we use the easy-to-learn programming language Snap4Arduino. Download the current version at
s4a.cat/snap or simply use the file Snap4Arduino-aktuell.exe from the Downloads for the Advent Calendar.
In Snap4Arduino, click on the Settings icon and select Language in the menu.
The Settings menu in Snap4Arduino
Select "English" in the list.
Before you can begin with the programming, a connection to the Arduino must be created. To do this, click on Arduino in the top left in the block palette and then on Connect to Arduino. When you open a new programme in Snap4Arduino, the connection to the Arduino will often be lost. Should an error occur when you start a new programme, reconnect the PC to the Arduino as described.
Snap4Arduino supports multiple Arduinos on one PC. As long as only one Arduino is connected, it is recognised immediately and the connection is created.
The connection to the Arduino was successfully created.
The programmes for the Advent calendar
You can download the programmes for the Advent calendar at www.buch.cd or simply put them together yourself each day using the figure. Unzip the ZIP file from the download in a folder on the hard drive. Then click on the Files icon at the top left in Snap4Arduino and select Import to import the programmes, which are in XML format, into Snap4Arduino. After the import, click once in the menu on Save. After that, the programme is available in the software's own library, which can be reached through the Open menu item.
In Snap4Arduino, one does not need to type in any programme code when programming. The blocks are connected to each other simply via drag-and-drop. The block palette in the left side of the window contains the available blocks, organised by themes.
The first programme uses the most important blocks:
When green flag clicked on from the Control palette forms the start for most of the programmes.
continuing from the Control palette is an infinite loop that is constantly repeated.
wait... sec from the Control palette causes the program to wait a certain amount of time before the next step.
Set digital pin... to... from the Arduino palette sets one of the digital pins of the Arduino to a logical value true or false. These two values can be
found as blocks on the Operators palette.
The programme starts when the user clicks on the green flag at the top right.
A continuing loop ensures that the two LEDs flash alternately indefinitely – until the user clicks on the red Stop icon at the top right in Snap4Arduino.
After the LED at Pin 5 is switched on, 0.1 seconds elapses so that StandardFirmata does not "swallow" a command. A minimum wait time should always be built in between the setting of two pins. After that, the LED at Pin 8 is switched off. Now the programme waits half a second.
Note: Decimal point
Snap4Arduino, like many American programmes, uses the point as the decimal separator, not the comma that is usual in Germany.
After that, in the same way, the LED at Pin 8 is switched on and the one at Pin 5 is switched off. After another half second, the cycle repeats from the beginning.
Day 3
Page 5
Today on the Advent calendar
• Yellow LED
• 220-ohm resistor (red-red-brown)
• Connection cable
Stop light
The experiment of the third day switches a traffic light made of three LEDs in their typical cycle of red through red-yellow to green and through yellow back to red.
Components: 1xbreadboard, 1xred LED, 1x yellow LED, 1x green LED, 3x220-ohm resistor (red-red-brown), 4xconnection cable
The programme
The programme functions similarly to the one from yesterday. Here, too, various combinations of LEDs are switched on and off in sequence in an infinite loop. In the intermediate phases red-yellow and yellow, the traffic light lights up for 0.5 seconds each time, in the red and green phases 3 seconds each. These times can also be set differently in the wait...sec blocks.
The delay time between the apparently simultaneous switching of multiple LEDs was reduced to 0.01 seconds, which is technically sufficient and is practically no longer recognisable by the observer.
The programme 03ampel01 causes the LEDs to flash alternately.
Day 4
Today on the Advent calendar
• RGB LED
RGB LED flashes colourfully
The experiment for the fourth day causes an RGB LED to flash in a series of different colours.
Components: 1xbreadboard, 1xRGB LED, 3x220-ohm resistor (red-red-brown), 4xconnection cable
The programme
The programme functions similarly to the one from yesterday. Here, too, various digital pins are switched on and off in sequence in an infinite loop. In this case, the three colour components of the RGB LED are involved.
Additive colour mixing
RGB LEDs use the so-called additive colour mixing. The three light colours red, green and blue are added and give pure white at the end. A colour printer, by contrast, uses the subtractive colour mixing. Each colour acts as a filter on a white page that removes (= subtracts) a portion of the white reflected light. If one prints all three printer colours over one another, the result is black, which no longer reflects any light at all.
In the programme, through alternating switches on and off, the LED alternates constantly between having one colour component lit and having two lit. The RGB LED thereby alternates back and forth between six different colours.
The programme 04rgb01 causes the LEDs to flash alternately.
The speed of the colour change is controlled through a variable z, which is set to a specific value at the beginning of the programme and then applies for each colour change.
Variables in Snap4Arduino
Variables are small memory spaces in which one can mark a number or anything else during a programme. When the programme is ended, this variable memory is automatically cleared again. Variables must first be set once in Snap4Arduino before one can use them.
Page 6
Day 5
Today on the Advent calendar
• Red LED
Dimming the LED
Components: 1xbreadboard, 2xred LED, 2x220-ohm resistor (red-red-brown), 3xconnection cable
LEDs are typical components for the output of signals in digital electronics. They can take on two different states: on and off, 0 and 1 or false and true. The same is true for the digital pins defined as outputs. Thus, in theory, it would not be possible to dim an LED.
However, it is possible to use a trick to regulate the brightness of an LED at a digital pin. If one allows an LED to flash fast enough, the human eye no longer perceives it as flashing. The technology known as pulse width modulation (PMW) generates a pulsing signal that switches on and off in very short intervals. The voltage of the signal always remains constant; only the ratio between the false level (0V) and true level (+3.3V) is changed. The duty cycle determines the ratio of the length of the switched-on state to the total duration of a switch cycle.
The smaller the duty cycle, the shorter the time is that the LED is lit within a switch cycle. The LED thus appears darker than an LED that stays switched on.
Pins for PWM signals
Pins 3, 5, 6, 9, 10, 11 are marked on the Arduino with a "~" symbol. These pins can be used for pulse width modulation.
The programme
The programme 05pwm01 dims the two LEDs brighter and darker in a cycle. At the beginning, two variables are defined for this purpose: bright designates the PMW value for the brightness of the LED and step gives the step width for dimming. The current values of both variables are shown in real time at the top right of the platform.
Now an infinite loop begins. As the first step in every pass, the current value of the variable bright is outputted as the PWM value at Pin5 and then after a short delay also at Pin6. Then the value of the variable bright is increased by the value step.
In the next step, the programme checks whether the value of bright has reached the limits 0 or 100. In this case, an or block is inserted, which in turn has space for two further queries. If at least one of these two is true, the or block returns the value true and the content of the if block is executed.
Two equals queries check whether the variable bright has reached the value 0 or 100. If this is the case, the variable step is set to a new value. Because Snap4Arduino does not offer the option to reverse the sign of a variable, we use the operator '-' and subtract the value of the variable from 0, which gives the same result.
At the end, the program waits 0.025 seconds. Then the infinite loop begins again and delivers the LEDs a new PWM value.
Day 6
Today on the Advent calendar
• 2xconnection cables
These connection cables will not be needed until the next few days.
Mixing colours with PWM
Components: 1xbreadboard, 1xRGB LED, 3x220-ohm resistor (red-red-brown), 4xconnection cable
The programme
The programme 06pwm02 dims the colour components of an RGB LED brighter and darker in a cycle. Various mixed colours are thereby generated. The current values of all variables are shown in real time at the top right of the platform.
Page 7
The variables bright_r, bright_g and bright_b store the PWM brightness values of the three colour components, the variables step_r, step_g and step_b the step widths in which the PWM values are changed. In this way, the variables switch between +5 and -5.
In the first 20 steps, the red component is reduced stepwise from 100 to 0 and the green component simultaneously increased from 0 to 100, whereby various mixed colours between red and green arise. After that, the signs of the two step values are reversed; the green component is reduced and thereby the blue component is increased. In the last loop, blue is then reduced again and red increased. Then the infinite loop starts again from the top. In all, 60 different colours can be seen.
Day 7
Today on the Advent calendar
• Clay
• 20-Mohm resistor (red-black-blue)
• Bare jumper wire
The jumper wire is needed for various experiments. Have a pair of wire cutters handy so that you can cut off the suitable wire pieces.
Switching LED with clay sensor
Components: 1xbreadboard, 1xred LED, 1x220-ohm resistor (red-red-brown), 1x20-Mohm resistor (red-black-blue), 4xconnection cable,
2xclay contact
The right clay contact (labelled in the image with a minus sign) is the earth contact. Always hold this one in your hand during the test.
How sensor contacts work
The pin switched as input is connected through an extremely high-ohmic resistor (20 Mohm) to +3.3V, so that a signal, weak but clearly defined as High, is present. A human who is not currently hovering in mid-air is always earthed and provides a Low level through the electrically conductive skin. If this person touches a sensor contact, the weak High signal is masked by the significantly stronger Low level of the fingertips and draws the GPIO pin to the Low level.
However, how high the resistance between hand and earth actually is depends on many factors, including shoes and floor. The connection to the earthing of the ground is best when one is barefoot in the wet grass, but it usually functions well on stone floors, too. Wood floors are more strongly insulating; synthetic floorings are often even positively charged. So that the circuit always works, there is – similar to the case with sensor buttons on lifts and doors – also an earth contact installed on the breadboard for every circuit. If one touches this and the actual sensor at the same time, the earth connection is created in every case.
Clay conducts current about as well as human skin does. It can easily be put into any form desired and a clay contact feels much better than a simple piece of wire. The surface with which the hand touches the contact is significantly larger. Thus, a "loose connection" is less likely to occur. Cut a piece of the bare jumper wire around 10cm long and stick in a piece of clay. Insert the other end, as shown in the figure, into the breadboard.
Because Snap4Arduino always switches on the pulldown resistors built into the Arduino, digital inputs are always drawn down to 0 and have a Low level even without being touched. However, the Arduino also has analogue inputs that are very good for sensor contacts. Analogue inputs deliver values between 0 (Low level) and 1023 (High level). The value 200 is a good limit value to differentiate between a sensor contact that is being touched and one that is not being touched.
The programme
The programme 07knete01 switches the LED on when the clay contact is being touched and off again when it is released. The variable x always shows the current value of analogue Pin5 on the platform. If it is lower than 200, the LED at digital Pin 7 is switched on; otherwise it is switched off.
Day 8
Today on the Advent calendar
• 20-Mohm resistor (red-black-blue)
• Connection cable
Crab crawl
Page 8
Components: 1xbreadboard, 1xred LED, 1x220-ohm resistor (red-red-brown), 2x20-Mohm resistor (red-black-blue), 5xconnection cable,
3xclay contact
The experiment of the eighth day is a quick game on the PC monitor that uses the Arduino as a small game pad with two clay contacts. In the game, a grab runs round a circular track and should deviate from its course as little as possible in the process.
The programme
When touched, the two clay contacts turn the crab left or right by 5 degrees. It automatically runs forward and only needs to be kept on the correct course by the turns.
At the beginning, the crab is placed at the Start position and the movement direction is set to 0° upward. In an infinite loop, the crab always goes two steps in the current movement direction. Afterwards, three conditions are queried.
If the red or green shovel of the crab touches the white surface outside the track, the error counter is increased by 1 and the LED lights up briefly.
If the player touches the clay contact at analogue Pin 5, the crab is turned by 5° counter-clockwise; if the contact at analogue Pin 3 is touched, the crab is turned by 5° clockwise.
Day 9
Today on the Advent calendar
• 20-Mohm resistor (red-black-blue)
• Connection cable
Controlling the RGB LED with clay contacts
Components: 1xbreadboard, 1xRGB LED, 3x220-Ohm resistor (red-red-brown), 3x20-Mohm resistor (red-black-blue), 8xconnection cable,
1xwire bridge, 4xclay contact
Three clay contacts control the three colour components of an RGB LED. If the corresponding sensor is touched, the colour is switched on. In this way, seven different colours are possible, the three primary colours, three secondary colours and white as a mixture of all three colours.
The programme
An infinite look constantly queries the three clay contacts in sequence. If a contact is touched, the corresponding colour component of the RGB LED is switched on, or else it is switched off if it was previously switched on. The sequential query of the clay contacts used here has the advantage over the parallel query that it cannot cause any timeout problems if two colour components are switched on at the same time.
The programme 09rgb02 controls the three colour components
of one RGB LED via clay contacts.
Day 10
Today on the Advent calendar
• Green LED
• 2x 220-ohm resistor (red-red-brown)
Pedestrian light
Components: 1xbreadboard, 2xred LED, 1x yellow LED, 2x green LED, 5x220-ohm resistor (red-red-brown), 1x20-Mohm resistor (red-black-
blue), 8xconnection cable, 2xclay contact
When you touch the clay contact, the typical traffic light cycle of a pedestrian light starts. In the resting state, the pedestrian light is red, the traffic light green.
Page 9
The programme
When you click on the green flag, the light is placed in the starting state. The pedestrian light is red, the traffic light green. When you touch the clay contact, the light cycle runs through. After one cycle, the program waits until the clay contact is touched again.
The programme 10ampel02 controls a pedestrian
light with a clay contact.
Day 11
Today on the Advent calendar
• Yellow LED
• 220-ohm resistor (red-red-brown)
Chaser light
Components: 1xbreadboard, 2xred LED, 2x yellow LED, 2x green LED, 6x220-ohm resistor (red-red-brown), 7xconnection cable
Chaser light with six LEDs
Chaser lights are always favourite effects, not just for advertising and party rooms. The experiment of the eleventh day lets six LEDs run as a chaser light.
The programme
The programme 11lauflicht uses a list variable, in which the pin numbers of the digital pins that are used for the LEDs are stored. Unlike in Scratch, lists in Snap do not need to be filled by the programme but can be preallocated with starting values directly in the programme code.
A loop runs over the length of the list and switches the LEDs on individually in sequence for 0.1 seconds each. This loop is repeated infinitely.
The programme 11lauflicht controls a
chaser light using a list variable.
Day 12
Today on the Advent calendar
• Blue LED
• 220-ohm resistor (red-red-brown)
• Insulated jumper wire
Today there is insulated jumper wire on the Advent calendar. With it, you create short jumpers with which contact rows on the breadboard are connected, whereby there is no danger that short circuits with other components exists. Cut the wire with a small pair of side-cutting pliers to the suitable length. In order to be better able to plug the wires into the breadboard, we recommend cutting them off at a slight angle so that a sort of wedge results. Strip the insulation from both ends of each jumper to a length of about half a centimetre.
LED dice
Components: 1xbreadboard, 2xred LED, 2x yellow LED, 2x green LED, 1x blue LED, 7x220-ohm resistor (red-red-brown), 1x20-Mohm resistor
(red-black-blue), 7xconnection cable, 5x insulated jumper (varying lengths), 2xclay contact
Switching LED die with seven LEDs using clay sensor
Everyone knows the typical gaming die, which shows 1 to 6 spots, and everyone has one at home. Far cooler is an electronically controlled die that causes the spots to light up with the push of a button – and not just 1 to 6 LEDs in a row, but in the pattern of a gaming die. These have spots in the typical square pattern, for which one needs 7 LEDs. To control the LEDs, only 4 digital pins are needed instead of 7, since a die uses
Page 10
the spots in pairs to display even numbers.
In the circuit, both side rails of the breadboard are used for earth lines. Therefore, the clay contact is connected somewhat differently this time.
How are random numbers generated?
It is commonly thought that nothing can be random in a programme – so how can a programme be in a position to generate random numbers? If one divides a large prime number by any value, the result after the x-th decimal place are numbers that are practically impossible to predict. These also change without any regularity when one increases the divisor regularly. This result does indeed appear to be random, but it can be reproduced at any time by an identical programme or by calling up the same programme multiple times. However, if one takes a number made up of some of these digits and divides it again by a number given by the current seconds or the content of any desired memory cell of the computer, an answer results that cannot be reproduced and can thus be considered a random number.
The programme
When the user clicks on the green flag, an infinite loop starts that constantly checks whether the clay contact is being touched. If this is the case, the four pins used for LEDs are first switched off to delete the previously displayed roll of the die.
Then a random number between 1 and 6 is stored in the variable w. For each possible result, there is a separate if… block that switches on the corresponding LEDs.
The programme 12wuerfel01 simulates a six-sided gaming die.
Day 13
Today on the Advent calendar
• 220-ohm resistor (red-red-brown)
This resistor will not be needed until the next few days.
LED dice with realistic rolling effect
Components: 1xbreadboard, 2xred LED, 2x yellow LED, 2x green LED, 1x blue LED, 7x220-ohm resistor (red-red-brown), 1x20-Mohm resistor
(red-black-blue), 7xconnection cable, 5x insulated jumper (varying lengths), 2xclay contact
An actual die does not immediately show the final number of spots; rather, it first rolls for a short time in which one sees results that do not then prove true. The program of Day 13 simulates the rolling in which the die first shows a few other results with minimal but ever increasing pauses between them before the final result appears.
The programme
The programme uses the same rolling routine but does not roll only once after the clay contact is touched; rather, it rolls four times in a row, whereby the wait times increase each time by 0.2 seconds before the result is deleted.
The programme 13wuerfel02 rolls with realistic rolling effect.
Day 14
Today on the Advent calendar
• 15-kohm potentiometer
Dimming LED with potentiometer
Required components: 1xbreadboard, 1xyellow LED, 1x220-ohm resistor, 1x15-kohm potentiometer, 4xconnection cable, 1xinsulated
jumper
LED dimmer with potentiometer
To truly dim an LED like a living room lamp, we use the potentiometer from the Advent calendar for today. With this adjustable resistor, one can
Page 11
form a voltage divider that can deliver any desired voltage between 0V and +5V. However, this analogue voltage cannot be passed on directly to the LED, but rather needs to be converted into a digital PWM value.
The analogue inputs of the Arduino evaluate an analogue voltage value and deliver digital values between 0 and 1023. Here 0 stands for 0V and 1023 for +5V voltage at the respective pin. These values must be converted to values between 0 and 255 for them to then outputted again as PWM value at an LED.
The programme
An infinite loop reads the value of analogue Pin 5 and divides it by 4. The result is stored in the variable x and displayed on the platform. Then the PWM Pin9 is set to this value.
The programme 14dimm01 dims an LED
using the setting of the potentiometer.
Day 15
Today on the Advent calendar
• 2xconnection cables
Analogue level indicator with LEDs
Components: 1xbreadboard, 2xred LED, 2x yellow LED, 2x green LED, 1x blue LED, 7x220-ohm resistor (red-red-brown), 1x15-kohm
potentiometer, 10xconnection cable, 1x insulated jumper
Level indicator with seven LEDs
Now you know how to control the brightness of an LED with analogue values. To really visualise an analogue value, however, this method is not expressive enough – brightnesses are perceived very subjectively and depend heavily on the surroundings and the LEDs used. Analogue values can be read much more clearly on a level indicator.
The programme
The numbers of the pins used for the LEDs are stored in a list. The analogue Pin 5 is continually queried and its value, which can lie between 0 and 1023, is divided by 128 to get a value between 0 and 8 and thereby switch on 0 to 7 LEDs.
An inner loop over the length of the list checks whether the value p calculated in this way is higher than the current number of the LED. If yes, the LED is switched on.
The programme 15pegel controls an LED strip
using the setting of the potentiometer.
Day 16
Today on the Advent calendar
• 7-segment indicator
Controlling a 7-segment indicator with the Arduino
7-segment indicators can display the digits from 0 to 9 as well as the letters A through F, which are necessary to represent hexadecimals, on seven LEDs. The 7-segment indicator on the Advent calendar also has, as many such display modules do, an eighth LED for the decimal point. The seven LEDs for the digit representation are labelled with the letters a through g. The LED marked DP represents the decimal point. With a single-digit 7-segment indicator, each LED can be controlled individually via its anode. All LEDs of a 7-segment indicator of the Common Cathode type use a common cathode, similar to RGB LEDs.
Components: 1xbreadboard, 1x7-segment indicator, 7x220-ohm resistor (red-red-brown), 8xconnection cable, 1xinsulated jumper
Page 12
The programme
If one presses a number key on the keyboard, the 7-segment indicator shows the corresponding digit. The Delete key deletes the display.
No number pad
Snap4Arduino supports only the digit keys in the top row of the QWERTY keyboard. The number keys of the number pad cannot be used.
The programme consists of multiple script blocks, each of which reacts to each of the keystrokes. On the platform at the top right, you will find a schematic diagram of the 7 segment display with the labels of the individual segments (a-g). This graphic has no function for the programme; it serves only as an overview.
When you click on the green flag, two list variables are initialised:
s contains the pins an which the seven segments from a to g are connected.
z contains ten other lists, each of which contains a series of true and false operators, that determine which segments of the 7-segment indicator should be switched on for the respective digits. The list begins with the digit 1; the digit 0 is represented by the tenth list element. This method is used because objects in Snap lists are always counted beginning with 1 – not with 0 as in many other programming languages.
Ten script blocks expect that one of the ten digit keys will be pressed. Depending on the key pressed, the variable n is set to the corresponding number, for the 0 key to 10. After that, the message display is sent.
When the message display has been received, the most important script block starts; this block presents the digit stored in the variable n on the 7-segment display.
Script block for display of a number
The loop in this script block runs 7 times through and thereby indicates the seven LED segments stored in the list element n of the list z. The associated pins are read from the list s.
To clear the 7-segment display, a separate script block is used that starts when the Delete key is pressed. Here a similar loop runs through 7 times and sets all segments to false.
The Delete key clears the 7-segment indicator.
Day 17
Today on the Advent calendar
• Clay
With the second piece of clay, you can now make different-coloured clay contacts, in order to clearly differentiate them.
Counter with 7-segment indicator
Components: 1xbreadboard, 1x7-segment indicator, 7x220-ohm resistor (red-red-brown), 1x 20-Mohm resistor (red-black-blue), 2x clay
contact, 10xconnection cable, 1xinsulated jumper
Each time you touch the clay contact, the counter increases by 1. The 0 automatically follows again after the 9.
The programme
Like the programme from yesterday, the programme first defines two lists with the pin numbers for the seven LED segments and the patterns for the ten digits.
The variable n always contains the number to be displayed on the 7-segment indicator, where the 10 stands for the digit 0 in order to display the tenth list element.
So that the counter does not race ahead when the clay contact is touched for a longer period of time, the programme waits first until the clay contact is touched and then until it has been released again. Only then does the counter increase by 1.
At the beginning of every pass, a send ... to all block again calls up a script block to display one of the digits defined in the list z. This script
Page 13
block corresponds to the one from the programme from yesterday.
Script block for display of a number
Day 18
Today on the Advent calendar
• 220-ohm resistor (red-red-brown)
This resistor will not be needed until the next few days.
Random die roll with 7-segment indicator
Components: 1xbreadboard, 1x7-segment indicator, 8x220-ohm resistor (red-red-brown), 1x 20-Mohm resistor (red-black-blue), 2x clay
contact, 11xconnection cable, 1xinsulated jumper
The circuit is largely the same as that from Day 17. The decimal point of the 7-segment indicator is also connected here.
The programme
In addition to a classic six-sided die, the programme also simulates dice with two to nine sides, such as a coin toss. You can adjust the value of the variable w using a sliding switch on the platform. This gives the highest value of the potential number range.
When one touches the clay contact, the last result is first deleted from the display. To this end, the main programme sends the message off, which starts a script block that switches off all LED segments in one loop.
This block clears the 7-segment indicator.
Then the die appears to roll, which is simulated by the flashing of the decimal point on the 7-segment indicator. When one releases the contact, a random number appears that is displayed again via the script block you already know.
Script block for display of a number
Day 19
Today on the Advent calendar
• 2xconnection cables
These connection cables will not be needed until the next few days.
Level indicator with 7-segment indicator
Components: 1xbreadboard, 1x7-segment indicator, 7x220-ohm resistor (red-red-brown), 1x 15-kohm potentiometer, 10xconnection cable,
2xinsulated jumper
The analogue value of a potentiometer is represented in this level indicator as a digit between 0 and 9.
The programme
This time the programme contains only one script block, which also contains the loop for displaying the digits on the 7-segment indicator. The potentiometer can deliver different values in quick succession by being turned slightly. Because no external routine is called up to represent the number, there is also no risk of timing problems if the number is not yet fully displayed and a new value is already read from the potentiometer.
The programme 19_7seg04 delivers a level indicator on the 7-segment indicator.
Page 14
An infinite loop reads the value of the analogue Pin 0 and rounds it to a digit between 0 and 9. This value is stored in the variable n and, if it is 0, set to 10 in order to display the tenth list element. Then the loop you already know switches on the LED segment that matches the digit.
Day 20
Today on the Advent calendar
• 220-ohm resistor (red-red-brown)
Countdown for flashing light with 7-segment indicator
Components: 1xbreadboard, 1x7-segment indicator, 10x220-ohm resistor (red-red-brown), 1x 20-Mohm resistor (red-black-blue), 2x clay
contact, 11xconnection cable, 4xinsulated jumper (varying lengths)
When the clay contact is touched, a ten-second countdown starts on the 7-segment indicator, after which the RGB LED flashes white briefly. For this purpose, all three anodes of the RGB LED are connected to the same Arduino pin with series resistors.
The programme
The main program loop waits first until the clay contact is touched and then causes a countdown to run from 9 to 1. At the end, the 7-segment indicator is set to 0 and the LED is switched on for 0.1 seconds.
The same method you know from earlier programmes is used again to display the digits.
Script block for display of a number
Day 21
Today on the Advent calendar
• 20-Mohm resistor (red-black-blue)
This resistor will not be needed until the next few days.
Guess the number with 7-segment indicator
Components: 1xbreadboard, 1x7-segment indicator, 7x220-ohm resistor (red-red-brown), 3x 20-Mohm resistor (red-black-blue), 4x clay
contact, 12xconnection cable
The game generates a random number that the player must guess. You can use the clay contacts to increase or decrease the number that is your own guess by steps. The clay contact marked with the square submits the guess.
The programme
In an infinite loop, a secret number x is first randomly defined. The displayed starting number is likewise randomly defined and stored directly in the variable n, which is used as in past programmes for presentation on the 7-segment indicator. The player's guess is stored in the variable y. The main loop of the game runs until this guess is equal to the number sought.
At the beginning of each pass, the currently set number n is shown in the 7-segment indicator. After that follow three if queries that check the three clay contacts.
If the contact at Pin 2 is touched, the number shown is increased by 1. An internal if query prevents this number from becoming greater than 9.
If the contact at Pin 1 is touched, the number shown is decreased by 1. Here, too, the number is prevented from falling below 1.
Page 15
Because the main loop of the game runs very fast and does not wait for user inputs, it could turn out that the script block to display a number is called up before the display of the last number has been completely cleared. To prevent this, the programme uses send ... to all and wait blocks. This waits until the subroutine that has been called up has finished running.
If the contact at Pin 0 is touched, the player has entered a guess. The variable y used for the guesses is set to the currently adjusted number n and the 7-segment indicator is switched off. Depending on whether the number sought is larger or smaller than the last guess, the topmost or bottommost segment of the 7-segment indicator flashes briefly three times.
Then the main loop of the game starts again and shows the last number guessed again. If the victory condition of the game is fulfilled, the guessed number flashes five times. Then a new game round begins with a new secret number.
The script blocks you already know are used to display a number and to clear the 7-segment indicator.
Day 22
Today on the Advent calendar
• 20-Mohm resistor (red-black-blue)
Tennis game
Today for a change, we have another game that runs on the PC monitor and is controlled by clay contacts on the Arduino.
Components: 1x breadboard, 4x 20-Mohm resistor (red-black-blue), 5x connection cable, 6x clay contact
With some dexterity, the player must attempt to hit the ball back constantly with his or her paddle so that the ball does not fly against the red or green wall. Each player has two clay contacts in order to move his or her paddle in the two directions. There is also an earth contact for each player in order to have as few accidents from uneven earthing as possible.
If the ball touches the player's own wall because he or she has not put the paddle in the way in time, the opponent gets one point. The ball then starts again at the starting position. The game can be stopped at any time by clicking on the red Stop icon. Whoever has more points has won.
The programme
The ball is controlled by four script blocks that all run simultaneously and are started when the user clicks on the green banner.
The main script creates the basic requirements for the game when you click on the green flag. The two point counters are set to 0 first. Then the ball is put in its starting position at x:0 y:0. The ball should be released at a random angle. For this purpose, the direction is set to a random value between -20 and -160.
Then the motion of the ball is continually repeated. It bounces off the edge when it touches it. Otherwise it flies four steps in the set direction. This movement repeats infinitely in theory. However, because three other script blocks are started when the green banner is clicked, other motions can still result.
The main script block for the initialisation and motion of the ball
If the ball touches one of the two paddles, the movement direction is converted to the negative. The ball continues flying downward in the same angle at which it came from above, or vice versa. To keep the motion somewhat unpredictable, the ball is first moved five steps so that in every case it no longer touches the ball afterwards. Then the flight direction is changed compared to the previous direction by a random value between
-20 and 20 degrees.
If the ball does not touch the paddle but rather touches the green bar on the right edge, the left player receives a point. Then the ball is returned to its starting position and turned by a random value between -20 and 20 degrees so that it never takes exactly the same flight path twice but still flies roughly in the same direction in which it last flew. The same thing happens when the ball touches the red bar on the left edge; the only difference is that this time the right player gets the point.
This script block is executed if the ball touches the left red or the right green edge.
In Snap4Arduino, each Snap object can be connected to exactly one Arduino. The Player1 paddle thus evaluates all four clay contacts and forwards the results accordingly. When creating the connection with the Arduino, therefore, this object must be selected.
Page 16
When you touch the clay contacts at Pin3 and Pin2, the left paddle Player1 should move. In addition, the messages Player1up and Player1down are sent, which are evaluated by the same object and converted into upward or downward movements.
Evaluating clay contacts and moving paddle "Player1"
The Player2 paddle cannot access the same Arduino and thus simply receives the messages that the Player1 paddle sends when the clay contacts at Pin1 and Pin0 are touched. The paddle moves upward or downward accordingly. When the green flag is clicked, it is moved to the starting position and then waits for messages.
The movements of the paddle "Player2"
Day 23
Today on the Advent calendar
• RGB LED
Flashing Christmas lighting
Components: 1xbreadboard, 2xRGB LED, 6x220-ohm resistor (red-red-brown), 7xconnection cable, 2xinsulated jumper
Two RGB LEDs flash randomly.
The programme
The programme shows how little programming effort is needed in Snap4Arduino to make two RGB LEDs flash in random colours. The numbers of the pins used for the LEDs are stored in a list. An infinite loop sets a random pin selected from the list to true and then another randomly selected pin to false. So all possible colour combinations can arise.
The programme 23blink causes two
RGB LEDs to flash in bright colours.
Day 24
Today on the Advent calendar
• Piezo buzzer
The piezo buzzer contained on the Advent calendar today makes electrical oscillations audible. If one applies a pulsing direct voltage between the two terminals of the buzzer, it starts to vibrate. Depending on the frequency, individual clicks or a continuous tone can be heard. The human ear still hears frequencies of a few Hertz (vibrations per second) as individual tones; frequencies between around 20 Hertz and 16 kHertz are perceived as continuous tone of varying pitches.
Installing BuzzerFirmata
Snap4Arduino does not support a piezo buzzer as standard because this is also not supported by the base software StandardFirmata on the Arduino. In order to generate tones with the piezo buzzer from Snap4Arduino, we use our own version of the current StandardFirmata, which is expanded by the functions from the Snap4Arduino Firmata library (github.com/mdcanham/Snap4Arduino-Firmata).
The installation runs similarly to the standard installation of the Arduino described on the first day:
1 First quit Snap4Arduino and start the Arduino IDE.
2 Go to File/Open and load the file
BuzzerFirmata25.ino from the Downloads for the Advent Calendar in the Arduino IDE.
3 Click on the Upload icon in the top toolbar – this is the round symbol with the arrow pointing to the right. The software is then compiled and
transferred to the connected Arduino.
Page 17
4 Exit the Arduino IDE and start
Snap4Arduino again.
Christmas music
Components: 1x breadboard, 5x 20-Mohm resistor (red-black-blue), 6x connection cable, 6x clay contact, 1x piezo buzzer
The five clay contacts generate five different tones. Naturally, the Arduino can play any tones with the piezo buzzer. Many catchy tunes result from five tones. Plus, these can easily be played with the five fingers of one hand without one having to switch the keys with the fingers.
Thus, for example, you can play the well-known Christmas tune "Jingle Bells":
Jin-gle bells, jin-gle bells
e e e e e e
jin-gle all the way e g c d e
Oh what fun it is to ride in a f f f f f e e e
one horse o-pen sleigh. Oh! e d d d d g
Jin-gle bells, jin-gle bells e e e e e e
jin-gle all the way e g c d e
Oh what fun it is to ride in a f f f f f e e e
one horse o-pen sleigh. e d d d d
The programme
At the start, the programme defines five variables with the frequencies of the five tones used. Then an infinite loop waits for one of the clay contacts to be touched. If this is the case, a tone is played and the programme waits for the contact to be released again. Then the tone is ended.
So that only one tone at a time can always be played, the clay contacts are queried in a loop in sequence and not in script blocks launched in parallel. It is thus impossible for the program to attempt to play multiple tones at the same time.
Snap4Arduino offers the option to define your own blocks and execute JavaScript functions in order to thereby access the components defined in StandardFirmata. The programme for Day 24 contains two such blocks:
To n e a t P i n . . . w i t h f r e q u e n c y . . . plays a tone with a specific frequency through a piezo buzzer that is connected to the stated pin.
Stop tone at Pin ... stops the playing of one tone and thus makes it possible to play a new tone. Before one tone can be played, the playing tone must
always be stopped first.
Loading...