Quick help with millis()

void activeTouch()
{
  unsigned long activeTime = millis(); //this is when the function was called

  while(true)
  {
    if(digitalRead(button1))
    {
      digitalWrite(led1, false);
      activeTime = millis(); //reset countdown if button is pressed
    }
    else
      digitalWrite(led1, true);
    
      if( millis() - activeTime > deactivateTime) //after "deactivateTime" milliseconds of no button pressing, exit
        return; //exit activeTouch() function

    Serial.println("active");
    delay(100); //add a little delay
  }

}

While keeps looping...

Post your code.

Sorted now...Please check

const byte button1 = 30;
const byte button2 = 32;

const byte led1 = 22;
const byte led2 = 23;

const byte redPin = 12;
const byte greenPin = 11;
const byte bluePin = 10;


byte  hold = false;
unsigned long switchOn = 0;
boolean isButtonHeld = false;
const unsigned int holdTime = 3000;
const unsigned int deactivateTime = 3000;



void setup()
{
  Serial.begin(9600);
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(redPin, HIGH);
  pinMode(greenPin, HIGH);
  pinMode(bluePin, HIGH);
}

void loop()
{
   if(digitalRead(button1) == LOW)
  {
    if(!isButtonHeld) //only activate once!
      switchOn = millis();
    isButtonHeld = true;
   
   Serial.print(millis());
   Serial.print(" deactivated: ");
   Serial.println(switchOn);
     
  }
  else
    isButtonHeld = false;
    
  if(isButtonHeld && millis() - switchOn > holdTime) //button has to be held AND for at least "holdTime" milliseconds
    {
      activeTouch();
    }
     
  }

void activeTouch()
{
  unsigned long activeTime = millis(); //this is when the function was called

boolean beenPressed = false;

  while(!beenPressed)
  {
    Serial.println(millis());
    if(digitalRead(button1) == LOW)
    {
      digitalWrite(led1, HIGH);
      beenPressed = true;
      activeTime = millis(); //reset countdown if button is pressed
    }
    else
      digitalWrite(led1, LOW);
      beenPressed = false;
    
      if(beenPressed == false && millis() - activeTime > deactivateTime) //after "deactivateTime" milliseconds of no button pressing, exit
        return; //exit activeTouch() function

    Serial.println("active");
    delay(100); //add a little delay
  }

}

Just have to put all in array as i have 13 inputs 8 outputs.... that is another challenge

Missing curly braces in active touch

Weird… it does exactly what i want.It keeps looping for 3 seconds, if i press in between those 3 seconds timer resets and countdown starts again.

Sorry Im so random replying, work and school kinda get in the way, sometimes Im just checking with my phone...

Here is a snippet from your code that has a problem

while(!beenPressed)
  {
    Serial.println(millis());
    if(digitalRead(button1) == LOW)
    {
      digitalWrite(led1, HIGH);
      beenPressed = true;
      activeTime = millis(); //reset countdown if button is pressed
    }
    else
      digitalWrite(led1, LOW);
      beenPressed = false;
    
      if(beenPressed == false && millis() - activeTime > deactivateTime) //after "deactivateTime" milliseconds of no button pressing, exit
        return; //exit activeTouch() function

See that "else", there is no curly braces after that, so it is always doing "beenPressed = false;" which makes the next if statement a little pointless

if(beenPressed == false && ...

it is always false, why bother checking it...

Make it this

    else
    {
      digitalWrite(led1, LOW);
      beenPressed = false;
    }

Although, Im looking and I am also noticing another issue. If you add those braces, this will happen.

  1. You press and hold the button
  2. It takes you to activeTouch()
  3. It sees you pressing the button and sets beenPressed to true, exiting the function
  4. loop still sees you pressing the button and doesnt reset the isButtonHeld flag, thus it takes you too activeTouch() again
  5. The loop repeats

Try this

const byte button1 = 30;
const byte button2 = 32;

const byte led1 = 22;
const byte led2 = 23;

const byte redPin = 12;
const byte greenPin = 11;
const byte bluePin = 10;


byte  hold = false;
unsigned long switchOn = 0;
boolean isButtonHeld = false;
const unsigned int holdTime = 3000;
const unsigned int deactivateTime = 3000;



void setup()
{
  Serial.begin(9600);
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(redPin, HIGH);
  pinMode(greenPin, HIGH);
  pinMode(bluePin, HIGH);
}

void loop()
{
   if(digitalRead(button1) == LOW)
  {
    if(!isButtonHeld) //only activate once!
      switchOn = millis();
    isButtonHeld = true;
   
   Serial.print(millis());
   Serial.print(" deactivated: ");
   Serial.println(switchOn);
     
  }
  else
    isButtonHeld = false;
    
  if(isButtonHeld && millis() - switchOn > holdTime) //button has to be held AND for at least "holdTime" milliseconds
    {
      activeTouch();
      isButtonHeld = false; //just a precaution to keep the program consistent until all circumstances
    }
     
  }

void activeTouch()
{
  unsigned long activeTime = millis(); //this is when the function was called

  while(true)
  {
    Serial.println(millis());
    if(digitalRead(button1) == LOW)
    {
      digitalWrite(led1, HIGH);
      activeTime = millis(); //reset countdown if button is pressed
    }
    else
      digitalWrite(led1, LOW);
    
      if(millis() - activeTime > deactivateTime) //after "deactivateTime" milliseconds of no button pressing, exit
        return; //exit activeTouch() function

    Serial.println("active");
    delay(100); //add a little delay
  }

}

Ps991 Your code also work. Thank you.