Dimming and ON/OFF

Guys, need ur help. i want to build a dimming for the LED bulb which first need to detect whether are the bulb already on or not, if yes, then just the dimming can be work. if no, then ignore the dimming. But now my code can be work for on off and for the dimming, it just work on D1 but D0 can work. and it can detect whether it is on or not.

here is my code.

int brightness0 = 0;    // how bright the LED is
int brightness1 = 0;
int fadeAmount = 0;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup()  { 
    
  // declare pin 9 to be an output:
  for(int i = 0; i < 2; i++)
    {
       
        pinMode(i,OUTPUT);
    }

    
    //Export functions to cloud
    Spark.function("on", on);
    Spark.function("off", off);
    Spark.function("dim",dim);
} 

// the loop routine runs over and over again forever:
void loop()  { 
 
}

int dim(String args){
    
  
  if(D0==HIGH){
   
    if(fadeAmount>brightness0 && fadeAmount <255){
        brightness0 = fadeAmount;
        analogWrite(D0, brightness0);
    }else if(fadeAmount<brightness0 && brightness0>0){
        brightness0 = fadeAmount;
        analogWrite(D0, brightness0);
    }
  }
}else if(pin == 1){
      if(D1==HIGH){
    analogWrite(led1, brightness);
   if(fadeAmount>brightness1 && fadeAmount <255){
        brightness1 = fadeAmount;
        analogWrite(D1, brightness1);
    }else if(fadeAmount<brightness1 && brightness1>0){
       brightness1 = fadeAmount;
        analogWrite(D1, brightness1);
     }  
   }
  
  }
}

int on(String args)//,String fade)
{
   
    int pin = args.toInt();
   
    analogWrite(pin, HIGH);
    
   
  
  return pin;
}

int off(String args)
{
    int pin = args.toInt();
    
    analogWrite(pin, LOW);
    
    return pin;
}

thanks…

First things first:
Could you please edit your post to format the text better? To do this, click on the little pencil [edit] icon, select all the code, and then press CRTL+k (or use the </> button). This way it gets nicely formatted and is much easier to read.

On to the question:
What is it exactly that you want?
a) Switch a light on/off, and dim it to a certain level by entering that variable value?
b) Switch a light on/off and dim it to a fixed level?
c) a or b with instantaneous change, from 255 to 127 immediately for example?
d) a or b with a soft transition, from 255 to 127 over 2 seconds for example, slowly dimming?
c) something else entirely?

I think it’s best if you try to answer that first. The code you provided contains so many mistaken, that I don’t even know where to begin. I’m pretty sure it won’t run on any controller, that is, if the compiler let’s it pass. If we know what it is you want exactly, it’s easier to work toward a solution.

On this topic I have posted some code for a sunrise clock, with a web interface with sliders. You can change the brightness of an RGB strip with a slider on a webpage. Might be useful.

Hi Moors7,

First, please forgive for my broken English What exactly I want is actually as what you ask on the question a. I need to switch on /off and dim it to certain level by entering my own value. My problem is that I need to do it at multiple pin but not single pin.

And again thanks for the link you given, I will look at it. If i bring any inconvenience, please for give me.

Thank you very much.