i have some code for basically an NRF24 to rs485 translator to allow wireless access to the network for various devices.
my question is for a small section of the code regarding structs. the code is very rough but here it is >
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include "SerialTransfer.h"
RF24 radio(10, 2); // nRF24L01 (CE, CSN) on raptor protosystem using NANO CE0 is pin 10, CSN attacheed to pin2 for NRF24-PRI
const byte address[6] = "00001";
#define RS485inout 9 // RS485 Transmit or Receive status | on raptor protosystem using NANO no direct connection can be made to RE-DE TIE so jumper must be place to gpio9
#define RS485Transmit HIGH
#define RS485Receive LOW
#define NetCommandRX 0
#define NetCommandTX 1
#define THIS_NODE_ID 0x1A
#define CENTRAL_PROC_ID 0x01
unsigned long lastReceiveTime = 0;
unsigned long currentTime = 0;
byte TXRXMode = 1; //rs485 Listen/transmit mode
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
byte command;
byte remID;
byte j1PotX;
byte j1PotY;
byte j1Button;
byte j2PotX;
byte j2PotY;
byte j2Button;
byte Sel;
byte Start;
byte X;
byte Circle;
byte Square;
byte Triangle;
byte L1;
byte L2;
byte R1;
byte R2;
byte U;
byte D;
byte L;
byte R;
};
struct RS485Data_Package {
byte NetCommand; //00 Recieve (NetworkControl), 01 Transmit (NetworkControl)
byte remID; //Sender ID# (See List of Available IDs)
byte destID; //Intended Reciever (i.e ThisNode)
byte ControlCommand; //What to Do with this information
byte Byte0; //
byte Byte1; //
byte Byte2; //
byte Byte3; //
byte Byte4; //
byte Byte5; //
byte Byte6; //
byte Byte7; //
byte Byte8; //
byte Byte9; //
byte Byte10; //
byte Byte11; //
byte Byte12; //
byte Byte13; //
byte Byte14; //
byte Byte15; //
byte Byte16; //
byte Byte17; //
byte Byte18; //
byte Byte19; //
byte Byte20; //
byte Byte21; //
byte Byte22; //
byte Byte23; //
byte Byte24; //
byte Termination; // End of Transmission Marker
};
Data_Package data; //Create a variable with the above structure
RS485Data_Package DatStruct;
RS485Data_Package IncommingStruct;
SerialTransfer myTransfer;
void setup() {
Serial.begin(115200);
myTransfer.begin(Serial);
radio.begin();
radio.openReadingPipe(0, 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
RS485Data_Package DatStruct = {NetCommandRX,
CENTRAL_PROC_ID,
THIS_NODE_ID,
0,
data.command,
data.remID,
data.j1PotX,
data.j1PotY,
data.j1Button,
data.j2PotX,
data.j2PotY,
data.j2Button,
data.Sel,
data.Start,
data.X,
data.Circle,
data.Square,
data.Triangle,
data.L1,
data.L2,
data.R1,
data.R2,
data.U,
data.D,
data.L,
data.R,
0,
0,
0xFF
};
if (TXRXMode != 2) {
if (TXRXMode != 1) { // Recieve all Packets
if (myTransfer.available())
{
digitalWrite(RS485inout, RS485Receive);
myTransfer.rxObj(IncommingStruct);
delay(1);
ParseDatStruct();
}
} else { // Send Packet
delay(5);
digitalWrite(RS485inout, RS485Transmit);
myTransfer.sendDatum(DatStruct);
delay(5);
//TXRXMode = 0;
digitalWrite(RS485inout, RS485Receive);
}
}
}
// Check whether we keep receving data, or we have a connection between the two modules
currentTime = millis();
if ( currentTime - lastReceiveTime > 1000 ) { // If current time is more then 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
}
// Print the data in the Serial Monitor
//Serial.print("j1PotX: ");
//Serial.print(data.j1PotX);
//Serial.print("; j1PotY: ");
//Serial.print(data.j1PotY);
//Serial.print("; button1: ");
//Serial.print(data.X);
//Serial.print("; j2PotX: ");
//Serial.println(data.j2PotX);
}
void resetData() {
// Reset the values when there is no radio connection - Set initial default values
data.command = 0;
data.remID = 0;
data.j1PotX = 0;
data.j1PotY = 0;
data.j1Button = 0;
data.j2PotX = 0;
data.j2PotY = 0;
data.j2Button = 0;
data.Sel = 0;
data.Start = 0;
data.X = 0;
data.Circle = 0;
data.Square = 0;
data.Triangle = 0;
data.L1 = 0;
data.L2 = 0;
data.R1 = 0;
data.R2 = 0;
data.U = 0;
data.D = 0;
data.L = 0;
data.R = 0;
}
void ParseDatStruct() {
if (!IncommingStruct.destID == THIS_NODE_ID) {
return;
} else {
switch (IncommingStruct.ControlCommand) {
case 0x00: //Command 0 = Copy Data
//CopyStructData();
break;
case 0x01: //Command 1 = Change Serial Mode
ChangeSerialMode();
break;
case 0x13: //for this to work the command sent needs to be NetCommand = 1,CENTRAL_PROC_ID = 0, THIS_NODE_ID = radio translator id ,ControlCommand = 0x13
delay(5);
digitalWrite(RS485inout, RS485Transmit);
myTransfer.sendDatum(DatStruct);
delay(5);
digitalWrite(RS485inout, RS485Receive);
break;
default:
return;
break;
}
}
}
void ChangeSerialMode() {
TXRXMode = IncommingStruct.NetCommand;
}
my question pertains to the section of code in the main loop :
RS485Data_Package DatStruct = {NetCommandRX,
CENTRAL_PROC_ID,
THIS_NODE_ID,
0,
data.command,
data.remID,
data.j1PotX,
data.j1PotY,
data.j1Button,
data.j2PotX,
data.j2PotY,
data.j2Button,
data.Sel,
data.Start,
data.X,
data.Circle,
data.Square,
data.Triangle,
data.L1,
data.L2,
data.R1,
data.R2,
data.U,
data.D,
data.L,
data.R,
0,
0,
0xFF
};
will this set all the variables in the named struct or does this redeclare the entire struct as a new locally named struct? I am new to the handling of structs in arduino so some guidance would be helpful .
if i have to set all the variables within the struct using the DatStruct.Netcommand = 0x00 and set each variable in the struct then i do not gain much aside from a nifty organization of the data.
to clarify my thoughts i suppose what i am asking is how do you set all the variables in a struct at the same time. Have i done this correctly, if not can you please advise how becasue i have googled it to death and i keep coming across answers that don't indicate how to do this .
thanks in advance.