# Создание подписчика

Аналогичным образом мы можем создать подписчика на Arduino, который получает данные из ROS и обрабатывает их.

```c
#include <ros.h>
#include <std_msgs/Empty.h>

ros::NodeHandle nh;

void messageCb( const std_msgs::Empty& toggle_msg){
  digitalWrite(13, HIGH-digitalRead(13));   // blink the led
}

ros::Subscriber<std_msgs::Empty> sub("toggle_led", &messageCb );

void setup()
{
  pinMode(13, OUTPUT);
  nh.initNode();
  nh.subscribe(sub);
}

void loop()
{
  nh.spinOnce();
  delay(1);
}
```

Приведенный выше скетч подписывается на топик `toggle_led` И при получения сообщения меняет уровень напряжения на ноге `d13` к которой "подключен" светодиод.

Для проверки загрузим код на Arduino и начнем публиковать данные к топик.

```
rostopic pub /toggle_led std_msgs/Empty "{}" -r 2
```

Параметр `-r 2` задает частоту публикации данных 2Гц. Это значит что наш светодиод должен переключаться из состояние "включен" в состояние "выключен" каждые 500мс.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://voltbro.gitbook.io/arduino-ros/4_ros_arduino/sozdanie-podpischika.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
