cant solve this one out!

hello fellas! as always thank ya all for the support

this time i can't solve this problem

I am trying to use a button to select 3+ different programs

and

use funtions as well.

however, I can get the button select program to work, and functions as well.

independently they work, but when they are included in the same sketch, it doesn't work.

here's the skeleton of my code:

float pressLength_milliSeconds  = 0;
int optionOne_milliSeconds  = 50;
int optionTwo_milliSeconds  = 500; 
int optionThree_milliSeconds  = 2000; 
              
int buttonPin = 1;


void funtion1(){}

void funtion2()}

void funtion3(){}

void funtion4(){}

void  setup(){
    
    pinMode(buttonPin,  INPUT_PULLUP);          
   
    pinMode(A0,  OUTPUT); 
    pinMode(A1,  OUTPUT); 
    pinMode(A2,  OUTPUT); 
    pinMode(A3,  OUTPUT); 
    pinMode(A4, OUTPUT); 
    pinMode(A5,  OUTPUT); 
    pinMode(2,  OUTPUT); 
    pinMode(3,  OUTPUT); 
    pinMode(4,  OUTPUT); 
}

    void  loop()  {
    while (digitalRead(buttonPin) ==  HIGH ){  
        delay(10);   
        pressLength_milliSeconds  = pressLength_milliSeconds  + 10;      
        Serial.print("ms  = ");
        Serial.println(pressLength_milliSeconds);
    } 


 if  (pressLength_milliSeconds >=  optionThree_milliSeconds){}


 if  (pressLength_milliSeconds >=  optionTwo_milliSeconds){}


 if  (pressLength_milliSeconds >=  optionOne_milliSeconds){}
    
    }

any help is greatly appreciated! :)

The word is functions, NOT funtions.

I cannot tell what you want.
You want a program to do something. I do not know what.
Your program “doesn’t work” but you do not provide that program and you do not tell us what was wrong with it.

The code that was provided does not compile, partly because this line is missing a left curly bracket:

void funtion2()}

I cannot help with code snippets anyway.

You are going to have to do a lot better if you want free help.

Most of that code doesn't do anything anyway. How would you ever know if it works? It's just empty functions and empty if statements. Most of the loop function gets optimized away.

I have no idea what you're trying to do, but I'm going to say your 3 if statements at the end are a problem. If the presslength is checking against 3 different outcomes, it will only ever satisfy the shortest one and never get to any longer ones because the first one it triggers will start happening.

I also see no reason to use a float.

INTP: I have no idea what you're trying to do, but I'm going to say your 3 if statements at the end are a problem. If the presslength is checking against 3 different outcomes, it will only ever satisfy the shortest one and never get to any longer ones because the first one it triggers will start happening.

And once it finishes it will move on to the next if since it's not else if.

But it is of no consequence because the if statements aren't really there. They're empty and the compiler removed them when the code was compiled.

i am sorry, it's empty because i only posted the fundamental layout of my code

so, the functions should include different codes for each.

the button code, if used alone works well. the press length is what selects the different modes

if used together with function capability included, it only triggers the shortest presslength as mentioned by our friend above.

i would like to fix this part

any suggestions?

here's a more understandable code

float pressLength_milliSeconds  = 0;
int optionOne_milliSeconds  = 50;
int optionTwo_milliSeconds  = 500; 
int optionThree_milliSeconds  = 2000; 
              
int buttonPin = 1;


void funtion1(){
//yellow led 10 times
//green led 20 times
//red led 30 times
}

void funtion2(){
//yellow led 20 times
//green led 30 times
//red led 40 times
}

void funtion3(){
//yellow led 30 times
//green led 40 times
//red led 50 times
}


void  setup(){
    
    pinMode(buttonPin,  INPUT_PULLUP);          
   
    pinMode(2,  OUTPUT); //green led
    pinMode(3,  OUTPUT); //yellow led
    pinMode(4,  OUTPUT);  //red led
}

    void  loop()  {
    while (digitalRead(buttonPin) ==  HIGH ){  
        delay(10);   
        pressLength_milliSeconds  = pressLength_milliSeconds  + 10;      
        Serial.print("ms  = ");
        Serial.println(pressLength_milliSeconds);
    } 


 if  (pressLength_milliSeconds >=  optionThree_milliSeconds){
Function1();
}


 if  (pressLength_milliSeconds >=  optionTwo_milliSeconds){
Function2();
}


 if  (pressLength_milliSeconds >=  optionOne_milliSeconds){
Function3();
}   
}

Maybe you should time a press by measuring the time between the press and when it's actually released. That way the pressing happens, THEN the code can decide what to do with the duration recorded. Maybe that's what you had in mind, but that's not in the code.

Delta_G: And once it finishes it will move on to the next if since it's not else if.

But it is of no consequence because the if statements aren't really there. They're empty and the compiler removed them when the code was compiled.

it doesn't matter if it moves on because i for loop it nearly infinite times, but thanks for the info anyways :)

INTP: Maybe you should time a press by measuring the time between the press and when it's actually released. That way the pressing happens, THEN the code can decide what to do with the duration recorded. Maybe that's what you had in mind, but that's not in the code.

It actually measures how long the button is pressed at the beginning of the loop.

nothing happens until the button is pressed

well, guess what, I solved it by tweaking with the code a little, but I actually don't know what made it work :)

LOL, anyways thank ya all for the attention

Check this code, not tested.

unsigned long buttonPressedAt = 0;
const int BUTTON = 3;
bool buttonReleased = false;
byte buttonPrevState = 0;

void setup() {
 pinMode(BUTTON,INPUT);
}

void loop() {
  int state = digitalRead(BUTTON);
  
  if (state != buttonPrevState) {
    if (state == HIGH) {
      buttonReleased = false;
      buttonPressedAt = millis();
    }
    else {
      buttonReleased = true;
    }
    buttonPrevState=state;
  }

  if (buttonReleased == true) {
    function1();
    function2();
    buttonReleased=false;
    buttonPressedAt = 0;
  }
}

void function1() {
  long diff = millis()-buttonPressedAt;
  if (diff >= 1000 && diff<2000) {
    //do action for 1 sec button press
  }
}

void function2() {
  long diff = millis()-buttonPressedAt;
  if (diff >= 2000 && diff<3000) {
    //do action for 2 sec button press
  }
}

dovidu: It actually measures how long the button is pressed at the beginning of the loop.

nothing happens until the button is pressed

Not really. It is a counter that you increase in steps of 10. You have no idea how long time it takes to execute your while loop. With a delay(10) you can only be sure it is more than 10 ms. Estimation is good enough for your purpose and that is what matters, but don't call it time measurement.