Back to Parent

int led1 = D0;
int led2 = D1;
int isOn = 0;
int pResistor = A0;
int value;
int dimmerVal = 100;
String userControlled = "false";


void slide(const char *event, const char *data) {
    int convertedData = atoi(data);
    dimmerVal = convertedData;
    analogWrite(led1,dimmerVal);   
}

void switchedCalled(const char *event, const char *data) {
    userControlled = data;
    if(userControlled == "true") {
        Serial.println("1");
    }
    if(userControlled == "false") {
        Serial.println("2");
    }
}

void setup()
{

   // Here's the pin configuration, same as last time
   pinMode(led1, OUTPUT);
   pinMode(led2, OUTPUT);
   pinMode(pResistor, INPUT);
   
   Serial.begin(9600);

   // We are also going to declare a Particle.function so that we can turn the LED on and off from the cloud.
   Particle.function("led",ledToggle);
   Particle.function("connected",connectedLed);
   Particle.variable("isOn",isOn);

   // For good measure, let's also make sure both LEDs are off when we start:
   digitalWrite(led1, LOW);
   digitalWrite(led2, LOW);
   Particle.subscribe("slider", slide);
   Particle.subscribe("switch",switchedCalled);
}

void loop()
{
  value = analogRead(pResistor);
  Serial.println(userControlled);
  if(userControlled == "false") {
  if (value > 600){
    digitalWrite(led1, LOW);  //Turn led off
  } else {
        // digitalWrite(ledPin, HIGH); //Turn led on
    analogWrite(led1, dimmerVal);
  }

    delay(500); //Small delay
  }
}

int connectedLed(String command) {
    digitalWrite(led2,HIGH);
    delay(500);
    digitalWrite(led2,LOW);
    delay(500);
    digitalWrite(led2,HIGH);
    delay(500);
    digitalWrite(led2,LOW);
    Particle.variable("isOn",isOn);
}


int ledToggle(String command) {
    
    if (command=="on") {
        analogWrite(led1,dimmerVal);
        // digitalWrite(led2,HIGH);
        isOn = 1;
        Particle.variable("isOn", isOn );
        Particle.publish("test-lightOn","23456");
        return isOn;
    }
    else if (command=="off") {
        digitalWrite(led1,LOW);
        // digitalWrite(led2,LOW);
        isOn = 0;
        Particle.variable("isOn", isOn);
        Particle.publish("test-lightOn","9000000");
        return 0;
    }
    else {
        return -1;
    }
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0