We will be needing several new display devices for our following experiments. We only worked with regular LEDs before, and now we are going to work with some new ones. Let’s figure out how to work with a piezo buzzer first, and we shall
have some auditory indication. Let’s look at the circuit that I’ve assembled and try to see what it
can do. This is what a piezo buzzer looks like, which is sometimes also simply called
a buzzer or a beep tone – it’s all the same. It only has two outputs: one of them I have connected to the “ground” rail, which is subsequently connected to Arduino’s “ground” input, whilst the second one is connected to a digital pin, through which we
will be controlling the buzzer. In order to control the buzzer, I have once again connected the light sensor, and now we are going to conduct an experiment which will look a lot
like a Thereminvox experiment. Obviously, the way this thing works is somewhat different from the way a
a Thereminvox operates: in our experiment, we will be covering the photo resistor from the light to
change the pitch of the piezo
buzzer. Let’s look at the sketch that I’ve prepared. We have determined macrodefinitions for the buzzer and the photo resistor. The pin for the buzzer is determined as our output. Now I’m going to use the Arduino in-built tone function to generate a signal of a certain frequency for the buzzer, because we won’t hear any sound if we simply turn on the digital signal. That’s why Arduino library already has this built-in tone function to help
us work with the buzzer. It uses 3 parameters. Firstly, we tell it which pin we need and where the buzzer is connected. Secondly, we let it know what frequency in Hz should be communicated. As we can see, I am reading the analog input data here and multiply it by 4, because the frequency between 0 and 1024 will be poorly reproduced by the piezo
buzzer. However, if we multiply our diapason by 4, it will be perfect for the buzzer, because we will be able to produce sound at the diapason which is up to 4 kilohertz. The third parameter is not an obligatory one, and it’s the time value. I’ve marked 10 milliseconds here, so the loop will be changing
really fast. In fact, we don’t really need to enter this parameter. In our situation, the noTone function would be more useful, and I have added some comments here, so that we could have it in front of our
eyes just to know what it looks
like. It turns off power at the pin that you determine, if you don’t specify the time value at once. Now let’s try to start this process and see what we will get. [BUZZ] We can hear a sound like this one, which will be changing depending on the amount of light falling upon the photo resistor. Frankly speaking, this does not sound quite like music. I strongly recommend that you have pity on people around you and avoid writing tunes using the piezo buzzer. Besides, don’t even try connecting several buzzers at once: you are not going to achieve polyphony anyway. You can use piezo buzzers for auditory indication. It might not always be convenient for you to look at your device, but instead you will be able to communicate information about
some events, which happen in your program, with the help of sounds of various pitch and time value. Moving on. The next display device is the so-called LED package, a bar graph in our case. It’s called an LED package, because it consists of a dozen of LEDs united under one package. There are no other peculiarities apart from this one inside. Thus, it has 20 headers with each separate LED having 2 headers. And don’t forget that they are all of different length here, so you will have to figure out where the “plus” and “minus” are in an experimental way. I know that in my circuits, anode, or “plus” is on the same side as the labelling is. I am inserting the bar graph in the breadboard like this. Every LED should have a resistor connected to it, otherwise they
will burn out. At the “plus” end, I have connected them to Arduino’s digital outputs
from 2 to 11. Let’s try not to use outputs 0 and 1, like I said before. I have prepared the following sketch to test the bar graph. This is a classic running light which appears when we turn the LEDs on one by one. What’s curious about it? To begin with, I decided not to determine macrodefinitions for
all the pins and for each LED, because we are going to work with each of them
in a row. That’s why I’ve only marked the pin which has the first LED out of all
10 connected to it, and the last pin. You are going to see why now. I have created one more macrodefinition – lag time, aka the time during which each LED will be on. Now let’s see: instead of writing the pin mode function for each of the
10 pins, through which we control the bar graph, I have created a cycle. Here we have some new details to consider. Firstly, the counter can begin with something other than 0, right? I am giving it the value of LED1 here, which in our case equals 2. The counter will continue to run until it’s ≤ the number of the last pin. Let me draw your attention to this section, where I am increasing the
counter’s value not in the way I used earlier: i = i + 1. Here I’ve just written i + + , which is the so-called increment operator: in increases the variable by 1. One more thing: I used to pay special attention to curly brackets and to the fact that they should always be in the place, where a certain fragment of the code needs to be highlighted. They also
should come in a pair. However, to improve readability and make our lives easier, we
can do one simple thing. If you need to perform only one instruction in the program
control command in the For-
operator, curly brackets can in reality be omitted. The same thing can be done with the If-operator. Thus, I have placed the pin mode function in a cycle, but curly brackets are absent before and after it. If you find it a bit hard to understand, make a rule of writing curly brackets everywhere. We shall then determine all the necessary outputs as outputs by passing the i counter variable to the pin mode function. Thus, the second pin will become the output, then the third one, etc,
all the way to the eleventh pin. What happens in the loop? In the loop, we have one more For-cycle. And I am going to show you one more thing here. This is another way of increasing the variable, meaning that i + = 1 is the same thing as i + + and i = i + 1. Such abbreviated notation is also suitable for other arithmetic
operators, so we can write «- = », «* = », «/ = », if you need to work with the value of one and the same variable.
For instance, i * = 2, which means that every time i will be multiplied by 2. Inside this cycle, we will be turning on each LED one by one, and then we shall wait for some time (it is mentioned in the LED_ON function) and turn them off. Next, we turn to the next operation, and the next LED turns on. Let’s see how this works. We are getting a running light like this. We wrote down the parameters of the LEDs which turn on for 200 milliseconds and then turn off – this is the sequence that we can witness in a cycle. Now let’s make out of this bar graph a real scale which increases or decreases depending on the increasing or decreasing values. Let’s make it so that out bar graph increases or decreases with the rotation of the potentiometer’s knob. I have reconnected in to the circuit: I’ve plugged it in the minus of the
same rail, which has all the resistors plugged in it, and which is also connected to
Arduino’s ground rail. As far as the plus in concerned, I have brought it out to the plus rail and
then to the potentiometer’s output so that the wires would not get entangled. Let’s see what sketch we are getting. According to what logic does it operate? Last time, we were turning all 10 LEDs from this bar graph one by
one, and now we need to turn on either 2, 5 or all 10 LEDs, but not all at the same time. We will need to determine which LED to turn on last. This will depend on the value the potentiometer’s knob points at. How will I realize this? Well, I have added a line with a macrodefinition for the analog
input, and I’ve left setup unchanged. So now we can see a new line where we can try out a new useful Arduino language oriented function. I have created a lastLed variable, which stores the number of the last pin that needs to be turned on. I am now going to save in it whatever the
“map” function brings back. The function is created to transfer the value from one diapason to that of another diapason. As we already know, we can get values from 0 to 1023 at the analog input, while at the output we need to get values from 2 to 11. This means that one of the pins from 2 to 11 will be the last one. What does the “map” function take as parameters? First, it needs to be assigned a value with which it’s going to work. In our situation, this value is brought back by the analogRead function: it simply reads off data at the analog input. It then needs to be assigned 2 values: they are going to signify the
beginning and the end of the
input range, aka the range of values which the first argument can take. The fourth and the fifth parameters signify the beginning and the end
of the output range. What does it mean exactly? If analogRead with ANALOG_IN give us 0, it will turn into 2, which will mean that the beginning and the end of the input and
output ranges coincide. If we put in 1023, we will get 11, since “map” will turn it into this value. It will actually turn any intermediate value into an intermediate value from the output range. It will also bring back the integer, meaning that it will round it up to the closest integer. Thus, we are going to get the number of the LED in the lastLed variable, the LED which needs to be turned off last. What happens next? Then we again have a cycle with a counter, only this time it does not change until an invariable or any fixed value, but until the lastLed
value, which needs to be calculated every time, and which will change depending on the rotation of the
potentiometer's knob. Thus, if with the help of the “map” function we can figure out that
the last LED should be turned on on pin 7, then the cycle will be run until the value of 7. Inside the cycle, we can turn the pin on and off, and its number will equal the current value of the counter. We shall then create a delay with a LED_ON value. In our case, I have assigned LED_ON a very low value, only a couple of
milliseconds. This signifies that each of our LEDs will be on only for a short period
of time. However, since they are turned on and off very frequently, we won’t even notice that they blink really fast. Let’s see what it will look like. This is the bar graph that we get: it’s a very handy display device, and by this time, you might have already understood that you can
enter any value that you have calculated. This means that it’s not necessary to display what you get at the input. Now let’s move on to the next LED package, which is a seven-segment
display.