Back to Parent

//Device 1 code
int tapPin = A0;
int servoPin = D0;
int pirPin = D1;
int tap = 0;
bool isPresent = LOW;

void setup()
{
  pinMode(tapPin, INPUT);
  pinMode(pirPin, INPUT);
  pinMode(servoPin, OUTPUT);
  Particle.subscribe("motor-turnOn2", motorControl2);
  digitalWrite(servoPin,LOW);
}

void loop()
{
  tap = analogRead(tapPin);
  if (tap < 900){
    digitalWrite(servoPin, HIGH);
    delay(500);
    digitalWrite(servoPin,LOW);
    Serial.println(tap);
    Particle.publish("motor-turnOn1", "tap-detected1");
  }
}

bool checkPresence(String command)
{
  int cmd = command.toInt();
  if (cmd == 1){
    isPresent = digitalRead(pirPin);
    delay(2000);
    if (isPresent == LOW) isPresent = digitalRead(pirPin);
    if (isPresent == HIGH){  
      return true;
    }
  }
  return false;
}

void motorControl2(const char *event, const char *data)
{
    if (strcmp(data, "tap-detected2")==0){
      //Motor turn on
      digitalWrite(servoPin, HIGH);
      delay(500);
      digitalWrite(servoPin,LOW);
    }
}

//Device 2 code
int tapPin = A0;
int servoPin = D0;
int pirPin = D1;
int tap = 0;
bool isPresent = LOW;

void setup()
{
  pinMode(tapPin, INPUT);
  pinMode(pirPin, INPUT);
  pinMode(servoPin, OUTPUT);
  Particle.subscribe("motor-turnOn1", motorControl1);
  digitalWrite(servoPin, LOW);
}

void loop()
{
  tap = analogRead(tapPin);
  if (tap < 900){
    Serial.println(tap);
    Particle.publish("motor-turnOn2", "tap-detected2");
  }
}

bool checkPresence(String command)
{
  int cmd = command.toInt();
  if (cmd == 1){
    isPresent = digitalRead(pirPin);
    delay(2000);
    if (isPresent == LOW) isPresent = digitalRead(pirPin);
    if (isPresent == HIGH){  
      return true;
    }
  }
  return false;
}

void motorControl1(const char *event, const char *data)
{
    if (strcmp(data, "tap-detected1")==0){
      //Motor turn on
      digitalWrite(servoPin, HIGH);
      delay(500);
      digitalWrite(servoPin, LOW);
    }
}
Click to Expand

Content Rating

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

0