C ++ if select object

1

I'm doing a program for arduino written in C ++ to turn on and off leds when a button is pressed. I created a class Led and put the methods in it, now I wrote an if that checks when a button is pressed, however I want it when the if verify that the button was pressed it automatically passes the led referring to the button as for the method. Something like that.

if(digitalRead(led1.botão || led2.botão) == 0) { // verifica se algum botão foi pressionado

  "Ledpressionado".ligar(); // seleciona o led referente ao botão pressionado

In java this happens automatically with the use of this, but in c ++ it does not.

    
asked by anonymous 03.08.2014 / 22:40

1 answer

1

There is no way for you to do a single check of "Are there any buttons pressed?" and only on that basis find out what it was. You inevitably need two checks, one for each button. You can do this:

if (digitalRead(led1.botao) == 0)
    led1.ligar();

if (digitalRead(led2.botao) == 0)
    led2.ligar();

Or else ...:

void Led::checarBotao() {
    if (digitalRead(botao) == 0)
        ligar();
}

// ...

led1.checarBotao();
led2.checarBotao();

With either solution you can also work with a loop iterating over a list of leds instead of manually typing each one.

    
04.08.2014 / 04:49