errors compiling this button library

I made a library from Jeff Saltzman button reading code but I am getting some errors when trying to use and compile it in my sketch:

/Users/admin/Documents/Arduino/libraries/Button/Button.h:17: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'Button'

/Users/admin/Documents/Arduino/libraries/Button/Button.c:19: error: expected '=', ',', ';', 'asm' or '__attribute__' before ':' token

/Users/admin/Documents/Arduino/libraries/Button/Button.c:51: error: expected '=', ',', ';', 'asm' or '__attribute__' before ':' token

Here is the arduino sketch:

#include <Button.h>


#define mybuttonPin 19
#define ledPin1 17 // digital output pin for LED 1
#define ledPin2 16 // digital output pin for LED 2
#define ledPin3 15 // digital output pin for LED 3
#define ledPin4 14 // digital output pin for LED 4

Button myButton(mybuttonPin);

// LED variables
boolean ledVal1 = false; // state of LED 1
boolean ledVal2 = false; // state of LED 2
boolean ledVal3 = false; // state of LED 3
boolean ledVal4 = false; // state of LED 4

//=================================================

void setup()
{
  // Set LED output pins
  pinMode(ledPin1, OUTPUT);
  digitalWrite(ledPin1, ledVal1);
  pinMode(ledPin2, OUTPUT);
  digitalWrite(ledPin2, ledVal2);
  pinMode(ledPin3, OUTPUT);
  digitalWrite(ledPin3, ledVal3);
  pinMode(ledPin4, OUTPUT);
  digitalWrite(ledPin4, ledVal4);
}

void loop()
{
  // Get button event and act accordingly
  int b = myButton.checkButton();
  if (b == 1) clickEvent();
  if (b == 2) doubleClickEvent();
  if (b == 3) holdEvent();
  if (b == 4) longHoldEvent();
}

//=================================================
// Events to trigger by click and press+hold

void clickEvent() 
{
  ledVal1 = !ledVal1;
  digitalWrite(ledPin1, ledVal1);
}

void doubleClickEvent() 
{
  ledVal2 = !ledVal2;
  digitalWrite(ledPin2, ledVal2);
}

void holdEvent() 
{
  ledVal3 = !ledVal3;
  digitalWrite(ledPin3, ledVal3);
}

void longHoldEvent() 
{
  ledVal4 = !ledVal4;
  digitalWrite(ledPin4, ledVal4);
}

and the library files:

Button.h:

/*
 *  Button.h
 *  
 *
 *  Library Created by Danjel van Tijn on 30/10/09 using code from 
 *  Jeff Saltzman Oct. 13, 2009 .
 *  
 *
 */

#ifndef Button_h
#define Button_h


#include "WProgram.h"
  
class Button
{

public:
      Button(int pin);
      int checkButton();
      // Button timing variables
      int debounce; // ms debounce period to prevent flickering when pressing or releasing the button
      int DCgap; // max ms between clicks for a double click event
      int holdTime; // ms hold period: how long to wait for press+hold event
      int longHoldTime; // ms long hold period: how long to wait for press+hold event
  
private: 
      int buttonPin
      boolean buttonVal; // value read from button
      boolean buttonLast; // buffered value of the button's previous state
      boolean DCwaiting; // whether we're waiting for a double click (down)
      boolean DConUp; // whether to register a double click on next release, or whether to wait and click
      boolean singleOK; // whether it's OK to do a single click
      long downTime; // time the button was pressed down
      long upTime; // time the button was released
      boolean ignoreUp; // whether to ignore the button release because the click+hold was triggered
      boolean waitForUp; // when held, whether to wait for the up event
      boolean holdEventPast; // whether or not the hold event happened already
      boolean longHoldEventPast;// whether or not the long hold event happened already
}

#endif

Button.c:

/*
 *  Button.c
 *  
 *
 *  Library Created by Danjel van Tijn on 30/10/09 using code from 
 *  Jeff Saltzman Oct. 13, 2009 .
 *  
 *
 */


#include "Button.h"
#include "WProgram.h"


/******************************************************************************
* Constructor
******************************************************************************/
Button::Button(int pin)
{
   buttonPin = pin;
   pinMode(buttonPin, INPUT);
   digitalWrite(buttonPin, HIGH );
   
// Button timing variables
  debounce = 20; // ms debounce period to prevent flickering when pressing or releasing the button
  DCgap = 250; // max ms between clicks for a double click event
  holdTime = 2000; // ms hold period: how long to wait for press+hold event
  longHoldTime = 5000; // ms long hold period: how long to wait for press+hold event
  
  // Other button variables
  buttonVal = HIGH; // value read from button
  buttonLast = HIGH; // buffered value of the button's previous state
  DCwaiting = false; // whether we're waiting for a double click (down)
  DConUp = false; // whether to register a double click on next release, or whether to wait and click
  singleOK = true; // whether it's OK to do a single click
  downTime = -1; // time the button was pressed down
  upTime = -1; // time the button was released
  ignoreUp = false; // whether to ignore the button release because the click+hold was triggered
  waitForUp = false; // when held, whether to wait for the up event
  holdEventPast = false; // whether or not the hold event happened already
  longHoldEventPast = false;// whether or not the long hold event happened already
}


/******************************************************************************
* Public methods
******************************************************************************/


int Button::checkButton()
{
    int event = 0;
    // Read the state of the button
    buttonVal = digitalRead(buttonPin);
    // Button pressed down
    if (buttonVal == LOW && buttonLast == HIGH && (millis() - upTime) > debounce) 
    {
      downTime = millis();
      ignoreUp = false;
      waitForUp = false;
      singleOK = true;
      holdEventPast = false;
      longHoldEventPast = false;
      if ((millis()-upTime) < DCgap && DConUp == false && DCwaiting == true) DConUp = true;
      else DConUp = false;
      DCwaiting = false;
    }
    // Button released
    else if (buttonVal == HIGH && buttonLast == LOW && (millis() - downTime) > debounce) 
    {
      if (not ignoreUp) 
        {
          upTime = millis();
          if (DConUp == false) DCwaiting = true;
          else 
          {
            event = 2;
            DConUp = false;
            DCwaiting = false;
            singleOK = false;
          }
        }
      }
    // Test for normal click event: DCgap expired
    if ( buttonVal == HIGH && (millis()-upTime) >= DCgap && DCwaiting == true && DConUp == false && singleOK == true) 
    {
      event = 1;
      DCwaiting = false;
    }
    // Test for hold
    if (buttonVal == LOW && (millis() - downTime) >= holdTime) 
    {
      // Trigger "normal" hold
      if (not holdEventPast) 
      {
        event = 3;
        waitForUp = true;
        ignoreUp = true;
        DConUp = false;
        DCwaiting = false;
        //downTime = millis();
        holdEventPast = true;
       }
      // Trigger "long" hold
      if ((millis() - downTime) >= longHoldTime) 
      {
        if (not longHoldEventPast) 
        {
          event = 4;
          longHoldEventPast = true;
        }
      }
    }
    buttonLast = buttonVal;
    return event;
}

Just a wild guess: It breaks on the class definitions. Can it be that Button.c should be renamed to Button.cpp? Will that make the compiler recognize it as C+?

John

that was it!
I was also missing “;” at the end of my class declaration (not used to that)

and I had to change some of the timer variables to type long.

btw, do I need to manually make that Keywords.txt file that I see with a lot of libraries?

Yup, you're going to need to make one yourself.

Down near the bottom, it explains how to create the keywords file: http://arduino.cc/en/Hacking/LibraryTutorial

Basically just need to make sure it's a tab and not spaces between the word and the KEYWORD define.

Did you manage to get it all done as a library? Is it available?