Go Down

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

monkphin

Dec 30, 2009, 01:44 am Last Edit: Dec 30, 2009, 01:45 am by monkphin Reason: 1
Hi,
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 pinsLights[]={RED, AMBER, RED, AMBER, GREEN, GREEN, AMBER, AMBER, RED};
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
   digitalWrite(pinLights[sequence],stateLights[sequence]);
   
   // then if there's a delay then wait (may not need this if delay(0) works correctly)
   if(delayLights[sequence])
     delay(delayLights[sequence])};
     
   // next light in the sequence
   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?

PaulS

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, ...

becomes

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

monkphin

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
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy