int count_High = 0;
int count_Low = 0;
int hallPin = D3;
int ledPin = D8;
int trigPin = D6;
int echoPin = D5;
int led2 = D7;
bool state = true;
void setup()
{
Serial.begin( 9600 );
pinMode(ledPin, OUTPUT);
pinMode(hallPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led2, OUTPUT);
Particle.variable("hall sensor", state);
}
void loop()
{
delay (10);
if ( digitalRead(hallPin) == HIGH )
{
state = true;
Particle.publish("Box", "OPEN");
}
else
{
state = false;
Particle.publish("Box", "CLOSED");
}
digitalWrite (ledPin, state);
long duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
distance = (duration/2) / 29.1;
if (distance < 10)
{
digitalWrite(led2,HIGH);
Particle.publish("Hand", "YES");
}
else
{
digitalWrite(led2,LOW);
Particle.publish("Hand", "NO");
}
if (distance >= 200 || distance <= 0)
{
Serial.println("Out of range");
}
else
{
Serial.print(distance);
Serial.println(" cm");
}
delay( 500 );
}
Click to Expand