Yes by luck that code worked on UNO but not on OpenCM.
Yes here is the code
Transmitter:
/*
DIY Arduino based RC Transmitter
by Dejan Nedelkovski, www.HowToMechatronics.com
Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Wire.h>
//===============Button======================
int buttonPin1=7;
int buttonPin2=4;
int buttonVal1;
int buttonVal2;
int DXLpos=0;
int dt=10;
unsigned long currentT, previousT=0;
//====================================================
RF24 radio(8, 10); // nRF24L01 (CE, CSN) for UNO
//RF24 radio(16, 17); // nRF24L01 (CE, CSN)for OpenCM
const byte address[6] = "00001"; // Address
unsigned long lastReceiveTime = 0;
unsigned long currentTime = 0;
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
byte DXPos;
};
Data_Package data; //Create a variable with the above structure
void setup() {
Serial.begin(115200);
//=====================================================
pinMode(buttonPin1,INPUT);
pinMode(buttonPin2,INPUT);
//=========================================================
// Define the radio communication
radio.begin();
radio.openWritingPipe(address);
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_LOW);
resetData();
}
void loop() {
buttonVal1=digitalRead(buttonPin1);
buttonVal2=digitalRead(buttonPin2);
if (buttonVal1==0){
DXLpos=DXLpos+1;
}
if (buttonVal2==0){
DXLpos=DXLpos-1;
}
if (DXLpos>250){
DXLpos=250;
}
if (DXLpos<0){
DXLpos=0;
}
//====================================================================
data.DXPos=DXLpos;
radio.stopListening();
// Send the whole data from the structure to the receiver
radio.write(&data, sizeof(Data_Package));
Serial.print("B1 = ");
Serial.print(buttonVal1);
Serial.print(", ");
Serial.print("B2 = ");
Serial.print(buttonVal2);
Serial.print(", ");
Serial.print("DXL = ");
Serial.print(DXLpos);
Serial.print(", ");
Serial.println(data.DXPos);
}
void resetData() {
// Reset the values when there is no radio connection - Set initial default values
Serial.println("No data available");
}
Receiver:
/*
DIY Arduino based RC Transmitter Project
== Receiver Code ==
by Dejan Nedelkovski, www.HowToMechatronics.com
Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(16, 17); // nRF24L01 (CE, CSN) // CE, CSN //SCK->13, MOSI->11, MISO->12 FOr UNO
const byte address[6] = "00001";
unsigned long lastReceiveTime = 0;
unsigned long currentTime = 0;
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
byte DXPos;
};
Data_Package data; //Create a variable with the above structure
void setup() {
Serial.begin(115200);
radio.begin();
radio.openReadingPipe(1, address);
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_LOW);
radio.startListening(); // Set the module as receiver
resetData();
}
void loop() {
// Check whether there is data to be received
if (radio.available()) {
radio.read(&data, sizeof(Data_Package)); // Read the whole data and store it into the 'data' structure
lastReceiveTime = millis(); // At this moment we have received the data
}
// Check whether we keep receving data, or we have a connection between the two modules
currentTime = millis();
if ( currentTime - lastReceiveTime > 10000 ) { // If current time is more then (10000)1 second since we have recived the last data, that means we have lost connection
resetData(); // If connection is lost, reset the data. It prevents unwanted behavior, for example if a drone has a throttle up and we lose connection, it can keep flying unless we reset the values
}
Serial.print("DXL: ");
Serial.println(data.DXPos);
}
void resetData() {
// Reset the values when there is no radio connection - Set initial default values
Serial.println("No data available");
}