How to Combine Codes?

I'm new to Arduino and i'm just trying my own thing and working with some LED's I want to combine these codes so that when the switch is not connected the LED's fade on and off and when it is connected, they flash on and off in a line. Also, for some reason, led4 is not fading, just blinking on and off if you could help with that thanks

int led6 = 6; int led5 = 5; int led4 = 4; int led3 = 3; int brightness = 0; int fadeAmount = 5;

void setup() {

pinMode(led3, OUTPUT); pinMode(led4, OUTPUT); pinMode(led5, OUTPUT); pinMode(led6, OUTPUT); }

void loop() {

analogWrite(led6, brightness); analogWrite(led5, brightness); analogWrite(led4, brightness); analogWrite(led3, brightness);

brightness = brightness + fadeAmount;

if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ; }

delay(30); }

int switchState = 0;

void setup(){ pinMode(3,OUTPUT); pinMode(4,OUTPUT); pinMode(5,OUTPUT); pinMode(6,OUTPUT); pinMode(2,INPUT); }

void loop() { switchState = digitalRead(2);

if (switchState == LOW) {

int led = 6;

}

else { digitalWrite(3, HIGH); digitalWrite(4, LOW); digitalWrite(5, LOW); digitalWrite(6, LOW); delay(100); digitalWrite(3, LOW); digitalWrite(4, HIGH); digitalWrite(5, LOW); digitalWrite(6, LOW); delay(100); digitalWrite(3, LOW); digitalWrite(4, LOW); digitalWrite(5, HIGH); digitalWrite(6, LOW); delay(100); digitalWrite(3, LOW); digitalWrite(4, LOW); digitalWrite(5, LOW); digitalWrite(6, HIGH); delay(100); } }

Led4 isn't on a PWM pin, which is why it doesn't fade

Next time, please post your code using [ code ] & [ /code ] Combine the pre-setup code, the void setup code, the loop code.

int led6 = 6;
int led5 = 5;
int led4 = 4;
int led3 = 3;
int brightness = 0;
int fadeAmount = 5;
int switchState = 0;


void setup()  {

  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);

   pinMode(2,INPUT);
}


void loop()  {

   switchState = digitalRead(2);
   // if D2 is LOW do one thing, if  HIGH do the other thing.

   if (switchState == LOW) { 
   
     digitalWrite(3, HIGH);
     digitalWrite(4, LOW);
     digitalWrite(5, LOW);
     digitalWrite(6, LOW);
     delay(100);
     digitalWrite(3, LOW);
     digitalWrite(4, HIGH);
     digitalWrite(5, LOW);
     digitalWrite(6, LOW);
     delay(100);
     digitalWrite(3, LOW);
     digitalWrite(4, LOW);
     digitalWrite(5, HIGH);
     digitalWrite(6, LOW);
     delay(100);
     digitalWrite(3, LOW);
     digitalWrite(4, LOW);
     digitalWrite(5, LOW);
     digitalWrite(6, HIGH);
     delay(100);
   }

else{

  analogWrite(led6, brightness);  
  analogWrite(led5, brightness);    
  analogWrite(led4, brightness);
  analogWrite(led3, brightness);

  brightness = brightness + fadeAmount;

  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }    
  
  delay(30);                            
}

}

sorry, but thanks it helped a lot

How to Combine Codes?

The best way to combine code is to write your code modularly. For example, in your case, your code needs initialization and then it runs in the loop.

So something like this should have been how your wrote your code:

//function 1

//function 1's initialization
func1_init() {
}

func1() {
...
}

//function 2's initialization
func2_init() {
...
}

func2() {
}

setup() {
  func1_init(); //initialize function 1
  func2_init(); //initialize function 2
}

loop() {
  func1(); //run function 1
  func2(); //run function 2
}