Pages: [1]   Go Down
Author Topic: [SOLVED] Unexpected error in .h file of custom library  (Read 362 times)
0 Members and 1 Guest are viewing this topic.
Earth (I think)
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm new to C++ and Arduino libraries, so I have no clue what's wrong here. I'm making a library to drive a bicolour LED, based on some Processing code I wrote.
Here's the code:

BiColorLed.h
Code:
#ifndef BiColorLED
#define BiColorLED 1

#include "WProgram.h"

class BiColorLED{
public:
BiColorLED(uint8_t ledPin1, uint8_t ledPin2);
void drive();
void setColor(uint8_t toColor);
private:
uint8_t color;
uint8_t pin1, pin2;
bool yellowRed;
};
  
#endif
BiColorLed.cpp
Code:
#include "BiColorLED.h"

BiColorLED::BiColorLED(uint8_t ledPin1, uint8_t ledPin2) {
pin1=ledPin1;
pin2=ledPin2;
color=0;
yellowRed=false;
}

void setColor(uint8_t toColor) {
color=toColor;
drive(); // Change the color
}

void drive() {
// TODO blinking goes here
if (color == 3) { // Yellow
color = (yellowRed?1:2);
yellowRed = !yellowRed;
} // Beyond this point the color is always red, green, or black
// Actual LED driving
if (color == 1) { // Red
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
} else if (color == 2) { // Green
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
} else { // Black
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
}
}

And, just for completeness, my sketch:
Code:
#include <BiColorLED.h>
void setup() {}
void loop() {}

The .h and .cpp files are largely based off of looking at other libraries. Now, here's the problem: when I verify the sketch, the Arduino IDE comes back with:
Code:
In file included from BareMinimum.cpp:1:0:
/home/wolf/sketchbook/libraries/BiColorLED/BiColorLED.h:6:7: error: expected identifier before numeric constant
/home/wolf/sketchbook/libraries/BiColorLED/BiColorLED.h:6:7: error: expected unqualified-id before numeric constant

As near as I can tell, this means that there's a problem between the words `class' and `BiColorLED'. This line is copied verbatim from another library with a change of class name, so I can't figure out what's wrong. Any ideas?
« Last Edit: July 31, 2012, 12:44:52 pm by wolfgang42 » Logged


Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#define BiColorLED 1
...
class BiColorLED{

Becomes
Code:
class 1{
I believe.
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

class BiColorLED{
Code:
#define BiColorLED 1

#include "WProgram.h"

class BiColorLED{

BiColorLED is 1, as defined at the top. So, after preprocessing, the compiler sees
Code:
class 1{
which is obviously a problem. That's also why use of the preprocessor should be avoided when possible (although you're using it fine in this case)

The solution is to change the #define -- people generally use the name of the file, like this:
Code:
#ifndef BICOLORLED_H
#define BICOLORLED_H
//...
#endif
Logged

Earth (I think)
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Blech... so that's what it was. Thanks for your quick reply. I don't like the C preprocessor--it does things like that to me and I don't notice.
Logged


Pages: [1]   Go Up
Jump to: