Go Down

Topic: Building Class Librarys (Read 480 times) previous topic - next topic

asuryan

Dear Arduino Community!

First of all I would like to say "Hi!" to all of you! :) Im new
to the arduino and microcontrollers in general but Im amazed
what even a designer with some programming skills can do with
it. :)

My question:
I would like to build my own class lib for fading leds but Im
not really sure what Im doing wrong.
This is my FadeLED.h:
Code: [Select]

#ifndef FadeLED_h
#define FadeLED_h

#include "WProgram.h"

class FadeLED
{
public:
     FadeLED(int ledPin,int fadeSpeed);
     void fade();
private:
     int pulseSignal();
     int _loga[64];
     int _pin;
     int _fadeSpeed;
     int _counter;
     int _changeMarker;
};

#endif


This is my FadeLED.cpp
Code: [Select]
#include "WProgram.h"
#include "FadeLED.h"

FadeLED::FadeLED(int pin, int fadeSpeed)
{
pinMode(pin, OUTPUT);
_pin = pin;
_fadeSpeed = fadeSpeed;
_counter = 0;
_changeMarker = 1;
_loga[64] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,16,18,20,22,25,28,30,33,36,39,42,46,49,53,56,60,64,68,72,77,81,6,90,95,100,105,110,116,121,127,132,138,144,150,156,163,169,176,182,189,196,203,210,218,225,233,240,248,255};
}

void FadeLED::fade()
{
int pwmSignal = pulseSignal();
analogWrite(_pin, pwmSignal);
delay(_fadeSpeed);
}

int FadeLED::pulseSignal()
{
  if(_counter == 63){
    _changeMarker = -1;
  }
  if(_counter == 0){
    _changeMarker = 1;
  }
  _counter = _counter+_changeMarker;
  int value = _loga[_counter];
  return value;
}



And this is my sketchfile:
Code: [Select]

#include <FadeLED.h>

FadeLED myLed(10,50);

void setup(){
 
}

void loop(){
myLed.fade();
}



Basically I understand the concept of classes etc. but I
get allways the following error:
Code: [Select]

C:\Program Files\arduino-0022\libraries\FadeLED\FadeLED.cpp: In
constructor 'FadeLED::FadeLED(int, int)':
C:\Program Files\arduino-0022\libraries\FadeLED\FadeLED.cpp:11:
error: expected primary-expression before '{' token
C:\Program Files\arduino-0022\libraries\FadeLED\FadeLED.cpp:11:
error: expected `;' before '{' token


I dont see where a ";" is missing!? What could be wrong!? :\

Thank you very much for help! :)
Regards!

PaulS

Code: [Select]
_loga[64] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,16,18,20,22,25,28,30,33,36,39,42,46,49,53,56,60,64,68,72,77,81,6,90,95,100,105,110,116,121,127,132,138,144,150,156,163,169,176,182,189,196,203,210,218,225,233,240,248,255};

A list of initializers can only be provided when the variable is declared. The variable is declared in the header file, so this is not the correct way to initialize the array. You need to have 64 lines to initialize each element.

Code: [Select]
pinMode(pin, OUTPUT);
When this call is made, do you KNOW the pin is ready to have it's mode set? In other words, is you constructor called before or after the init() function is called?

The answer is that you do not know, so you should not be initializing hardware in the constructor. Your class needs a begin() method (and a call to it) in which to perform hardware initialization.

asuryan

Thank you very much for your fast reply PaulS! :)

I think I understod your advice regarding the begin() and init of hardware in a library.
I fill only the variables inside the constructor and afterwards I call the begin() function in the
setup-function to init the hardware:

CPP:
Code: [Select]

FadeLED::begin()
{
     pinMode(_pin, OUTPUT);
}


Sketch:
Code: [Select]

#include <FadeLED.h>

FadeLED myLed(10,50);

void setup()
{
    myLed.begin();
}

void loop()
{
    myLed.fade();
}


Is this correct?


Unfortunately I dont really understand what you mean reagrding the array declaration especially
your statement:
Quote
You need to have 64 lines to initialize each element.

Could you post an example how to declare an array inside a library?
Is the array declaration the main problem of my compiler error?

Thank you! :)

PaulS

Quote
Is this correct?

Yes.

Quote
Could you post an example how to declare an array inside a library?
Is the array declaration the main problem of my compiler error?

There are two parts to defining an array. There is a declaration part and an initialization part. There is a shortcut that allows you to define and initialize an array in a single step:
Code: [Select]
int array[3] = {0, 1, 45};

If the declaration and initialization must be separate, as in a class, the declaration looks like:
Code: [Select]
int array[3];
and the initialization looks like:
Code: [Select]
array[0] = 0;
array[1] = 1;
array[2] = 45;

There are no shortcuts when initializing the array separate from the declaration, like there are when declaring and initializing in one step.

Since your array has 64 elements, while mine only has 3, you need 64 lines to initialize your array, while I needed only 3.

Go Up