Library looping error

I am writing a library and have come across a problem. In one of my voids in the .cpp file, I am recreating the LED fade effect from the fade example sketch. Here is that part of the .cpp file:

void LED::fade(int amount)
{
  _amount = amount;
  analogWrite(_pin, _bright);
  _bright = _bright + _amount;
  if (_bright <= 0 || _bright >= 255) {
    _amount = -_amount;
  }
  delay(30);
}

The problem comes in the part where I am setting the private “_amount” variable to be equal to the public “amount” variable(_amount = amount;). At the beginning of a loop this will always change the “_amount” variable back to the “amount” variable and I don’t want that to happen because the “_amount” variable changes at the end of the code(_amount = -_amount;). I have tried putting that statement(_amount = amount;) in other places like the header file but of course that didn’t work. Is there another place where I can put it?

Loop? What Loop?

JaBa: Loop? What Loop?

The loop in my actual sketch:

void loop() {
  led.fade(5)
}

You might want to post the rest of your LED class.

-jim lee

jimLee:
You might want to post the rest of your LED class.

-jim lee

OK here it is:

LED::LED(int pin)
{
  pinMode(pin, OUTPUT);
  _pin = pin;
}

void LED::blink(int mill)
{
  _mill = mill;
  digitalWrite(_pin, HIGH);
  delay(_mill);
  digitalWrite(_pin, LOW);
  delay(_mill);  
}

void LED::fade(int amount)
{
  _amount = amount;
  analogWrite(_pin, _bright);
  _bright = _bright + _amount;
  if (_bright <= 0 || _bright >= 255) {
    _amount = -_amount;
  }
  delay(30);
}

void LED::on()
{
  digitalWrite(_pin, HIGH);
}

void LED::off()
{
  digitalWrite(_pin, LOW);
}

In one of my voids in the .cpp file

You need to fill those voids with functions.

Lets see the header file so we know what's been defined..

And void is a way of saying, nothing.

void function(void); passes back nothing and you pass in nothing. int function(void); passes back an int but you pass in nothing.

For some reason many people are getting this mixed up lately.

Oh, and if they are in a class, void classname::function(void) they are called methods, not functions.

Go figure.

So, .h file?

-jim lee

No offense intended but if you call those things 'void', you might not be ready to write a library. It's called a function or a member function (also known as method). In this case member function / method as it's part of a class but if you called it function it would also be fine (my view).

I would also suggest that you read up on millis() based timing (see e.g. blink-without-delay) instead of delay if you want that library to be really re-usable.

jimLee: Lets see the header file so we know what's been defined..

And void is a way of saying, nothing.

void function(void); passes back nothing and you pass in nothing. int function(void); passes back an int but you pass in nothing.

For some reason many people are getting this mixed up lately.

Oh, and if they are in a class, void classname::function(void) they are called methods, not functions.

Go figure.

So, .h file?

-jim lee

Here it is:

#ifndef SYMPEL_h
#define SYMPEL_h

#include "Arduino.h"

class LED
{
  public:
    LED(int pin);
    void blink(int mill);
    void fade(int amount);
    void on();
    void off();
  private:
    int _pin;
    int _mill;
    int _amount;
    int _bright = 0;
};

#endif

What is supposed to cause the brightness of the LED to actually change? You set the brightness to the exact same value every time you call the function.