How to create a function in Arduino with optional arguments

Asked

Viewed 472 times

1

I would like to create a function that takes two arguments: Statuspino(nPino,Valor) However I would like, when I send only the pin number, the function read the logical pin value and when I enter the pin number WITH A VALUE, the function save the current value on the pin. Ex:

If StatusPino(Sensor1);   // Se o Sensor1 tiver valor HIGH, retorna true 
   // Sensor Ativado
Else
   // Sensor Desativado

or

StatusPino(Sensor1,LOW);    // Seta o Sensor1 para LOW e retorna LOW

I found no similar function.

Yes, I could do 2 functions Lerstatuspino() and Gravarstatuspino(), but I want to avoid just this.

Thank you.

1 answer

0

You just need to declare the function, as it is done in C, follow an example.

    void StatusPino(int nPino = 0,int Valorint = 0); \\ Declare a funcao com um parametro padrão

    void setup() { 

        pinMode(13, OUTPUT);
    }

    void loop() {

        StatusPino(13,1);
        delay(1000); 

        StatusPino(12,1);
        delay(1000); 

        StatusPino(13);
        delay(1000); 

        StatusPino(12);
        delay(1000); 

    }

    void StatusPino(int nPino,int Valorint)
    {

        digitalWrite(nPino, Valorint); 

    }

Browser other questions tagged

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