led fade with pir and sound sensor

Hello! so i just finished my first project and im here to get some final help

The idea
the idea is to have a rgb led sensor connected to a nano, a pir motion sensor detects movement and turns it on, when no motion it turns off, clapping makes it change color

the problem
the problem i have is that i want it to fade up when motion is seeen and fade down (with current color ) when no motion, so i need some kind of alpha value but i dont know how to do it :confused:

    #define REDPIN 9
    #define GREENPIN 10
    #define BLUEPIN 11
    int sensor = 2;             //motion sensor
    int state = LOW;             // by default, no motion detected
    #define FADESPEED 10     // make this higher to slow down
     int r =100;
     int g =100;
     int b =100;
     int x;
     int a=1;
     int z=1;
     int button=6;
     int val=0;
    void setup() {
      pinMode(sensor, INPUT);
     pinMode(button,INPUT);
      Serial.begin(9600);       
      pinMode(REDPIN, OUTPUT);
      pinMode(GREENPIN, OUTPUT);
      pinMode(BLUEPIN, OUTPUT);
      
    }
     
     
    void loop() {
      int sensorValue = digitalRead(2);
      int sensorValueAUDIO = analogRead(A0);
      int digitalVlaue = digitalRead(15);
      val=digitalRead(button);

    
 if (digitalVlaue) { // if clap
      r=random(255);
      g=random(255);
      b=random(255);

  
        }
        
        

if (sensorValue) { // if motion seen

  
  
        analogWrite(BLUEPIN,b);
        analogWrite(GREENPIN,g);
        analogWrite(REDPIN,r);
        a=1;
  }
        
  
   if(!sensorValue){   // if motion not seen


   if(a==1){ // the fade down kinda (only fade from white)
  for (x = 255; x >= 0; x--) { 
        analogWrite(BLUEPIN,x);
        analogWrite(GREENPIN,x);
        analogWrite(REDPIN,x);
    delay(FADESPEED);
  
  a=0;
  z=1;
   } }
 
    
    }}

any ideas? /// Kaffex

solved :slight_smile:

  void loop() {
      int sensorValue = digitalRead(2);
      int sensorValueAUDIO = analogRead(A0);
      int digitalVlaue = digitalRead(15);


        analogWrite(BLUEPIN,b*a);
        analogWrite(GREENPIN,r*a);
        analogWrite(REDPIN,g*a);



 if (digitalVlaue) { // if clap
      r=random(25);
      g=random(25);
      b=random(25);
        }
        
        

if (sensorValue) { // if motion seen

while(a<10){
  a++;
  //Serial.println(a);
        analogWrite(BLUEPIN,b*a);
        analogWrite(GREENPIN,r*a);
        analogWrite(REDPIN,g*a);
  delay(FADESPEED);
  }

  }
        
  
   if(!sensorValue){   // if motion not seen
while(a>0){
  a--;
        analogWrite(BLUEPIN,b*a);
        analogWrite(GREENPIN,r*a);
        analogWrite(REDPIN,g*a);
  //Serial.println(a);
    delay(FADESPEED);
  }
   } 
   }