#include // we connect library for work with the serial peripheral interface #include // connect the library to work with the module NRF24L01 #define horizontalSticPin A0 // pin on which the vertical axis of the joystick is connected #define verticalSticPin A1 // pin of the horizontal axis of the joystick #define horizontalSticCenter 130 // the value "0" on the horizontal axis in the above values #define verticalSticCenter 125 // the value "0" on the vertical axis in the above values #define k 3 // coefficient of deviation from the center #define max_speed 127 // Maximum value of speed. It is used for coding of speed. #define min_speed -127 // minimum value of speed. It is used for coding of speed byte data[2]; // an array that is passed to the robot. Stores "encrypted" engine speeds int speed_left, speed_right; // variables stored in the speed calculations of the left and right engines int speed, horizontal_value; // variables that store average speed and turn speed const uint32_t pipe1 = 123456789; // channel address of this communication module RF24 radio(9, 10); // pins connection with the module. You can use any (ports 15-19 CSN CE MOSI MISO SCK) /*-------------------- encodings function of values of speed ---------------*/ void getValue() { speed = -1 * (analogRead(verticalSticPin) / 4 - verticalSticCenter); // 2. if (speed < k && speed > -1 * k) speed = 0; // 3. horizontal_value = analogRead(horizontalSticPin) / 4; // 4. if (horizontal_value < horizontalSticCenter - k) { // 5. speed_left = speed + (horizontalSticCenter - horizontal_value); speed_right = speed - (horizontalSticCenter - horizontal_value); } else if (horizontal_value > horizontalSticCenter + k) { // 6. speed_left = speed - (horizontal_value - horizontalSticCenter); speed_right = speed + (horizontal_value - horizontalSticCenter); } else { // 7. speed_left = speed; speed_right = speed; } // 8. if (speed_left > max_speed) speed_left = max_speed; if (speed_right > max_speed) speed_right = max_speed; if (speed_left < min_speed) speed_left = min_speed; if (speed_right < min_speed) speed_right = min_speed; // 9. if (speed_left <= 0) data[0] = byte(-1 * speed_left); else if (speed_left > 0) data[0] = byte(128 + speed_left); if (speed_right <= 0)data[1] = byte(-1 * speed_right); else if (speed_right > 0) data[1] = byte(128 + speed_right); } /*--------- function of sending values to the communication module -----------*/ void sendData() { radio.write(&data, sizeof(data)); } /*-------- Ń„function of data output in the terminal ------------------*/ void printData() { Serial.print(speed); // average speed output Serial.print(" "); // output dividing value of a space Serial.print(horizontal_value); // turning speed output Serial.print(" "); Serial.print(data[0]); // left engine speed output Serial.print(" "); Serial.print(data[1]); // right motor speed output Serial.println(); // line feed } void setup() { Serial.begin(9600); // Serial port (terminal) launch at 9600 speed radio.begin(); // launch of communication channel with radio module radio.setDataRate(RF24_2MBPS); // set the baud rate of RF24_1MBPS or RF24_2MBPS radio.setCRCLength(RF24_CRC_8); // setting the length of the checksum 8-bit or 16-bit radio.setPALevel(RF24_PA_MAX); // setting the power level of the RF24_PA_MIN amplifier (minimum), RF24_PA_LOW (low), RF24_PA_HIGH (high) and RF24_PA_MAX (maximum) // Corresponds to levels: -18dBm, -12dBm, -6dBM, 0dBm radio.setChannel(120); // channel setup radio.setAutoAck(false); // auto answer off radio.powerUp(); // enable or reduce powerDown consumption - powerUp radio.openWritingPipe(pipe1); // open channel to send data } void loop() { getValue(); // read and process values sendData(); // send values printData(); // output values to the terminal }