problem after making my own library

Dear Arduino heroes,

I'm new with C++ but a little familliar with C. So i've been struggling today but i've made my first library! XD

However there is a problem and i have no idea why it does not work, the problem is the following. I've made a function so when u press the button it toggles and not presses it over and over and over again. .. for example "buttonUp" or "buttonDown".

the function worked great before i used it in the library, but now when i copied it in the library and put in the "nes" class the buttonpress works but the toggle bit of the function does not work anymore???

i'm sorry for the bad english, i will post my code below. If anyone could help me out I would be very gratefull, also an explenation would be great because i can't wrap my head around it why it doesn't work. I'm still a beginner programmer.

many thanks

---- edit: i removed the code cause it wasn't relevant anymore.

Are you getting any errors or is it just not working?

I didn't check it all, but functions need a return type. This function should be a void return (I think).

class Nes
{
  public:
    void Nes(int latch, int clock, int datin);
void Nes::Nes(int latch, int clock, int datin)
{
  pinMode(latch, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(datin, INPUT);  
  
  _latch = latch;
  _clock = clock;
  _datin = datin;
}

Thanks for the quick reply, after reading my own post again after all my excitment i realised it's very confusing.

i'm not getting any errors. Only these functions do not work anymore correctly. It recognises the button press, but the part with "buttonState" does not work anymore.

unsigned char Nes::buttonUp(){ 

  buttonStateUp = (buttonInputUp());     
  if (buttonStateUp != lastButtonStateUp) {       
    if (buttonStateUp == HIGH) {
      lastButtonStateUp = buttonStateUp;
      return 1;
    }
    else{
      lastButtonStateUp = buttonStateUp; 
      return 0;
    }
  }  
}

unsigned char Nes::buttonDown(){ 

  buttonStateDown = (buttonInputDown());     
  if (buttonStateDown != lastButtonStateDown) {       
    if (buttonStateDown == HIGH) {
      lastButtonStateDown = buttonStateDown;
      return 1;
    }
    else{
      lastButtonStateDown = buttonStateDown; 
      return 0;
    }
  }  
}

unsigned char Nes::buttonLeft(){ 

  buttonStateLeft = (buttonInputLeft());     
  if (buttonStateLeft != lastButtonStateLeft) {       
    if (buttonStateLeft == HIGH) {
      lastButtonStateLeft = buttonStateLeft;
      return 1;
    }
    else{
      lastButtonStateLeft = buttonStateLeft; 
      return 0;
    }
  }  
}

unsigned char Nes::buttonRight(){ 

  buttonStateRight = (buttonInputRight());     
  if (buttonStateRight != lastButtonStateRight) {       
    if (buttonStateRight == HIGH) {
      lastButtonStateRight = buttonStateRight;
      return 1;
    }
    else{
      lastButtonStateRight = buttonStateRight; 
      return 0;
    }
  }  
}

unsigned char Nes::buttonSelect(){ 

  buttonStateSelect = (buttonInputSelect());     
  if (buttonStateSelect != lastButtonStateSelect) {       
    if (buttonStateSelect == HIGH) {
      lastButtonStateSelect = buttonStateSelect;
      return 1;
    }
    else{
      lastButtonStateSelect = buttonStateSelect; 
      return 0;
    }
  }  
}

unsigned char Nes::buttonStart(){ 

  buttonStateStart = (buttonInputStart());     
  if (buttonStateStart != lastButtonStateStart) {       
    if (buttonStateStart == HIGH) {
      lastButtonStateStart = buttonStateStart;
      return 1;
    }
    else{
      lastButtonStateStart = buttonStateStart; 
      return 0;
    }
  }  
}

unsigned char Nes::buttonB(){ 

  buttonStateB = (buttonInputB());     
  if (buttonStateB != lastButtonStateB) {       
    if (buttonStateB == HIGH) {
      lastButtonStateB = buttonStateB;
      return 1;
    }
    else{
      lastButtonStateB = buttonStateB; 
      return 0;
    }
  }  
}

unsigned char Nes::buttonA(){ 

  buttonStateA = (buttonInputA());     
  if (buttonStateA != lastButtonStateA) {       
    if (buttonStateA == HIGH) {
      lastButtonStateA = buttonStateA;
      return 1;
    }
    else{
      lastButtonStateA = buttonStateA; 
      return 0;
    }
  }  
}

SurferTim: I didn't check it all, but functions need a return type. This function should be a void return (I think).

class Nes
{
  public:
    void Nes(int latch, int clock, int datin);
void Nes::Nes(int latch, int clock, int datin)
{
  pinMode(latch, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(datin, INPUT);  
  
  _latch = latch;
  _clock = clock;
  _datin = datin;
}

No, constructors do not have a return type - they are a special case.

The problem here is that you are interracting with pins (pinMode) from within the constructor. This does not work. The constructor is run before main(), and main() does work to configure the system, including configuring the pins.

The constructor should make a note of the pins you want to use, then you do the actual pinMode() calls in a .begin() function.

Thanks for replying Majenko,

I did what u said:

Nes::Nes(int latch, int clock, int datin)
{ 
  _latch = latch;
  _clock = clock;
  _datin = datin;  
}

void Nes::beginButton(){ 
  pinMode(_latch, OUTPUT);
  pinMode(_clock, OUTPUT);
  pinMode(_datin, INPUT);       
}

and I put the nes.beginButton in the setup().

Altho i don’t think that this was the problem. It has the same behaviour, the function:

unsigned char Nes::buttonRight(){ 

  buttonStateRight = (buttonInputRight());     
  if (buttonStateRight != lastButtonStateRight) {       
    if (buttonStateRight == HIGH) {
      lastButtonStateRight = buttonStateRight;
      return 1;
    }
    else{
      lastButtonStateRight = buttonStateRight; 
      return 0;
    }
  }  
}

still does not work correctly, when used like this.

if (nes.buttonRight()) {   
    master.main++; 
    if (master.main > 3){
      master.main=0;
    }
    lcd.clear();
  }

it keeps adding master.main instead of adding 1 untill i release and press it gain. and the funny thing is that before the library the function was exactly the same but it did work like it should.

thanks tho

unsigned char Nes::buttonUp(){ 

  buttonStateUp = (buttonInputUp());     
  if (buttonStateUp != lastButtonStateUp) {       
    if (buttonStateUp == HIGH) {
      lastButtonStateUp = buttonStateUp;
      return 1;
    }
    else{
      lastButtonStateUp = buttonStateUp; 
      return 0;
    }
  }  
}

What gets returned if buttonStateUp == lastButtonStateUp?

Thanks a bunch Majenko that did the trick!

Is there anyway i can thank you?

Could you explain me why it did work when the function wasn't in the library yet and it stopped working when i put it in the library ?

ps. i saw UECIDE in ur signature, would you recomend that to a beginner? Cause i'm not happy at all with the arduino "IDE".

Tehuster:
Thanks a bunch Majenko that did the trick!

Is there anyway i can thank you?

Could you explain me why it did work when the function wasn’t in the library yet and it stopped working when i put it in the library ?

ps. i saw UECIDE in ur signature, would you recomend that to a beginner? Cause i’m not happy at all with the arduino “IDE”.

I cannot explain why it worked, no. The return value would be undefined - I guess that in a pure function that means 0, and in a class member function it may mean the last returned value. Your guess is as good as mine :wink:

I would recommend UECIDE for everyone :wink: Using it is just like using the Arduino IDE (in fact they stem from the same source) - it’s just got major improvements. You write the same sketches, use the same libraries, etc, just in a nicer environment.

Thanks again, i'm going to try it out now! :D