Button loop (class, method)

HI everyone,

i´m learning Arduino and i have to write a program with a button class which contains a template.

I have this information:

// define port for led output
const uint8_t LedPortOut = 13;
// define pin for button input
const uint8_t ButtonPinIn = 2;
// define delay for blinking in ms
const uint32_t Delay = 2000;

/*The template TLed was already given */

template <const uint8_t PORT_NB>
class TLed {
public:
    TLed(const uint8_t f_ledState = LOW)  : m_ledState(f_ledState), m_disabled(false) {
        pinMode(PORT_NB, OUTPUT); // led is always output
        digitalWrite(PORT_NB, m_ledState); // set led to default state 
    }
 
    void toggle() {
        if (m_disabled) // somehow no longer active
            return; 
        if (m_ledState == LOW) { // toggle state
            m_ledState = HIGH;
        }
        else {
            m_ledState = LOW;
        }
        digitalWrite(PORT_NB, m_ledState); // set led to current state
    }

    void off() {
        m_disabled = true; 
        m_ledState = LOW;
        digitalWrite(PORT_NB, m_ledState); // set led to current state      
    }
private:
    uint8_t m_ledState; // current state of led
    bool m_disabled; // disable flag (on if led is finally turned off)
};

template <uint8_t ButtonPinIn> /7 template function of TButton
class TButton{
//Constructor
public:
	//Construktor
	TButton() : Button
	{
		pinMode(ButtonPinIn, LOW);
		
	}

	void state(){
	// read the state of the pushbutton value:
		buttonState = digitalRead(ButtonPinIn); 
		if (buttonState == HIGH) {    
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

private:
	  uint8_t m_buttonPin; // current state of button
    bool m_disabled; // disable flag (on if led is finally turned off)

};
       

// global instances for led output
TLed<LedPortOut> Led;
// and for button pin 
TButton<ButtonPinIn> Button;

void setup() {
 }

void loop() {
    // if emergency stop, turn led off
    if (Button.state() == HIGH) {
        Led.off();
    } else { //otherwise toggle
        Led.toggle();
    }
    // wait
    delay(Delay);
 
}

Now my question is:
Is the TButton class and the function state() right? What is missing?
What exactly do I have to write there?

Thanks

dutuvey

What is this all supposed to do? why have you used templates? you have various private variables like m_buttonPin, but then never use it? I suggest you start from scratch and draw out a little diagram on paper for your classes to work out what you actually need.

There are a few things,

	TButton() : Button
	{
		pinMode(ButtonPinIn, LOW);
		
	}

‘Button’ is not part of the class, there is also a ‘Button’ further down which is an instantiation of TButton:

TButton<ButtonPinIn> Button;

maybe you intended to initialise ‘m_buttonPin’:

	TButton() : m_buttonPin( ??? )
	{
		pinMode(ButtonPinIn, LOW);
	}

The function state() uses a variable ‘buttonState’ which has not been declared. Is there some confusion here? You have labelled ‘m_buttonPin’ as “current state of button” but you do not use it to store the state.

You should keep your design far more simpler until you are comfortable with classes, templates will only add to the confusion. The way you are using the template parameter will serve little benefit over a pin number passed to the constructor.

I know that there is a more easier way to write this code but that was the task and the templates really confusing me.

I´m going to refactor the code now.

I forgot to write here the definition of buttonState, sorry. But if i declared buttonstate then the state() function is ok?

duruvey:
I know that there is a more easier way to write this code but that was the task and the templates really confusing me.

I´m going to refactor the code now.

I forgot to write here the definition of buttonState, sorry. But if i declared buttonstate then the state() function is ok?

This is not an appropriate use of templates in c++, don’t use a template unless you need to.
I think you need to rethink what you are trying to do here completely. The whole point of classes is that they are reusable, all of the variables used in your class are unused and the functions all refer to global variables.