Arduino Code for Joystick for transmitter

Hi ,

I am creating on Arduino joystick for my RC car but getting error while verifying

here is the code

//Transmitter code

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const uint64_t pipeOut = 0xE9E8F0F0E1LL;
RF24 radio(7, 8);
struct Signal {
byte throttle;
byte pitch;
byte roll;
byte yaw;
}
Signal data;
void ResetData()
{
data.throttle = 127;
data.pitch = 127;
data.roll = 127;
data.yaw = 127;
}
void setup()
{
//Start everything up
radio.begin();
radio.openWritingPipe(pipeOut);
radio.stopListening();
ResetData();
}

int mapJoystickValues(int val, int lower, int middle, int upper, bool reverse)
{
val = constrain(val, lower, upper);
if ( val < middle )
val = map(val, lower, middle, 0, 128);
else
val = map(val, middle, upper, 128, 255);
return ( reverse ? 255 - val : val );
}
void loop()
{

data.throttle = mapJoystickValues( analogRead(A0), 12, 524, 1015, true );
data.roll = mapJoystickValues( analogRead(A1), 12, 524, 1020, true );
data.pitch = mapJoystickValues( analogRead(A2), 12, 524, 1020, true );
data.yaw = mapJoystickValues( analogRead(A3), 12, 524, 1020, true );
radio.write(&data, sizeof(Signal));
}

Here is the error

C:\Users\Hacker\AppData\Local\Temp.arduinoIDE-unsaved2023527-1388-14fva2y.0uun\sketch_jun27a\sketch_jun27a.ino:14:8: error: expected initializer before 'data'
Signal data;
^~~~
C:\Users\Hacker\AppData\Local\Temp.arduinoIDE-unsaved2023527-1388-14fva2y.0uun\sketch_jun27a\sketch_jun27a.ino: In function 'void ResetData()':
C:\Users\Hacker\AppData\Local\Temp.arduinoIDE-unsaved2023527-1388-14fva2y.0uun\sketch_jun27a\sketch_jun27a.ino:17:1: error: 'data' was not declared in this scope
data.throttle = 127;
^~~~
C:\Users\Hacker\AppData\Local\Temp.arduinoIDE-unsaved2023527-1388-14fva2y.0uun\sketch_jun27a\sketch_jun27a.ino:17:1: note: suggested alternative: 'atan'
data.throttle = 127;
^~~~
atan
C:\Users\Hacker\AppData\Local\Temp.arduinoIDE-unsaved2023527-1388-14fva2y.0uun\sketch_jun27a\sketch_jun27a.ino: In function 'void loop()':
C:\Users\Hacker\AppData\Local\Temp.arduinoIDE-unsaved2023527-1388-14fva2y.0uun\sketch_jun27a\sketch_jun27a.ino:43:1: error: 'data' was not declared in this scope
data.throttle = mapJoystickValues( analogRead(A0), 12, 524, 1015, true );
^~~~
C:\Users\Hacker\AppData\Local\Temp.arduinoIDE-unsaved2023527-1388-14fva2y.0uun\sketch_jun27a\sketch_jun27a.ino:43:1: note: suggested alternative: 'atan'
data.throttle = mapJoystickValues( analogRead(A0), 12, 524, 1015, true );
^~~~
atan

exit status 1

Compilation error: expected initializer before 'data'

Some cut&paste, lickit & stickit going on there?
Missing a library?

1 Like

You are missing a semicolon ( ; ) to close the declaration of the Signal struct. See the comment in the formatted and properly posted code. Code now compiles for me.

//Transmitter code

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

const uint64_t pipeOut = 0xE9E8F0F0E1LL;
RF24 radio(7, 8);

struct Signal
{
   byte throttle;
   byte pitch;
   byte roll;
   byte yaw;
};  // added ;  ***********************************

Signal data; 

void ResetData()
{
   data.throttle = 127;
   data.pitch = 127;
   data.roll = 127;
   data.yaw = 127;
}

void setup()
{
   //Start everything up
   radio.begin();
   radio.openWritingPipe(pipeOut);
   radio.stopListening();
   ResetData();
}

int mapJoystickValues(int val, int lower, int middle, int upper, bool reverse)
{
   val = constrain(val, lower, upper);
   if ( val < middle )
      val = map(val, lower, middle, 0, 128);
   else
      val = map(val, middle, upper, 128, 255);
   return ( reverse ? 255 - val : val );
}

void loop()
{

   data.throttle = mapJoystickValues( analogRead(A0), 12, 524, 1015, true );
   data.roll = mapJoystickValues( analogRead(A1), 12, 524, 1020, true );
   data.pitch = mapJoystickValues( analogRead(A2), 12, 524, 1020, true );
   data.yaw = mapJoystickValues( analogRead(A3), 12, 524, 1020, true );
   radio.write(&data, sizeof(Signal));
}

Please read the forum guidelines to see how to properly post code and some information on making a good post.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in a code block.

Thanks Pal . Its really compile for me . Thanks !

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