need help with making this code please! :)

hello, in this piece of code I am trying to control 8 relays. I am using a button to use two different loops of codes. when button is pressed <1 sec the first part of code runs, if >1 sec, second part of the code is run. I can't quite figure out how to set up the button in the code.... plz help me here!
also I would like to add a third part of the code, where it does nothing ( maybe if the button is pressed more than 5 seconds)

void setup() {
  
  pinMode(2, OUTPUT); 
  pinMode(3, OUTPUT); 
  pinMode(4, OUTPUT); 
  pinMode(5, OUTPUT); 
  
  pinMode(6, OUTPUT); 
  pinMode(7, OUTPUT); 
  pinMode(8, OUTPUT); 
  pinMode(9, OUTPUT); 
    
}

void loop() {
while (status_button == HIGH){
 button_delay++;
 delay(100);
   Serial.println("holding");
 status_button = digitalRead(inputPin);
}
if (timer < 10)   //button has been pressed less than a second
{               
  digitalWrite(5, HIGH);   
  delay(200);              
  digitalWrite(5, LOW);    
  delay(300); 
  digitalWrite(5, HIGH);   
  delay(200);              
  digitalWrite(5, LOW);    
  delay(300);  
  digitalWrite(5, HIGH);   
  delay(200);              
  digitalWrite(5, LOW);    
  delay(3000);
      
  digitalWrite(9, HIGH);   
  delay(200);              
  digitalWrite(9, LOW);    
  delay(300);  
  digitalWrite(9, HIGH);   
  delay(200);              
  digitalWrite(9, LOW);    
  delay(300);
  digitalWrite(9, HIGH);   
  delay(200);              
  digitalWrite(9, LOW);    
  delay(3000);

  digitalWrite(4, HIGH);   
  delay(200);              
  digitalWrite(4, LOW);    
  delay(300); 
  digitalWrite(8, HIGH);   
  delay(200);              
  digitalWrite(8, LOW);    
  delay(300);  
  digitalWrite(4, HIGH);   
  delay(200);              
  digitalWrite(4, LOW);    
  delay(300); 
  digitalWrite(8, HIGH);   
  delay(200);              
  digitalWrite(8, LOW);    
  delay(300);    
        
  delay(169000);
  
}

else {

  digitalWrite(3, HIGH);   
  delay(150);              
  digitalWrite(3, LOW);    
  delay(300);

  digitalWrite(7, HIGH);   
  delay(150);              
  digitalWrite(7, LOW);    
  delay(300);
  
  digitalWrite(5, HIGH);   
  delay(100);              
  digitalWrite(5, LOW);    
  delay(300);
  digitalWrite(9, HIGH);   
  delay(100);              
  digitalWrite(9, LOW);    
  delay(300); 
  digitalWrite(5, HIGH);   
  delay(100);              
  digitalWrite(5, LOW);    
  delay(300); 
  digitalWrite(9, HIGH);   
  delay(100);              
  digitalWrite(9, LOW);    
  delay(300);  

  digitalWrite(3, HIGH);   
  delay(100);              
  digitalWrite(3, LOW);    
  delay(500);
  digitalWrite(7, HIGH);   
  delay(100);              
  digitalWrite(7, LOW);    
  delay(500);

  digitalWrite(5, HIGH);   
  delay(100);              
  digitalWrite(5, LOW);    
  delay(500); 
  digitalWrite(9, HIGH);   
  delay(100);              
  digitalWrite(9, LOW);    
  delay(500);  

  digitalWrite(2, HIGH);   
  delay(100);              
  digitalWrite(2, LOW);    
  delay(500); 
  digitalWrite(6, HIGH);   
  delay(100);              
  digitalWrite(6, LOW);    
  delay(500); 

}
}
while (status_button

I don't think you posted all your code.

  1. Is this the whole code? If it is, it's never going to work, because you have undeclared variables and unconfigured pins. Where do you set the pin mode for your input? You only have outputs in the setup.

  2. Even if you fix the variables, the code certainly needs some refactoring and some use of arrays and iterations over them: this is quicker, easier on the eyes and less error-prone. Even better, write two functions for the two behaviours you want and test them independently first.

  3. You are not going to work it out with delays. During a delay, the arduino is frozen and can't do anything, so it can't keep track of how much more time is passing. You need to use millis() for a problem like this. Your functions should be called when the button is released, otherwise, since the program can't possibly figure out wether you are going to keep the button pressed or not, it will always execute the code for the shortest press time. Take a look at this and this.

ah, there is so much to learn... and i am not smart enough :confused:

actually i don't know how to write this thing..

i just have to make a three part program that reacts according to button press lengths.

I've done much research, but the code is like a mystery puzzle, it is very hard indeed!

thank you guys for input! I will try to find more about this...

Why don't you start with something simpler, like controlling just one relay or LED with just one function and move up from there? For example, write a blinking function for one LED and have it start or stop when you press a button. You will need millis() for this. Then, write a program that will dump to the serial monitor whether a button was pressed less or more than one second. When that works too, try to make the two parts work together, etc.

dovidu:
i just have to make a three part program that reacts according to button press lengths.

Is this a school assignment?

dovidu:
hello, in this piece of code I am trying to control 8 relays. I am using a button to use two different loops of codes. when button is pressed <1 sec the first part of code runs, if >1 sec, second part of the code is run. I can't quite figure out how to set up the button in the code.... plz help me here!
also I would like to add a third part of the code, where it does nothing ( maybe if the button is pressed more than 5 seconds)

I'm sure I can help you with one of my infamous Automaton scripts but I can't figure out what you want exactly in terms of relays and buttons and what should happen when which condition occurs.