Compile wiht FrequencyTimer2 throws errors, confusing

g'day

i just been looking at this article here for driving an 8x8 grid of an LED matrix http://www.arduino.cc/playground/Main/DirectDriveLEDMatrix

i hooked it all up correctly and downloaded the FrequencyTimer2 package and added it to my arduino installation, and copied the sketch on the page to test it out, but the sketch throws these errors:

h:/my stuff/arduino things/arduino-0022/arduino-0022/hardware/tools/avr/lib/gcc/../../avr/include/math.h:439: error: expected unqualified-id before 'double'
h:/my stuff/arduino things/arduino-0022/arduino-0022/hardware/tools/avr/lib/gcc/../../avr/include/math.h:439: error: expected `)' before 'double'
h:/my stuff/arduino things/arduino-0022/arduino-0022/hardware/tools/avr/lib/gcc/../../avr/include/math.h:439: error: expected `)' before 'double'

ive searched the forums and tried all solutions i found but none worked and it still refused to compile so, what do i have to do to get this to work??

using IDE 0022, win7 64bit and an arduino mega 2560

There is a thread about it here:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1239820770/4

But I must confess that even after following that, I can't get it to compile. I sort-of know the reason.

The error is around the line with "round" in it

In wiring.h we have this:

#define round(x)     ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))

and in math.h we have:

double round (double __x) __ATTR_CONST__;

I don't see how they can work together. That is, substituting the define we get:

double ((double __x)>=0?(long)((double __x)+0.5):(long)((double __x)-0.5)) __ATTR_CONST__;

... which doesn't really make sense.

But why does it ever work?

so, if i replace

#define round(x)     ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))

with

double ((double __x)>=0?(long)((double __x)+0.5):(long)((double __x)-0.5)) __ATTR_CONST__;

then it should compile/will compile okay??

No.

I finally got it to compile. Must have been written a while back.

Change FrequencyTimer2.h to read:

#ifndef FREQUENCYTIMER2_IS_IN
#define FREQUENCYTIMER2_IS_IN

/*
  FrequencyTimer2.h - A frequency generator and interrupt generator library
  Author: Jim Studt, jim@federated.com
  Copyright (c) 2007 David A. Mellis.  All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


#include <WProgram.h>

class FrequencyTimer2
{
  private:
    static uint8_t enabled;
  public:
    static void (*onOverflow)(); // not really public, but I can't work out the 'friend' for the SIGNAL
    
  public:
    static void setPeriod(unsigned long);
    static unsigned long getPeriod();
    static void setOnOverflow( void (*)() );
    static void enable();
    static void disable();
};

#endif

Inside FrequencyTimer2.cpp change each line that looks like this:

#if defined(__AVR_ATmega168__)

to be:

#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)

Then it should compile (on a Uno at least). For the Mega 2560 this probably isn’t enough.

Isn’t there a more recent library that works on the more modern chips?

as far as im aware of after doing some searching thats the only one i have found

if i compile with making those changes, i get all these errors back:

H:\My Stuff\arduino things\arduino-0022\arduino-0022\libraries\FrequencyTimer2\FrequencyTimer2.cpp: In static member function 'static void FrequencyTimer2::setOnOverflow(void (*)())':
H:\My Stuff\arduino things\arduino-0022\arduino-0022\libraries\FrequencyTimer2\FrequencyTimer2.cpp:51: error: 'TIMSK' was not declared in this scope
H:\My Stuff\arduino things\arduino-0022\arduino-0022\libraries\FrequencyTimer2\FrequencyTimer2.cpp:51: error: 'OCIE2' was not declared in this scope
H:\My Stuff\arduino things\arduino-0022\arduino-0022\libraries\FrequencyTimer2\FrequencyTimer2.cpp:52: error: 'TIMSK' was not declared in this scope
H:\My Stuff\arduino things\arduino-0022\arduino-0022\libraries\FrequencyTimer2\FrequencyTimer2.cpp:52: error: 'OCIE2' was not declared in this scope
H:\My Stuff\arduino things\arduino-0022\arduino-0022\libraries\FrequencyTimer2\FrequencyTimer2.cpp: In static member function 'static void FrequencyTimer2::setPeriod(long unsigned int)':
H:\My Stuff\arduino things\arduino-0022\arduino-0022\libraries\FrequencyTimer2\FrequencyTimer2.cpp:99: error: 'TCCR2' was not declared in this scope
H:\My Stuff\arduino things\arduino-0022\arduino-0022\libraries\FrequencyTimer2\FrequencyTimer2.cpp:102: error: 'OCR2' was not declared in this scope
H:\My Stuff\arduino things\arduino-0022\arduino-0022\libraries\FrequencyTimer2\FrequencyTimer2.cpp:103: error: 'COM20' was not declared in this scope
H:\My Stuff\arduino things\arduino-0022\arduino-0022\libraries\FrequencyTimer2\FrequencyTimer2.cpp: In static member function 'static long unsigned int FrequencyTimer2::getPeriod()':
H:\My Stuff\arduino things\arduino-0022\arduino-0022\libraries\FrequencyTimer2\FrequencyTimer2.cpp:113: error: 'TCCR2' was not declared in this scope
H:\My Stuff\arduino things\arduino-0022\arduino-0022\libraries\FrequencyTimer2\FrequencyTimer2.cpp:114: error: 'OCR2' was not declared in this scope
H:\My Stuff\arduino things\arduino-0022\arduino-0022\libraries\FrequencyTimer2\FrequencyTimer2.cpp: In static member function 'static void FrequencyTimer2::enable()':
H:\My Stuff\arduino things\arduino-0022\arduino-0022\libraries\FrequencyTimer2\FrequencyTimer2.cpp:150: error: 'TCCR2' was not declared in this scope
H:\My Stuff\arduino things\arduino-0022\arduino-0022\libraries\FrequencyTimer2\FrequencyTimer2.cpp:150: error: 'COM20' was not declared in this scope
H:\My Stuff\arduino things\arduino-0022\arduino-0022\libraries\FrequencyTimer2\FrequencyTimer2.cpp: In static member function 'static void FrequencyTimer2::disable()':
H:\My Stuff\arduino things\arduino-0022\arduino-0022\libraries\FrequencyTimer2\FrequencyTimer2.cpp:160: error: 'TCCR2' was not declared in this scope
H:\My Stuff\arduino things\arduino-0022\arduino-0022\libraries\FrequencyTimer2\FrequencyTimer2.cpp:160: error: 'COM20' was not declared in this scope

i think i may need to use another library??

Those changes only work for the Atmega328P. I wasn’t too sure about the registers for the Mega 2560.

I was having the same problem as described by kin37ik and the solution posted by Nick Gammon works on ATmega168. Thanks