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…