Back to Parent

#Code on Social Object #2
const int trigPin = D2;
const int echoPin = D3;
const int RelayPin = D4;

long duration;
int distance;

void setup() {
pinMode(RelayPin, OUTPUT); 
pinMode(trigPin, OUTPUT); 
pinMode(echoPin, INPUT); 
Serial.begin(9600); 
Particle.subscribe("Social_object2", Social_object2, MY_DEVICES);
}

void loop() {

digitalWrite(RelayPin, LOW);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
Serial.print("Distance: ");
Serial.println(distance);
if(distance <10)
{
    Particle.publish("Social_object1", PRIVATE);
    digitalWrite(RelayPin, HIGH);
    delay(10000);
    digitalWrite(RelayPin, LOW);
    delay(100);
    
}
}

void Social_object2(const char *event, const char *data) {
    digitalWrite(RelayPin, HIGH);
    delay(10000);
    digitalWrite(RelayPin, LOW);
    delay(100);
    
}
Click to Expand

Content Rating

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

0