Back to Parent

Code for both devices
//DEVICE TWO
//magnet monitor
int magPin = D1;
int magState1 = 0;
int magState2 = 0;
String sorryState = "0";
int response = 0;
//Servo Control
int servoPin = D0;
Servo Servo2;
int servoPos = 0;
void setup() {
  //magnet monitor
  pinMode(magPin, INPUT_PULLUP);
  //Servo Control
  Servo2.attach(servoPin);
  Servo2.write(0);
  Particle.subscribe("DIotSorryControl1", servo2Control);
  Serial.begin(9600);
}
void servo2Control(const char *event,const char *data)
{
  Serial.println(data);
  sorryState = "1";
  //when device receives
   if(strcmp(data,"1on") == 0) {
      Servo2.write(180);
      Serial.println("1 is sorry");
      delay(5000);
      response = digitalRead(magPin);
      while (strcmp(sorryState,"2") == 0){
        if (response == 1){
            Particle.publish("DIotSorryControl2","itsOk");
            Serial.println("2apologized");
            sorryState = "0";
            Serial.print(" sorryState " + sorryState);
        }
        else
        {response = digitalRead(magPin);
        Serial.print(" sorryState " + sorryState);}
        }
        Serial.print(" sorryState " + sorryState);
   }
   if (strcmp(data,"itsOk") == 0)
     Servo2.write(0);
     sorryState = "0";
     Serial.print(" sorryState:" + sorryState);
}
void loop() {
  //magnet monitor
  magState1 = digitalRead(magPin);
  Serial.print(" m1: ");
  Serial.print(magState1);
  delay(1000);
  magState2 = digitalRead(magPin);
  Serial.print(" m2: ");
  Serial.println(magState2);
  if(magState2 == 1){
    if (magState1 == 0){
      if (sorryState == "0"){
        Particle.publish("DIotSorryControl2","2on");
        Serial.println("2isSorry");
        sorryState == "2";
        Serial.print(" sorryState:" + sorryState);
      }
    }
  }
}
Click to Expand

Content Rating

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

0