Search code examples
c++loopsif-statementarduinovoid

How to call a void function within if statement (Arduino)


I wrote a code supposed to do the following: if I press one button while in loop void btnpress(), the program is sent to another function void blink2(), and then one led goes on and after 3 secs the led should go off, and it should also return to void btnpress() again via btnpress();. The issue is that if i press the button and release, the led goes on and stay still infinitely on, program seems not to execute the following last parts digitalWrite(LED_BUILTIN, LOW); and btnpress();.

const int btnpin = 9;
int btnstate = 0;
unsigned long currentTime;
unsigned long previousTime;
const long period = 3000;

// the setup
void setup()
{
    pinMode(LED_BUILTIN, OUTPUT);
    pinMode(btnpin, INPUT);
    Serial.begin(9600);
}

// the loop
void loop()
{
    btnpress();
}

void btnpress()
{
    Serial.println("Press button");
    delay(500);
    btnstate = digitalRead(btnpin);
    if (btnstate == HIGH) {
        previousTime = millis();
        blink2();
    }
}

void blink2()
{
    if (currentTime - previousTime >= period) {
        Serial.println("Led on");
        previousTime = currentTime;
        digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
    }
    else {
        Serial.println("Led off");
        digitalWrite(LED_BUILTIN, LOW);
        btnpress();
    }
}

Solution

  • if I press one button while in loop void btnpress(), the program is sent to another function void blink2(), and then one led goes on and after 3 secs the led should go off, and it should also return to void btnpress() again via btnpress();

    According to this statement, I can suggest you to use the following code just to be sure if your functions are working according to your reqirement.

    const int btnpin = 9;
    int btnstate = 0;
    //unsigned long currentTime;
    //unsigned long previousTime;
    const long period = 3000;
    
    // the setup
    void setup()
    {
        pinMode(LED_BUILTIN, OUTPUT);
        pinMode(btnpin, INPUT);
        Serial.begin(9600);
    }
    
    // the loop
    void loop()
    {
        btnpress();
    }
    
    void btnpress()
    {
        Serial.println("Press button");
        delay(500);
        btnstate = digitalRead(btnpin);
        if (btnstate == HIGH) {
           // previousTime = millis();
            blink2();
        }
    }
    
    void blink2()
    {
            Serial.println("Led on");
            digitalWrite(LED_BUILTIN, HIGH);
            delay(3000);
    
            Serial.println("Led off");
            digitalWrite(LED_BUILTIN, LOW);
            //btnpress(); It's already inside the Void loop            
    }
    

    If it's working well then function call is okay. And there has no issue with "How to call a void function within if statement (Arduino)"

    The problem may be inside your if statement,

    if (currentTime - previousTime >= period) {
            Serial.println("Led on");
            previousTime = currentTime;
            digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
        }
    

    here, the currentTime is not Defined.