Code

Robot Programming: Part 2 of the Robotics Guide

Robot Programming: Part 2 of the Robotics Guide

Practical course: "Microcontroller Programmer"

Learn More

Preparing the Arduino IDE

The Arduino IDE is one of the most popular and convenient development environments for creating projects on the Arduino platform. In this environment, we will write a program to control the robot. The installation file can be downloaded from the official Arduino website.

In the Arduino IDE, we will create the code and then upload it to the Arduino Uno board using the same program. The template of an empty sketch in the Arduino IDE is presented as follows:

Screenshot: Arduino IDE

The programming language used in the Arduino IDE is is based on C++. C++ is one of the key languages ​​used in robotics and other programming areas. If you plan to delve into the field of robotics, it is highly recommended to study C++ in more detail. This will allow you to expand your skills and increase the efficiency of your work on projects related to Arduino and other platforms.

This sketch contains two functions: setup() and loop(). Let's take a closer look at their purpose. The setup() function is responsible for the initial configuration, where variables are initialized, pins are configured, and other preparatory actions occur. It is executed once when the program starts. In turn, the loop() function contains the main code that will be executed repeatedly. This is where the main execution of the program logic occurs, including data processing, interaction with sensors, and device control. Understanding these functions is key to effective programming on the Arduino platform.

  • In setup(), variables are usually initialized, as well as the modes of connected contacts (pins) using the pinMode() function. The code inside setup() is executed once, immediately after the Arduino starts up, so this method is actually used for the initial setup of the board.
  • The loop() function contains the main code that runs continuously after the setup() function completes.
  • The void parameter before the function indicates the type of return value after the function commands are executed. Translated into Russian, void means “emptiness” — this means that the function does not return anything.

Read also:

Robots: types, structure and areas of application

Robots are complex mechanisms capable of Perform tasks with a high degree of autonomy. There are many types of robots, varying in their functions and purposes. Industrial robots, for example, are widely used in manufacturing to automate assembly processes. Service robots are used in hotels, restaurants, and medical facilities, providing comfort and safety.

Robots consist of several key components: sensors, actuators, controllers, and software. Sensors allow robots to perceive their environment, while actuators provide movement. Controllers direct the robot's actions, while software defines operating algorithms and user interactions.

Robots are being actively implemented in various fields: from agriculture and construction to medicine and education. They help increase process efficiency, reduce costs, and improve the quality of task performance. Technology advances every year, and robots become more versatile, opening up new horizons for application in our lives.

Writing Code

Now we'll look at how to write a program to control our machine. Don't worry, we'll explain in detail and demonstrate the process with examples. If you don't feel like programming today, you can use the ready-made code.

To control the robot, we will use a mobile app to send commands that will be received by the Bluetooth module connected to the robot. Our goal is to develop a program capable of processing these commands. The program will read data from the Bluetooth module, convert it into control signals for the motors, and transmit these signals to the motors, ensuring that the specified actions are performed.

To activate the module, you need to establish communication via the serial port. In the setup() function, add the following line of code:

This command configures the serial port to a specific baud rate, which must match the baud rate of the Bluetooth module. The standard baud rate for the serial port is 9600 bps. Ensuring that these rates match is a key step for the correct operation of the device and stable data transfer.

In the previous section of the instructions, we connected the motor contacts to pins 6-9 on the Arduino. Now we need to specify these pins in the code. To do this, add the appropriate lines to the setup() function.

The pinMode command determines the operating mode of the connected pins in the Arduino. For example, using pinMode(6, OUTPUT); sets pin 6 to output mode, which allows sending control signals from the Arduino board. This is an important step when working with digital devices, since correctly setting the pins ensures correct interaction with external components.

In the void loop() function, we implement the key logic of the program. This is where the main operations that the program should perform on an ongoing basis will be defined. It is important to keep in mind that this loop will execute infinitely, so it is necessary to optimize the code for efficient use of resources. You should also provide for handling possible errors and exceptional situations to ensure stable operation of the program. Pay attention to the use of delays and conditions to avoid unnecessary load on the processor. Proper organization of work in this function will help achieve high performance and reliability of the entire application.

This function checks for the presence of readable data on the serial port, which in our case corresponds to control commands. To implement this check, the Serial.available() method is used. If the data is available, it is read for further processing.

The values ​​of the data attribute in this context can take many forms and values. These values ​​are used to store additional information associated with HTML elements, allowing you to effectively interact with them via JavaScript. Using data attributes helps improve the structure and functionality of a web page. For example, you can use data attributes to convey information about an element's state, identifiers, or any other custom data that may be useful for dynamic content. Thus, data attributes play an important role in the development of modern web applications and help provide a more interactive user experience.

  • ‘F’ (forward): move forward;
  • ‘B’ (backward): move backward;
  • ‘L’ (left): move left;
  • ‘R’ (right): move right;
  • ‘0’ (stop): stop.

When the "forward" command is received, the program activates pins 6 and 7. This ensures the required reaction of the device in response to a given signal. As a result, the activated pins initiate the corresponding actions, which can be used in various projects related to automation and control.

To determine the necessary ports for different movements, you can use an experimental method. It is necessary to activate pairs of pins one by one and observe the direction of movement of the car. This approach allows you to accurately identify which combinations of ports are responsible for each movement, which greatly simplifies the process of setting up and controlling the device.

In Arduino, HIGH and LOW represent logic levels that control the state of the pins. By using digitalWrite(pin, HIGH), we apply a high voltage to the specified pin, which turns it on. By using digitalWrite(pin, LOW), we apply a low voltage, which turns the pin off. These commands allow you to control various components, such as LEDs, relays, and other devices connected to the Arduino. Understanding how logic levels work is the foundation for creating various projects on the Arduino platform, ensuring reliable control of pin states and interaction with external devices.

The movement logic will be implemented similarly for the other directions: left, right, and back.

The finished code for controlling the motors is available below. Please expand the spoiler to view it.

Uploading the Code to the Arduino

Now that our code is ready, we need to upload it to the Arduino board. To do this, connect the Arduino to your computer using a USB cable and launch the Arduino IDE. In the IDE interface, configure the settings for uploading code: select the appropriate board and port. From the list of available boards, select the Arduino Uno, as this is the model we'll be working with. Uploading code to the Arduino is a crucial step in the development process, and proper settings ensure successful compilation and upload of the program to the board.

Screenshot: Arduino IDE

There are several factors to consider when choosing the right USB port. USB ports vary by type and standard, which impacts data transfer speed and device compatibility. The most common USB types include USB-A, USB-B, USB-C, and micro USB. USB-C, for example, offers high data transfer rates and reversible connectivity, making it ideal for modern devices. It's also important to consider the USB version: USB 2.0, 3.0, and 3.1 all have different speed and power specifications. Make sure the port you select matches your devices' requirements to ensure maximum efficiency and performance.

Screenshot: Arduino IDE

Before publishing the finished program, you must run a check for errors. To do this, use the "Check" button.

Screenshot: Arduino IDE

If everything is ready, we will upload code for the board.

Screenshot: Arduino IDE

Read also:

Neural networks in the modern world: AI with unstable behavior and mental health problems of chatbots

Modern neural networks, despite their achievements, sometimes demonstrate unstable behavior, which raises concerns among users. Artificial intelligence created using such systems can exhibit symptoms similar to mental disorders, giving rise to the term "AI schizophrenia." This phenomenon is becoming increasingly relevant in the context of the growing use of chatbots and other forms of AI in everyday life.

An important aspect is how incomplete algorithms can lead to unpredictable results in communication. Incorrect data or training errors can cause AI to exhibit behavior that is perceived as strange or even disturbing. Users should be aware that interactions with such systems may not always reflect real human emotions and thinking.

Therefore, it is important to consider these aspects when developing and using neural networks in order to minimize the risk of problems and increase the level of trust in artificial intelligence.

Setting up control from a smartphone

For this project, the Arduino Bluetooth Controller application on the Android platform was used to interact with the HC-06 Bluetooth module. Similar applications are also available for iPhone, but we cannot guarantee their effectiveness. You may need to independently find suitable alternatives for working with Bluetooth on Apple devices.

Setting up sending commands via Bluetooth from your phone requires several steps. First, make sure Bluetooth is enabled on your mobile device. Then, go to Bluetooth settings and select the device you want to connect to. After a successful connection, you will be able to send commands from your phone to the connected device. If necessary, install the necessary applications to manage the Bluetooth connection. Make sure your device supports the necessary features to handle commands correctly. By following these simple steps, you can easily set up sending commands via Bluetooth from your phone. To get started, download and install the Arduino Bluetooth Controller app on your phone. This app allows you to control Arduino devices via Bluetooth, providing a convenient interface for interacting with projects. Make sure your phone supports Bluetooth and is configured correctly for use with the app. In the interface, you must select the correct device for connecting to Bluetooth—the HC-06 module we're using in this project. After that, enter the default password, 1234. Then select the Gamepad scenario for further configuration.

You will see the control interface Screenshot: Arduino IDE / Skillbox Media

When you press buttons in the application interface, data should be transmitted to the Arduino via Bluetooth. This will enable wireless communication between the device and the controller, which is essential for creating an intuitive and functional app. Using Bluetooth for data transfer ensures a stable connection and a fast system response to user commands. Using the Arduino Mega controller instead of the standard Arduino Uno allows you to observe the process of sending commands from a mobile device to the robot board. This allows for more efficient robot control and expands design options. The Arduino Mega provides a larger number of inputs and outputs, making it an ideal choice for complex projects. It is important to note that successful communication between the smartphone and the board requires proper software configuration and the use of appropriate libraries. To complete this task, upload the verification code to the board and run it. This will allow you to test and make sure the device is working properly.

Instead of results

Congratulations on successfully assembling and programming your first robot. Now you have the opportunity to control it yourself. You are ready for new experiments and discoveries in the world of robotics. Your skills will develop, and you will be able to create more complex projects. Start exploring the functionality of your robot and bring your ideas to life.

This project helped us develop key skills in robotics. We improved our ability to assemble devices, mastered working with the Arduino microcontroller, and learned how to connect and control motors. These skills create a solid foundation for more complex projects in the future.

What's Next

Now you can customize your machine without limits, using all the possibilities of your imagination. For example, you can connect it to light sensors, a camera, a microphone, and a manipulator for capturing various objects. Such improvements not only expand the functionality of the device, but also open up new horizons for creativity and experimentation.

Different versions of wheeled robots based on Arduino Screenshot: Kuongshun / AliExpress / Supereyes

Some enthusiasts are integrating artificial intelligence into Arduino-based projects using the APIs of well-known neural networks. You can take it a step further and teach your robot to conduct a conversation using ChatGPT by adding a small speaker. Perhaps you'll be the one to create the first Terminator. The use of AI in robotics opens up new horizons and possibilities for creating interactive devices.

Microcontroller Programmer

You will learn to write code for devices that control robots, drones, and other electronics. You can start a career in one of the most promising areas of the IT industry.

Find out more