int ledPin = D2;
int switchPin = D4;
int switchState; //store the reading from the button
int switchState_prev = LOW;
int photoresPin = A4;
int photoresRead = 0;
void setup() {
//Set the LED as Output
pinMode(ledPin, OUTPUT);
//Set the switch as input
pinMode (switchPin, INPUT_PULLUP);
Particle.variable("photoResValue", photoresRead);
}
void loop() {
switchState = digitalRead( switchPin );
if(switchState == HIGH){
// added the below code to make sure publish happens only when..
// switch state changes from previous state
if (switchState != switchState_prev){
Particle.publish( "Switch ON" );
}
switchState_prev = switchState;
//0-4095
photoresRead = analogRead(photoresPin);
//using map function to convert the range
int brightness = map(photoresRead, 0 , 4095, 0, 255 );
analogWrite( ledPin, brightness );
delay(200);
}else{
if (switchState != switchState_prev){
Particle.publish( "Switch OFF" );
}
switchState_prev = switchState;
int brightness = 0;
analogWrite( ledPin, brightness );
}
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .