Help on understanding data structure usage

Hi,

I am writing a code to transmit a packet using wifi module NRF24L01. Not very much familiar with the structures. Kindly help me out to correct the error below.

Highlighted line : radio.write( &controldata, sizeof(controldata) );

exit status 1
expected primary-expression before ‘,’ token

My code is as below,

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CNS, CE
const byte address[6] = "00001";

struct controldata {
  int Xa; 
  int Ya;
  int sw;
 };



void setup() {
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}
void loop() {

  radio.stopListening();
  int potValueX = analogRead(A0);
  int angleValueX = map(potValueX, 0, 1023, 0, 255);
  int potValueY = analogRead(A1);
  int angleValueY = map(potValueY, 0, 1023, 0, 255);
  controldata variable = {angleValueX,angleValueY,0 };
  
  radio.write( &controldata, sizeof(controldata) ); 
  delay(300);
}

Thanks.

  radio.write( &variable, sizeof(controldata) );
  radio.write( (byte*) &variable, sizeof(controldata) );

cattledog:

  radio.write( (byte*) &variable, sizeof(controldata) );

Casting the pointer to byte* is completely unnecessary. It's not even the type that RF24::write accepts, so it'll need to implicitly casted again anyway.

Thanks guys. Can you also advice how to print structure variables using Serial.println.

With the dot operator.

Thanks a lot.

I often use a print() member function of the structure,
remember that structures are mere classes with default public attributes.

Whandall:
I often use a print() member function of the structure,
remember that structures are mere classes with default public attributes.

The Arduino core provides an abstract class called Printable that you can inherit from and implement the virtual function virtual size_t printTo(Print& p) const. Then, for any class that inherits from Print (like Serial or LCD), they get the size_t print(const Printable&); function that will call your class's printTo function.