Go Down

Topic: error when compiling (Read 623 times) previous topic - next topic


Dec 30, 2009, 01:44 am Last Edit: Dec 30, 2009, 01:45 am by monkphin Reason: 1
Just started to really toy with an arduino I was bought for christmas, so I'm pretty new to coding on it.

I'm getting the following error when compiling the code below.

"error: expected primary-expression before '=' token"

Code: [Select]
#define RED     2    // sets the red light to pin 2
#define AMBER   3    // sets the amber light to pin 3
#define GREEN   4    // sets the green light to pin 4
#define ON = 1;       // declare what on and off mean
#define OFF = 0;      

// Array for the light control
char sequenceLength = 9;
char stateLights[]={ON, ON, OFF, OFF, ON, OFF, ON, OFF, OFF};
char stateLights[]={10000, 2000, 0, 0, 10000, 0, 3000, 0, 0};

void setup()
 pinMode(2, OUTPUT);
 pinMode(3, OUTPUT);
 pinMode(4, OUTPUT);

// code
void loop()
 char sequence = 0;  // start at element sero of all the arrays
 // and do for every light we've programmed
 while(sequence<sequenceLength))  // loop for number of sequences
   // first switch on or off the current light in the sequence
   // then if there's a delay then wait (may not need this if delay(0) works correctly)
   // next light in the sequence

The error seems to highlight
char stateLights[]={ON, ON, OFF, OFF, ON, OFF, ON, OFF, OFF};
as being the problem, but for the life of me I don't see what the issue is. Anyone have any ideas?


The problem is with the #define statements.

The compiler substitutes whatever follows the name, whenever the name occurs elsewhere.

So, this:

Code: [Select]
char stateLights[] = {ON, ON, ...


Code: [Select]
char stateLights[] = {=1;, =1;, ...

It should be obvious that that statement will not compile.

Change the #define statements to look like

Code: [Select]
#define ON 1
#define OFF 0


Ah, of course, thank you. Sorry I'd spent too long trying to work out what this was to see the problem, I think.

Go Up