Emisor TX
#include <SPI.h>#include "Mirf.h"#include "nRF24L01.h"#include "MirfHardwareSpiDriver.h"Nrf24l Mirf = Nrf24l(10, 9);int value;int contador=0;int MOV=1;void setup(){Serial.begin(9600);Mirf.spi = &MirfHardwareSpi;Mirf.init();//Set your own address (sender address) using 5 charactersMirf.setRADDR((byte *)"CONTR");Mirf.payload = sizeof(value);Mirf.channel = 90; //Set the channel usedMirf.config();}void loop(){Mirf.setTADDR((byte *)"ROB01"); //Set the receiver addressvalue = MOV; //adelante MOV=1Mirf.send((byte *)&value); //Send instructions, send random number valuewhile (Mirf.isSending()) delay(1); //Until you send successfully, exit the loopdelay(200);contador=contador+1;if(contador>=50){MOV=2;}if(contador>=100){MOV=1;contador=0;}}
Receptor RX
//Receiver program#include <SPI.h>#include "Mirf.h"#include "nRF24L01.h"#include "MirfHardwareSpiDriver.h"Nrf24l Mirf = Nrf24l(10, 9);int value;int mov;//MOTOR 1const int mot1_A=6;const int mot1_B=5;//MOTOR 2const int mot2_A=4;const int mot2_B=3;//MOTOR 3const int mot3_A=14; //A0const int mot3_B=15; //A1//MOTOR 4const int mot4_A=16; //A2const int mot4_B=17; //A3void setup(){Serial.begin(9600);Mirf.spi = &MirfHardwareSpi;Mirf.init();Mirf.setRADDR((byte *)"ROB01"); //Set your own address (receiver address) using 5 charactersMirf.payload = sizeof(value);Mirf.channel = 90; //Set the used channelMirf.config();Serial.println("Listening..."); //Start listening to received datapinMode(mot1_A, OUTPUT);pinMode(mot1_B, OUTPUT);pinMode(mot2_A, OUTPUT);pinMode(mot2_B, OUTPUT);pinMode(mot3_A, OUTPUT);pinMode(mot3_B, OUTPUT);pinMode(mot4_A, OUTPUT);pinMode(mot4_B, OUTPUT);}void loop(){if (Mirf.dataReady()) { //When the program is received, the received data is output from the serial portMirf.getData((byte *) &value);mov=value;Serial.println(mov);switch (mov) {case 1:adelante();break;case 2:atras();break;case 3:izquierda();break;case 4:derecha();break;case 5:detenido();break;default:// if nothing else matches, do the default// default is optionalbreak;}}}void adelante() {digitalWrite(mot1_A, HIGH);digitalWrite(mot1_B, LOW);digitalWrite(mot2_A, HIGH);digitalWrite(mot2_B, LOW);digitalWrite(mot3_A, HIGH);digitalWrite(mot3_B, LOW);digitalWrite(mot4_A, HIGH);digitalWrite(mot4_B, LOW);}void atras() {digitalWrite(mot1_A, LOW);digitalWrite(mot1_B, HIGH);digitalWrite(mot2_A, LOW);digitalWrite(mot2_B, HIGH);digitalWrite(mot3_A, LOW);digitalWrite(mot3_B, HIGH);digitalWrite(mot4_A, LOW);digitalWrite(mot4_B, HIGH);}void izquierda() {//motor 1 y 3 adelantedigitalWrite(mot1_A, HIGH);digitalWrite(mot1_B, LOW);digitalWrite(mot3_A, HIGH);digitalWrite(mot3_B, LOW);//motor 2 y 4 atrasdigitalWrite(mot2_A, LOW);digitalWrite(mot2_B, HIGH);digitalWrite(mot4_A, LOW);digitalWrite(mot4_B, HIGH);}void derecha() {//motor 1 y 3 atrasdigitalWrite(mot1_A, LOW);digitalWrite(mot1_B, HIGH);digitalWrite(mot3_A, LOW);digitalWrite(mot3_B, HIGH);//motor 2 y 4 adelantedigitalWrite(mot2_A, HIGH);digitalWrite(mot2_B, LOW);digitalWrite(mot4_A, HIGH);digitalWrite(mot4_B, LOW);}void detenido() {digitalWrite(mot1_A, LOW);digitalWrite(mot1_B, LOW);digitalWrite(mot2_A, LOW);digitalWrite(mot2_B, LOW);digitalWrite(mot3_A, LOW);digitalWrite(mot3_B, LOW);digitalWrite(mot4_A, LOW);digitalWrite(mot4_B, LOW);}
Robot
ffdsa fdsa fds dfdafds