#include // connection library to work with a serial peripheral interface #include // connection library to work with a serial peripheral interface #include RF24 radio(7, 8); // pin assignment of communication with the module const uint32_t pipe = 123456789; // setting the address of the radio module in the channel byte data[2]; // array of values received from the radio module // pins to which the engine driver is connected #define M1 6 #define M2 9 #define M3 3 #define M4 5 #define speed_border 128 // speed range separation boundary /*-------------- engine control function ----------------------------------------*/ void Motor(bool directoin_left, byte speed_left, bool direction_right, byte speed_right) { //(left direction, left speed, right direction, right speed) 1 is forward, 0 is back if (directoin_left == 1) { digitalWrite(M2, 0); // set the direction of movement analogWrite(M1, speed_left); // set the rotation speed } if (directoin_left == 0) { digitalWrite(M1, 0);// set the direction of movement analogWrite(M2, speed_left); // set the rotation speed } if (direction_right == 1) { digitalWrite(M4, 0); // set the direction of movement analogWrite(M3, speed_right); // set the rotation speed } if (direction_right == 0) { digitalWrite(M3, 0);// set the direction of movement analogWrite(M4, speed_right); // set the rotation speed } } /*-------------------------- function "decrypt" the accepted values ---------------------------------*/ void drive() { if (data[0] <= speed_border) { if (data[1] <= speed_border) Motor(0, data[0], 0, data[1]); else if (data[1] > speed_border) Motor(0, data[0], 1, data[1] - speed_border); } else if (data[0] > speed_border) { if (data[1] <= speed_border) Motor(1, data[0] - speed_border, 0, data[1]); else if (data[1] > speed_border) Motor(1, data[0] - speed_border, 1, data[1] - speed_border); } } /*-------------------------- function "decrypt" the accepted values with multiplication ---------------------*/ void driveHigh() { if (data[0] <= speed_border) { if (data[1] <= speed_border) Motor(0, data[0]*2, 0, data[1]*2); else if (data[1] > speed_border) Motor(0, data[0]*2, 1, (data[1] - speed_border)*2); } else if (data[0] > speed_border) { if (data[1] <= speed_border) Motor(1, (data[0] - speed_border)*2, 0, data[1]*2); else if (data[1] > speed_border) Motor(1, (data[0] - speed_border)*2, 1, (data[1] - speed_border)*2); } } /*----- function of receiving data from the radio module ---------*/ void getData() { if (radio.available()){ // if new data is available radio.read(&data, sizeof(data)); // read them into the data array } } /*---- output function to terminal ----------*/ void printData() { Serial.print(data[0]); Serial.print(" "); Serial.print(data[1]); Serial.print(" "); Serial.println(); } void setup(){ Serial.begin(9600); // launch of the terminal (serial port) with a speed of 9600 radio.begin(); // launch of the 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.openReadingPipe(1, pipe); // channel opening on reception radio.startListening(); // start receiving data // assigning driver pins to the output pinMode(M1, OUTPUT); pinMode(M2, OUTPUT); pinMode(M3, OUTPUT); pinMode(M4, OUTPUT); } void loop(){ getData(); // read values drive(); // processing values printData(); // output values to the terminal }