What's wrong with my library...?

Okay, I’m trying to make a library for all of my led stuff, like fading for example.

As of now, directly copy and pasting the function into my sketch makes it work perfectly.
If I run the function from the library though, only the first led works, and it keeps fading ON instead of fading on then off…

Oh and how am I supposed to set the pins as input/output? I tried doing it the way the tutorial says to and it gives me an error saying that’s not allowed.

EDIT:
Here’s my header:

#ifndef ledmods_h
#define ledmods_h

#include <WProgram.h>

class LEDS {
public:
void snake(int speed);
};

class LED1 {
public:
void on();
void off();
void fadeOn();
void fadeOff();
void pulseLeft();
void pulseRight();
void pulseBoth();
};

class LED2 {
public:
void on();
void off();
void fadeOn();
void fadeOff();
void pulseLeft();
void pulseRight();
void pulseBoth();
};

class LED3 {
public:
void on();
void off();
void fadeOn();
void fadeOff();
void pulseLeft();
void pulseRight();
void pulseBoth();
};

class LED4 {
public:
void on();
void off();
void fadeOn();
void fadeOff();
void pulseLeft();
void pulseRight();
void pulseBoth();
};

#endif

and here’s my source code

#include “ledmods.h”
#include “WProgram.h”
const int led1 = 3;
const int led2 = 5;
const int led3 = 6;
const int led4 = 9;
byte fadevar = 0;
byte pulsevarleft = 0;
byte pulsevarright = 0;
byte pulsevarboth = 0;
void LEDS::snake(int speed){
for (fadevar = 0; fadevar <= 255; fadevar +=speed){
analogWrite(led1, fadevar);
delay(10);
}
for (fadevar = 255; fadevar >=0; fadevar -=speed){
analogWrite(led1, fadevar);
delay(10);
}
for (fadevar = 0; fadevar <= 255; fadevar +=speed){
analogWrite(led2, fadevar);
delay(10);
}
for (fadevar = 255; fadevar >=0; fadevar -=speed){
analogWrite(led2, fadevar);
delay(10);
}
for (fadevar = 0; fadevar <= 255; fadevar +=speed){
analogWrite(led4, fadevar);
delay(10);
}
for (fadevar = 255; fadevar >=0; fadevar -=speed){
analogWrite(led4, fadevar);
delay(10);
}
for (fadevar = 0; fadevar <= 255; fadevar +=speed){
analogWrite(led3, fadevar);
delay(10);
}
for (fadevar = 255; fadevar >=0; fadevar -=speed){
analogWrite(led3, fadevar);
delay(10);
}
}
void LED1::on(){
digitalWrite(led1, HIGH);
}
void LED1::off(){
digitalWrite(led1, LOW);
}
void LED1::fadeOn(){
for (fadevar = 0; fadevar <= 255; fadevar +=1){
analogWrite(led1,fadevar);
}
}
void LED1::fadeOff(){
for (fadevar = 255; fadevar >=0; fadevar -=1){
analogWrite(led1,fadevar);
}
}
void LED1::pulseLeft(){
pulsevarleft = analogRead(0);
analogWrite(led1, pulsevarleft/2);
}
void LED1::pulseRight(){
pulsevarright = analogRead(1);
analogWrite(led1, pulsevarright/2);
}
void LED1::pulseBoth(){
pulsevarboth = analogRead(0) + analogRead(1);
analogWrite(led1, pulsevarboth/4);
}
void LED2::on(){
digitalWrite(led2, HIGH);
}
void LED2::off(){
digitalWrite(led2, LOW);
}
void LED2::fadeOn(){
for (fadevar = 0; fadevar <= 255; fadevar +=1){
analogWrite(led2,fadevar);
}
}
void LED2::fadeOff(){
for (fadevar = 255; fadevar >=0; fadevar -=1){
analogWrite(led2,fadevar);
}
}
void LED2::pulseLeft(){
pulsevarleft = analogRead(0);
analogWrite(led2, pulsevarleft/2);
}
void LED2::pulseRight(){
pulsevarright = analogRead(1);
analogWrite(led2, pulsevarright/2);
}
void LED2::pulseBoth(){
pulsevarboth = analogRead(0) + analogRead(1);
analogWrite(led2, pulsevarboth/4);
}
void LED3::on(){
digitalWrite(led3, HIGH);
}
void LED3::off(){
digitalWrite(led3, LOW);
}
void LED3::fadeOn(){
for (fadevar = 0; fadevar <= 255; fadevar +=1){
analogWrite(led3,fadevar);
}
}
void LED3::fadeOff(){
for (fadevar = 255; fadevar >=0; fadevar -=1){
analogWrite(led3,fadevar);
}
}
void LED3::pulseLeft(){
pulsevarleft = analogRead(0);
analogWrite(led3, pulsevarleft/2);
}
void LED3::pulseRight(){
pulsevarright = analogRead(1);
analogWrite(led3, pulsevarright/2);
}
void LED3::pulseBoth(){
pulsevarboth = analogRead(0) + analogRead(1);
analogWrite(led3, pulsevarboth/4);
}
void LED4::on(){
digitalWrite(led4, HIGH);
}
void LED4::off(){
digitalWrite(led4, LOW);
}
void LED4::fadeOn(){
for (fadevar = 0; fadevar <= 255; fadevar +=1){
analogWrite(led4,fadevar);
}
}
void LED4::fadeOff(){
for (fadevar = 255; fadevar >=0; fadevar -=1){
analogWrite(led4,fadevar);
}
}
void LED4::pulseLeft(){
pulsevarleft = analogRead(0);
analogWrite(led4, pulsevarleft/2);
}
void LED4::pulseRight(){
pulsevarright = analogRead(1);
analogWrite(led4, pulsevarright/2);
}
void LED4::pulseBoth(){
pulsevarboth = analogRead(0) + analogRead(1);
analogWrite(led4, pulsevarboth/4);
}

I get no errors compiling but it just doesn’t work.
Right now I’m only testing the snake() function. There’s probably problems with the other parts too…

As of now, directly copy and pasting the function into my sketch makes it work perfectly.
If I run the function from the library though, only the first led works, and it keeps fading ON instead of fading on then off…

Oh and how am I supposed to set the pins as input/output? I tried doing it the way the tutorial says to and it gives me an error saying that’s not allowed.

If you're using an "older" version of the Arduino IDE, you will need to locate and delete the dot-oh (MyLibrary.o) file whenever you make changes to your library.

I'm using 0017. I used Microsoft Visual C++ 2008 Express Edition to code my library and header. There's no .o file.

I did’t test it. Try like this.

class header

#ifndef LEDS_H
#define LEDS_H

#include "WProgram.h"


class LEDS {
public:
      LEDS(uint8_t ledPin);
      void fadeOn();
      void fadeOff();
private: 
      uint8_t pin;
      int fadevar;      
};

#endif

class body

#include "LEDS.h"

LEDS::LEDS(uint8_t ledPin) {
      this->pin=ledPin;
      pinMode(pin,OUTPUT);
}

void LEDS::fadeOn() {
     for (fadevar = 0; fadevar <= 255; fadevar +=1){
           analogWrite(pin,fadevar);
     }
}

void LEDS::fadeOff() {
     for (fadevar = 255; fadevar >=0; fadevar -=1){
           analogWrite(pin,fadevar);
     }
}

Skecth

#include <LEDS.h>


LEDS led1 = LEDS(3);
LEDS led2 = LEDS(5);
LEDS led3 = LEDS(6);
LEDS led4 = LEDS(9);


void setup() 
{
}

void loop() {
  led1.fadeOn();
  led1.fadeOff();

}

Regards,
Nadir

Not to be an ass, but that didn't have anything to do with what I was having problems with. I stated my problem at the bottom of the first post.

Okay, I'm trying to make a library for all of my led stuff, like fading for example.

Not to be an ass ;) But have you seen my LED Library?

As of now, directly copy and pasting the function into my sketch makes it work perfectly. If I run the function from the library though, only the first led works, and it keeps fading ON instead of fading on then off...

I would like to see how you test the library version.

But, I can tell you, by looking at the sources, that you depend too much on global states:

const int led1 = 3;
const int led2 = 5;
const int led3 = 6;
const int led4 = 9;
byte fadevar = 0;

These should be private data members as Nadir suggests. An additional thing to nitice from his post is the suggestion of using a constructor with parameters, rather than 'hard coding' four different instances of what is basically the same. (Namely the LED1...4 classes)

Oh and how am I supposed to set the pins as input/output? I tried doing it the way the tutorial says to and it gives me an error saying that's not allowed.

Which tutorial? What does the error say?

:)

Come on, really: ;)

I get no errors compiling but it just doesn't work. Right now I'm only testing the snake() function.

I'll get to the fading when I need to, I'm focusing on the snake function first. When I use it, the leds just stay on. That's the problem I'm having.

Oh, the library I was going by is this: http://www.arduino.cc/playground/Code/Library

I also checked out this: http://arduino.cc/en/Hacking/LibraryTutorial

but between the two, they contradicted the other a lot, so I wasn't really sure which to use. I wrote my whole library from scratch by mainly following the first one.

EDIT: Right now I'm just going to take your Library and modify it to work with the functions that I want, and just follow how yours is set up. Thanks for pointing me out to that, I'm still new to this stuff ;D

EDIT2: Another thing I was having problems with (not with the library, though) was being able to have a second led start fading when the fade variable reaches 30. Like; Led1 starts fading on, then fades off. While it's fading off, when the variable reaches 30, I want led2 to start fading on but I still need led1 to finish fading off. How would I do that? I tried using if and while statements, but it just caused the leds to spaz out.

Okay what the hell?
I got my library working and all, but whenever I try using more than one function (or, even, a function that has more than one statement) only the first function works.

WHY?

edit:
It works when the fading is set as <255 and >0 but not when its <= or >=.
How come?
The code is

for (byte value = 0; value <255; value+=speed){
analogWrite(led1, value);
delay(1);
}
delay(10);
for (byte value = 255; value >0; value -=speed){
analogWrite(led1, value);
delay(1);
}

The reason why I need it >=0 is because the leds stay on after they’re done fading now.
At the moment I have a digitalWrite between each for statement so it sets the led high or low after it fades, but when I’m not using the library, I can use this same code with the <= and >= and it works fine and I’d like it that way in the library too.

We can't see what changes you made to your library, or how you are instantiating the class, or how you are calling the object's methods, or what evidence you have that a function with more than one statement doesn't work, or any evidence that more than one function call does not work, or what kind of errors you might be seeing.

And yet you want us to tell you what is wrong. Can you provide at least some help to those of us that lack mind reading skills?

I just added this to my post:

edit:
It works when the fading is set as <255 and >0 but not when its <= or >=.
How come?
The code is Quote:
for (byte value = 0; value <255; value+=speed){
analogWrite(led1, value);
delay(1);
}
delay(10);
for (byte value = 255; value >0; value -=speed){
analogWrite(led1, value);
delay(1);
}

The reason why I need it >=0 is because the leds stay on after they’re done fading now.
At the moment I have a digitalWrite between each for statement so it sets the led high or low after it fades, but when I’m not using the library, I can use this same code with the <= and >= and it works fine and I’d like it that way in the library too.

The problem isn’t my class or library, it’s the way this function is being coded. It works when I change it slightly (getting rid of the equals signs, for example) but not how I want it which is the problem.

At the moment, the working function is:

void LEDS::chaseFade(int speed){
for (byte value = 0; value <255; value+=speed){
analogWrite(led1, value);
delay(1);
}
digitalWrite(led1,HIGH);
for (byte value = 255; value >0; value -=speed){
analogWrite(led1, value);
delay(1);
}
digitalWrite(led1,LOW);
for (byte value = 0; value <255; value +=speed){
analogWrite(led2, value);
delay(1);
digitalWrite(led2,HIGH);
}
for (byte value = 255; value >0; value -=speed){
analogWrite(led2, value);
delay(1);
}
digitalWrite(led2,LOW);
for (byte value = 0; value <255; value +=speed){
analogWrite(led4, value);
delay(1);
}
digitalWrite(led4,HIGH);
for (byte value = 255; value >0; value -=speed){
analogWrite(led4, value);
delay(1);
}
digitalWrite(led4,LOW);
for (byte value = 0; value <255; value +=speed){
analogWrite(led3, value);
delay(1);
}
digitalWrite(led3,HIGH);
for (byte value = 255; value >0; value -=speed){
analogWrite(led3, value);
delay(1);
}
digitalWrite(led3,LOW);
}

I’d like to get rid of the digitalWrite statements and use >= and <= in the for statements, but when I do that it stops working and only fades led1 on.
That’s the problem. =/

Sorry for the double post, but I suppose this isn't a problem.

It's not noticeable (the jump between pwm and HIGH) enough to bother me, so this is no big deal I guess.

Does the same code, in a sketch, work for you?

Yes, and it works with >= and <= also. Everything else in my library works (just tested everything), so it’s only that bit of code. I’m completely stumped.

EDIT:
Retested it in just a sketch, and it does not work.
However, this:

for (fadevar = 0; fadevar <= 255; fadevar +=1){
analogWrite(p1, fadevar);
delay(1);
}
for (fadevar = 255; fadevar >=0; fadevar -=1){
analogWrite(p1, fadevar);
delay(1);
}
for (fadevar = 0; fadevar <= 255; fadevar +=1){
analogWrite(p2, fadevar);
delay(1);
}
for (fadevar = 255; fadevar >=0; fadevar -=1){
analogWrite(p2, fadevar);
delay(1);
}
for (fadevar = 0; fadevar <= 255; fadevar +=1){
analogWrite(p4, fadevar);
delay(1);
}
for (fadevar = 255; fadevar >=0; fadevar -=1){
analogWrite(p4, fadevar);
delay(1);
}
for (fadevar = 0; fadevar <= 255; fadevar +=1){
analogWrite(p3, fadevar);
delay(1);
}
for (fadevar = 255; fadevar >=0; fadevar -=1){
analogWrite(p3, fadevar);
delay(1);
}

works perfectly fine. The only difference between this and the other one is the speed variable(“speed” vs “1”) and pins(“p[1-4]” vs “led[1-4]”).

The only other difference is my declaration of “byte value” in the first function. The second function has fadevar declared before void setup().

Probably completely irrelevant, but I did notice that the for loops have a byte type as the index, with an int type added in the increment step. What I'm wondering is if the byte overflows when speed is added to it, resulting in a negative number, which analogWrite wouldn't like, and which would cause the for loop to begin cycling again, preventing it from ever getting to the next for loop.

Actually, analogWrite works perfectly fine with negative numbers. Try using PWM with an LED’s cathode (use analogWrite(pin,~value) or analogWrite(pin,-value)).

I just tested my sketch again setting “value” before void setup(). Works fine, even with >=,<=.

I just tried declaring “byte value = 0;” before the for statements, (same as when I declare it before setup in a sketch) and it gives me the same problem (I used the >= and <= as well).

Since the sketch works fine when the increment is 1, I would change the type of the loop variable from byte to int.

I suspect that what is happening is that, when the for loop variable gets incremented, by 2 for instance, that it gets set to 0, 2, 4, ..., 254. The next increment operation tries to set the variable to 256, which causes an overflow, and the value ends up at 0. It goes through the loop again with values of 0, 2, 4, ... 254. The increment and overflow occurs again, and the loop repeats.

Changing the loop variable type to int would prevent this problem.

It worked. Thanks so much!

I was following the LED library example by using "byte" in mine. Thanks for the help