Radio Does not Name a type error

Hello! Im currently trying to program a homemade RC car with a brushless motor and a servo. This is the code causing the error of

Transmitter_Base_Code:61:3: error: 'radio' does not name a type

radio.write(&data, sizeof(Data_Package));

^~~~~

Multiple libraries were found for "nRF24L01.h"

\Documents\Arduino\libraries\RF24

Not used: \Documents\Arduino\libraries\RF24-master

exit status 1

'radio' does not name a type

And here is the full code

#include <SPI.h>
#include <RF24.h>

RF24 radio(9, 10); // nRF24L01 (CE, CSN)
const byte address[6] = "00001"; // Address
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
byte j1PotX;
byte j1PotY;
byte j2PotX;
byte j2PotY;
};
Data_Package data; //Create a variable with the above structure
void setup() {
Serial.begin(9600);
pinMode(27,OUTPUT);
// Define the radio communication
radio.begin();
radio.openWritingPipe(address);
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_LOW);

// Set initial default values
data.j1PotX = 127; // Values from 0 to 255. When Joystick is in resting position, the value is in the middle, or 127. We actually map the pot value from 0 to 1023 to 0 to 255 because that's one BYTE value
data.j1PotY = 127;
data.j2PotX = 127;
data.j2PotY = 127;
}
void loop() {
// Read all analog inputs and map them to one Byte value
data.j1PotX = map(analogRead(A1), 0, 1023, 0, 255); // Convert the analog read value from 0 to 1023 into a BYTE value from 0 to 255
data.j1PotY = map(analogRead(A0), 0, 1023, 0, 255);
data.j2PotX = map(analogRead(A2), 0, 1023, 0, 255);
data.j2PotY = map(analogRead(A3), 0, 1023, 0, 255);
}
// Send the whole data from the structure to the receiver
radio.write(&data, sizeof(Data_Package));
}

This isn't all my code and I'm no Arduino code master, i barely know python, so some help would be appreciated. Thanks!

You have a curly bracket closing the loop() function early. That puts the radio.write statement outside of a function. That is an error.

If you had bothered to read the forum guidelines you would have known to format the code with the IDE autoformat tool (ctrl-t or Tools, Auto Format) before posting code in code tags. Here is your code fixed so that it compiles, formatted and posted properly. The comment marked with *** show the source of the error.

#include <SPI.h>
#include <RF24.h>

RF24 radio(9, 10);   // nRF24L01 (CE, CSN)
const byte address[6] = "00001"; // Address
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package
{
   byte j1PotX;
   byte j1PotY;
   byte j2PotX;
   byte j2PotY;
};
Data_Package data; //Create a variable with the above structure

void setup()
{
   Serial.begin(9600);
   pinMode(27, OUTPUT);
   // Define the radio communication
   radio.begin();
   radio.openWritingPipe(address);
   radio.setAutoAck(false);
   radio.setDataRate(RF24_250KBPS);
   radio.setPALevel(RF24_PA_LOW);

   // Set initial default values
   data.j1PotX = 127; // Values from 0 to 255. When Joystick is in resting position, the value is in the middle, or 127. We actually map the pot value from 0 to 1023 to 0 to 255 because that's one BYTE value
   data.j1PotY = 127;
   data.j2PotX = 127;
   data.j2PotY = 127;
}
void loop()
{
   // Read all analog inputs and map them to one Byte value
   data.j1PotX = map(analogRead(A1), 0, 1023, 0, 255); // Convert the analog read value from 0 to 1023 into a BYTE value from 0 to 255
   data.j1PotY = map(analogRead(A0), 0, 1023, 0, 255);
   data.j2PotX = map(analogRead(A2), 0, 1023, 0, 255);
   data.j2PotY = map(analogRead(A3), 0, 1023, 0, 255);
   
   // } ************* this misplaced } closed the loop function leaving the next statement       hanging
   
   // Send the whole data from the structure to the receiver
   radio.write(&data, sizeof(Data_Package));
}

The multiple libraries part is a warning, not an error, but you can fix it by deleting the \Documents\Arduino\libraries\RF24-master library.

If you format your code in the IDE (CTRL-T) and post it here using code tags as directed in the "How to use this forum" sticky, more people will look at it.

Your problem is misapplied braces. Which becomes obvious when looking at formatted code.
Your call to radio.write is outside of the loop.

Ah okay, thank you both!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.