C++ if select object

Asked

Viewed 280 times

1

I am making a program for Rduino written in C++ to turn on and off leds when a button is pressed. I created an Led class and put in it the methods, now I wrote an if that checks when a button is pressed, but I want that when the if checks 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++ no.

  • I am assuming that this code is within the method since you said that in java would use this. So just use the this in C++ as well: this->ligar();

  • So Tiago, actually the code is inside the void loop() and not inside a class method, so I can’t use this, already in Java I could use it to refer to the object invoked the action.

1 answer

1

There’s no way you can do a single check of "Is there a button pressed?" and just based on that figure out which one it was. You inevitably need two checks, one for each button. You can do so:

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 of the two solutions you can also work with a loop iterating over a list of leds, instead of manually writing each one.

  • I understood Guilherme, I thought it was possible because this is possible in Java. In case there were two doubts: 1. On the line: Led::checarBotao() { What are the double dots for? I had already searched and found nothing! 2. In the case how would you make this loop traveling over the list? In case the list would be an array? And if I used a while, as would be the condition for "as long as no button in the list is clicked, continue the loop"?

  • 1

    No, this is not possible in java or any other language. Perhaps you have not expressed the law well and the way I understand it is a logical impossibility. 1. It is the definition of a class member function. Rather declare it within the class so: void checarBotao();. 2. It would be an array same. Led leds[2];, for (int i = 0; i < 2; ++i) leds[i].checarBotao();. For the loop, you can use a boolean variable and set it to false at the beginning. So the moment any of the conditions of the true der buttons, seven the variable to true. You can use it to control while.

  • Thanks Guilherme. Just one more question, in case this can be used within the class to refer to the object that invoked it?

  • @user13880 Within the class can use this so: this->ligar(), or merely use implicitly, as I did there in the example. Call only ligar() will apply on the current object.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.