I see there are a couple of Q&A in this topic but I still can't find the clue to my issue. I must admit I am pretty new to c++, so this might be the root cause.
Ok, so I am trying to declare a new type like :
struct measurement_t
{
int sensorID;
int sensorData;
measurement_t(int ID, int Data)
{
sensorID = ID;
sensorData = Data;
}
};
and my project contains a method, that should return this type of data:
measurement_t getLightSensorData()
{
...
}
when I try to compile the code I got error : error: 'measurement_t' does not name a type
What I also find very strange is that the error line the compiler reports is the line number of the first #define statement at the very beginning of the code :
#define CE_PIN 7
If I simply change the method to void, the the project compiles (useless, though).
I also tried returning pointer by specifying measurement_t * as return type but the result is the same. Tried remove constructor from struct, same result again.
Can someone please help me to understand what the problem is here ?
The minimal version of the code that reproducing the same issue is:
struct measurement_t
{
int sensorID;
int sensorData;
measurement_t(int ID, int Data)
{
sensorID = ID;
sensorData = Data;
}
};
measurement_t getLightSensorData()
{
int sensorValue = 1;
measurement_t m(1, sensorValue);
return m;
}
For reference, this is intended to be an arduino sketch, and the full code is as below:
#include "RF24.h"
#include <SPI.h>
#include "printf.h"
#define CE_PIN 7
#define CS_PIN 8
#define LIGHTSENSOR_PIN A0
RF24 myRadio(CE_PIN, CS_PIN);
byte rxAddr[6] = { 0x0, 0x0, 0x0, 0x0, 0x0F };
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 3000; // send once per second
unsigned long loopSleepMillis = 200;
struct measurement_t
{
int sensorID;
int sensorData;
measurement_t(int ID, int Data)
{
sensorID = ID;
sensorData = Data;
}
};
// the setup routine runs once when you press reset:
void setup()
{
Serial.println(">>> Initializing...");
Serial.begin(115200);
printf_begin();
bool radioOk = myRadio.begin(); // Start up the physical nRF24L01 Radio
if (radioOk) Serial.println(" Radio initialized");
else Serial.println(" ERROR initializing radio !");
myRadio.setChannel(120); // Above most Wifi Channels
myRadio.setPALevel(RF24_PA_HIGH);
myRadio.setRetries(15, 15);
myRadio.openWritingPipe(rxAddr); // Use the first entry in array 'addresses' (Only 1 right now)
Serial.println("<<< Done initialization");
}
// the loop routine runs over and over again forever:
void loop()
{
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis)
{
transmitData(getLightSensorData());
prevMillis = millis();
}
delay(loopSleepMillis);
}
measurement_t getLightSensorData()
{
int sensorValue = analogRead(LIGHTSENSOR_PIN);
measurement_t m(1, sensorValue);
return m;
}
void transmitData(measurement_t data)
{
myRadio.stopListening();
Serial.print("Transmitting data...");
bool writeOK = myRadio.write(&data, sizeof(data));
if (writeOK)
{
Serial.println("OK");
}
else
{
Serial.println(F("no response"));
//myRadio.printDetails();
}
}
C
rather thanC++
? That might in some way account for those error messages. – G.M.