Pages: [1]   Go Down
Author Topic: error when compiling  (Read 491 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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?
« Last Edit: December 29, 2009, 07:45:38 pm by monkphin » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 654
Posts: 50934
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem is with the #define statements.

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

So, this:

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

becomes

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

It should be obvious that that statement will not compile.

Change the #define statements to look like

Code:
#define ON 1
#define OFF 0
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: