# LEGO NXT C Programming

{% hint style="info" %}
This article is automatically translated from Russian by Google Translator.
{% endhint %}

Text programming for LEGO NXT is done in C using macros and functions from the ECRobot library.

The general structure of the program:

* First you connect the header files used in the program (standard C header files and ECRobot header files).
* Then the constants and variables used in the program are declared.
* These are followed by the functions `ecrobot_device_initialize` and `ecrobot_device_terminate`. The first one is called when the program starts and the second one when it stops. They usually initialize and deinitialize sensors, encoders, and other robot devices. The random number generator is initialized in `ecrobot_device_initialize` by default.
* This is followed by the description of the `user_1ms_isr_type2` function, which is called every millisecond.
* Then comes the description of the task, which is executed when the program starts: `TASK (OSEK_Task_Number_0)`. This is where most of the programming is done.

```c
#include <string.h>
#include "kernel.h"
#include "kernel_id.h"
#include "ecrobot_interface.h"
#include "trik_studio_utils.h"
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <math.h>

U32 __interpretation_started_timestamp__ = 0;
static const float pi = 3.14159265;



void ecrobot_device_initialize(void)
{
	srand(systick_get_ms());

}

void ecrobot_device_terminate(void)
{

}

/* nxtOSEK hook to be invoked from an ISR in category 2 */
void user_1ms_isr_type2(void)
{

}

/* Main task */
TASK(TASK_MAIN)
{
	__interpretation_started_timestamp__ = systick_get_ms();

	TerminateTask();
}
```

{% hint style="info" %}
[Full description](http://lejos-osek.sourceforge.net/api.htm) of functions in English available for programming.

The code generated by TRIK Studio itself can be used as examples.
{% endhint %}


---

# 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://help.trikset.com/en/nxt/c.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.
