# Testando conexão (BLE+mRobots)

## Hardware necessário:

* mBattery
* mCookie Core+
* mCookie Hub
* LED colorido (qualquer cor)

Becoming a super hero is a fairly straight forward process:

```
$ give me super-powers
```

{% hint style="info" %}
&#x20;Super-powers are granted randomly so please submit an issue if you're not happy with yours.
{% endhint %}

Once you're strong enough, save the world:

```c
#include <Microduino_Protocol.h>
#include <Microduino_Motor.h>

#define _DEBUG  //DEBUG调试

#define BLE_SPEED 57600  //velocidade transmissão da porta
#define CHANNEL_NUM   8  //número de canais 
#define SAFE_TIME_OUT 250   //timeout de segurança
#define MAX_THROTTLE 255 //valor output acelerador < 255
#define MAX_STEERING 512 //valor máximo direção  < 512
#define CHANNEL_THROTTLE  1 //definir canal do acelerador
#define CHANNEL_STEERING  0 //definir canal da direção

Motor MotorLeft(MOTOR0_PINA, MOTOR0_PINB);
Motor MotorRight(MOTOR1_PINA, MOTOR1_PINB);

#define ProSerial Serial
ProtocolSer bleProtocol(&ProSerial, 16);  //utiliza ProSerial，protocol de 16 bytes

///////////////////////////////////////////////////////////
uint16_t channalData[CHANNEL_NUM]; //8 canais (ver define linha 7)
int16_t throttle = 0; //acelerador
int16_t steering = 0; //direção
unsigned long safe_ms = millis();

uint8_t recCmd;

void setup() {

  bleProtocol.begin(BLE_SPEED);
  pinMode(D6,OUTPUT);
  MotorLeft.begin();   //valor inicial motor esquerdo
  MotorRight.begin();  //valor inicial motor direito
}

void loop() {
  if (bleProtocol.available())
  {
    bleProtocol.readWords(&recCmd, channalData, 8);

    throttle = map(channalData[CHANNEL_THROTTLE], 1000, 2000, -MAX_THROTTLE, MAX_THROTTLE);
    steering = map(channalData[CHANNEL_STEERING], 1000, 2000, -MAX_STEERING, MAX_STEERING);

    MotorLeft.setSpeed((throttle + steering / 2));
    MotorRight.setSpeed(-(throttle - steering / 2));

#ifdef _DEBUG
    Serial.print("DATA OK :[");
    for (int a = 0; a < CHANNEL_NUM; a++) {
      Serial.print(channalData[a]);
      Serial.print(" ");
    }
    Serial.print("],throttle:");
    Serial.print(throttle);  //+153
    Serial.print(",steering:");
    Serial.println(steering);
    if(abs(throttle)>128)
    {
      digitalWrite(D6,HIGH);
    }else
    {
      digitalWrite(D6,LOW);
    }
#endif
    safe_ms = millis();
  }

  if (safe_ms > millis()) safe_ms = millis();
  if (millis() - safe_ms > SAFE_TIME_OUT) {
    MotorLeft.setSpeed(FREE);  //definir a posição do motor esquerdo como livre，ou seja velocidade=0
    MotorRight.setSpeed(FREE); //definir a posição do motor direito como livre，ou seja velocidade=0
  }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://microduinobrasil.gitbook.io/mdesigner/mrobots/carro-controlado-por-app-ble+mrobots.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.
