Last time I left you at the setting your Arduino for Mac or Windows and download a simple test app that flashes a built-in LED. Today I am going to explain the code you have uploaded, the structure of the Arduino software and a bit more about the electronic bits on the board itself.

This article is part of an introduction to the Arduino series. Other articles in this series:

  • What is an Arduino and what can you do with it What is an Arduino and what can you do with it ?
  • What is an Arduino starter kit and what does it contain?
  • More cool components to buy with your starter kit.
  • Getting Started with the Arduino Starter Kit — Installing Drivers and Configuring the Board and Port

Hardware

Let’s take a closer look at what the Arduino Uno has in terms of bits on the board.

Here is an enlarged diagram for reference:

arduino board

  • Along the top are 14 digital inputs/outputs (numbered 0-13). These are the most versatile pins on your Arduino that can work as both input and output and will be the backbone of your projects. Digital means that the signal that these pins can write or read will be turned on or off.
  • 6 of these digital pins marked with a tilde ~ are capable of doing what is called pulse-width modulation. I’m not an electrical engineer, so I won’t be embarrassed to explain the science behind this, but for you and me, this means that we can provide a range of output levels — like dimming an LED, or driving a motor at different speeds. ,
  • Pin 13 is different in that it has a built-in LED. This is for convenience and testing purposes only. You can use this built-in LED, as you did in the Blink application example, by simply outputting to pin 13 — or it can be used as a standard input/output pin.
  • At the bottom right are 6 analog input pins. They will read the values ​​of analog sensors such as a light meter or variable resistors.
  • On the bottom left next to the analog input pins are the power pins. The only thing you really need to worry about is the ground (GND) lines, the 3.3V and 5V supply lines.
  • Finally, the only switch found on the Arduino is the reset switch. This will restart any program in its memory.
  • The Arduino has a certain amount of memory, and if your program gets too big, the compiler will give you an error.

Arduino Program Structure

Every Arduino program consists of at least two functions (if you don’t know what a function is, be sure to read my Basic Programming Tutorial Part 2 — Function and Control Operators and Part 1 where we discussed variables before continuing).

The first is the customization feature. This runs initially — once only — and is used to tell the Arduino what is connected and where, as well as initialize any variables you might need in your program.

Second cycle. It is the core of every Arduino program. When the Arduino is running, after the setup function is complete, the loop will loop through all the code and then do it all over again — until either power is lost or the reset button is pressed. The time it takes to complete one complete cycle depends on the code. You can write code that says «wait 6 hours» in which case the loop won’t repeat very often.

Here is a brief state diagram to illustrate:

arduino board

Exploring the Blink Program

Look at the Blink code and identify the setup and loop functions.

Here are the settings:


 void setup () {// инициализируем цифровой вывод как выход.  // К выводу 13 подключен светодиод на большинстве плат Arduino: pinMode (13, OUTPUT);  } 

Lines starting with //, are just comments to explain the code to the reader and are not loaded into the Arduino. In fact, there is only one line of setup code in this particular Arduino application. This line says «Set pin 13 to output mode». 13, remember, this is a built-in LED.

Then there is a loop:


  void loop () {
 digitalWrite (13, ВЫСОКИЙ);  // установить светодиод
 Задержка (1000);  // подожди секунду
 digitalWrite (13, LOW);  // выключаем светодиод
 Задержка (1000);  // подожди секунду
 } 

The comments at the end of each line of code explain their function quite well. HIGH and LOW refer to the ON and OFF state of the digital output — in our case an LED. You could also write ON or OFF in code, both are synonyms (just like 0 and 1). Delay tells Arduino to wait a bit, in this case 1000 milliseconds (or 1 second).

Finally, a note about the programming language used here. Please note that before the setup and loop functions, before the word » void» . This is a special word for nothing because the function returns nothing when called — it simply runs the code contained within. For now, let’s leave it at that, saying that the function’s code block is enclosed in curly braces {}, and that each line of code must end with an a; semicolon.

Try modifying the underlying program somehow by changing the exact delay values ​​to something more or less. See how little you can get before the blinking is no longer noticeable. Determine which value needs to be changed so that it stays longer or longer. Try adding a few more digitalWrite and delay statements to the loop function to create a more complex flashing pattern, such as Morse code for SOS. If you have a buzzer, try connecting it to pins 13 and GND (hint: red wire goes to 13, black wire goes to ground).

It’s all for today. Next time we’ll add a few more LEDs and write our own application from scratch. As always, comments and shares are greatly appreciated. I can’t imagine you’ll have any problems with the code we’re linking to today, but if you’ve tried to modify the code a bit and run into bugs or unexpected behavior, feel free to post it in the comments and we’ll see if we can get through it’s together.

Похожие записи