Legal Code does not Compile (typedef)

Hi i have a portion of code that drove me nuts because i couldnt find the reason it was not working. So i opened visual studio and the offending code compiled fine. Please advise if you have the same problem. To complile in Visual Studio the only change i have made was to delete the loop and setup functions and replace it with a main(). I can reproduce the behavior i am hoping someone had the same problem. At first i thought it was a reserved name but i tried other names for the typedef and the result was the same.

#include <stdlib.h>

//-PIN ENUMERATION
#define PINW_FAN 9
#define PINW_RELAY1 5
#define PINR_TEMP 2


//-SYSTEM CONSTRAINTS
#define SAMPLE_NUMBER 7
#define PINW_NUMBER 2

//-NORMAL VALUES
#define TEMP_THRESHOLD 25 //ºCELCIUS

//-ALL IN msecECONDS DONT FORGET!!!!
#define REFRESH_TEMP 500
#define REFRESH_UI 2000 

#define CycleCheck(REFRESH_MILLI, CUR_MILLI, FUNC)  if (msec % REFRESH_TEMP == 0)   FUNC() 


//-CRITICAL/MALFUNCTIONS VALUES 
const int WHIGH_TEMP = 40; //40 ºC
const int WLOW_TEMP = 15; //15 ºC


float tempc_samples[SAMPLE_NUMBER] = {};
char sample_index = 0;


float avg_tempc = 0;// temperature variables
char uptime[9] = {};
typedef bool boolean;
//In the switches context true means on, false means off
typedef unsigned char MASK;
unsigned char messages_bitstring = 0;
const MASK TELNET_CONNECTED = 1;
const MASK SWITCH_FAN = 2;
const MASK SWITCH_RELAY1 = 3;
const MASK TEMP_BUFFER_FULL = 4;
const MASK TEMP_WARNING = 5;
const MASK TEMP_ABOVE_TEMP_THRESHOLD = 6; ///THIS IS 25ºC
const MASK BITSTRING_CHANGED = 7;
char *messages[] = {"SHOULD NOT SEE THIS\n", "TELNET CONNECTED\n", "FAN SWITCHED\n", "RELAY1 SWITCHED\n", "SOMETHING NOT YET\n", "WARNING: TEMPERATURE CRITICAL\n", "TEMP ABOVE 25ºC\n", "EVENTS HAVE OCURRED\n" };


boolean MessageCheckValue(MASK mask, boolean value) {
  return messages_bitstring & (value << mask);
}

void setup() {
}

void loop() {}

I believe this may be an issue with the Arduino IDE's automatic prototype generation - it seems to be incorrectly adjusting for the typedef when it creates the prototype, leading to a multiple-declaration compile error.

Well the error message I get is :- error: 'MASK' was not declared in this scope

Where are you declaring this?

if (msec % REFRESH_TEMP == 0)

Often a risky one.

Move your typedefs above your preprocessor macros. Edit: oops, no that doesn't work either.

Gumpy: That’s exactly what i am getting the problem is, if you check the code, the MASK typedef is declared in the beginning of the file:
typedef unsigned char MASK; So it seems to be reproducible.

Aeturnalus: I don’t quite know the innards of the IDE so i really can’t help the community in that .

AWOL: I have corrected that because it is all wrong in fact. If you notice it should be: if (CUR_MILLI % REFRESH_MILLI == 0) Were you talking about this correction or the modulo in general.

Thanks for the reply’s

if (CUR_MILLI % REFRESH_MILLI == 0)

...and if you happen to miss a tick?

Puzzled by the typedef. Tried "#define" instead - that works.

AWOL:The answer to tick i lessen the chance of missing it because i take samples and freeze the mili to pass to this functions. Even though it can happen that while some task is being done i may miss one tick but its not critical, unless the execution of other tasks always make it to happen that way. But if i go down that way lots of more logic has to be implemented in a project that is still in the very early stages. Thanks for the nudge even so. OffTopic:Do you know of a workaround without interrupts? Or direct me to a topic about that?

The #define is a workaround that is ugly in its best considering C does that and the compiler knows it:D

There is a workaround for the typedef - maybe a header? I'm sure it has come up before - It's in here somewhere!

No it is other stuff screwing you up, some random } and stuff. This compiles:-

 #include <stdlib.h>

//-PIN ENUMERATION
#define PINW_FAN 9
#define PINW_RELAY1 5
#define PINR_TEMP 2


//-SYSTEM CONSTRAINTS
#define SAMPLE_NUMBER 7
#define PINW_NUMBER 2

//-NORMAL VALUES
#define TEMP_THRESHOLD 25 //ºCELCIUS

//-ALL IN msecECONDS DONT FORGET!!!!
#define REFRESH_TEMP 500
#define REFRESH_UI 2000 

// #define CycleCheck(REFRESH_MILLI, CUR_MILLI, FUNC)  if (msec % REFRESH_TEMP == 0)   FUNC() 


//-CRITICAL/MALFUNCTIONS VALUES 
const int WHIGH_TEMP = 40; //40 ºC
const int WLOW_TEMP = 15; //15 ºC


float tempc_samples[SAMPLE_NUMBER] = {};
char sample_index = 0;


float avg_tempc = 0;// temperature variables
char uptime[9] = {};
 // typedef boolean bool;
//In the switches context true means on, false means off
typedef unsigned char MASK;
unsigned char messages_bitstring = 0;
 const MASK TELNET_CONNECTED = 1;
 const MASK SWITCH_FAN = 2;
 const MASK SWITCH_RELAY1 = 3;
 const MASK TEMP_BUFFER_FULL = 4;
 const MASK TEMP_WARNING = 5;
 const MASK TEMP_ABOVE_TEMP_THRESHOLD = 6; ///THIS IS 25ºC
 const MASK BITSTRING_CHANGED = 7;
char *messages[] = {"SHOULD NOT SEE THIS\n", "TELNET CONNECTED\n", "FAN SWITCHED\n", "RELAY1 SWITCHED\n", "SOMETHING NOT YET\n", "WARNING: TEMPERATURE CRITICAL\n", "TEMP ABOVE 25ºC\n", "EVENTS HAVE OCURRED\n" };


// boolean MessageCheckValue(MASK mask, boolean value) {
//  return messages_bitstring & (value << mask);


void setup() {
}

void loop() {}

Indeed that compiles just fine but you just commented the function that had that typedef invoked on the parameters which is exactly what is triggering the bug so of course it doesn't show up. I had tried it with different functions that invoke it and it spews the same error. It's not a '}' because that exactly same code compiles just fine in another compiler without the function commented out. If you had found the missing } then it would have been a solved problem, but it isn't. It's just a guess which is wrong (from what i understood). I don't mean this to be offending and i am happy that you took interest.

Like I said, it appears to be a problem with the Arduino IDE’s automatic prototype generation: manually replacing the MASK with a ‘uint8_t’ or an ‘unsigned char’ works fine, as does compiling it directly with avr-gcc (provided a main function that calls setup and loop).

Although, in this case, you could easily just use an enum instead (and an enum would be a better fit, too).

Yes the enum is a better solution and hadn't changed yet because it's not important. The problem with the typedef is not critical to my program and i had actually already worked around it with the unsigned char. I just made the post and insisted on some answers so that others can benefit and not waste any unnecessary time.Is there any way to report a bug?

Thanks for the advice regarding what happens to the IDE.

Yes, I was right - if you put the typedef in a header file, it will compile without problems. Agreed, it is a bit of a pain.

Bugs should be reported i n"Suggestions for the Arduino project", I guess.