int buttonPin = D3;
int buttonState; // has the button been pushed
int lastPress = 0;
int buttonDelay = .5*1000;
int ledPin = D2;
bool ledOn = false;
int ledBrightness = 0;
int potPin = A1;
int potReading = 0;
void setup() {
pinMode( ledPin, OUTPUT );
pinMode( buttonPin, INPUT_PULLUP );
// Add a pullup resistor to the pin (prevent floating pin, where button is connected to the digital pin)
Particle.function("led", ledControl);
Particle.variable("ledOn", ledOn);
}
void loop() {
int elapsedTime = millis()-lastPress;
buttonState = digitalRead(buttonPin);
potReading = analogRead(potPin); // from 0 to 4095
ledBrightness = map(potReading, 0, 4095, 0, 255);
if ((buttonState == LOW) && (elapsedTime > buttonDelay)) {
lastPress = millis();
ledOn = !ledOn;
}
if (ledOn == true) {
analogWrite(ledPin, ledBrightness);
}
else {
digitalWrite(ledPin, LOW);
}
}
int ledControl(String command) {
// find out the state of the led
if(command == "HIGH"){
ledOn = true;
}else if(command == "LOW"){
ledOn = false;
}else{
return -1;
}
return 1;
}
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. .