Back to Parent

int solPin = D4;
int trigPin = D2;    // Trigger
int echoPin = D3;    // Echo
long duration, cm, inches;

bool shouldActivate = false;

void setup()
{
    Particle.function( "activate", activateSolenoid );

    pinMode(solPin, OUTPUT);
    //Define inputs and outputs
	pinMode(trigPin, OUTPUT);
	pinMode(echoPin, INPUT);
}

void loop()
{
    int counter = 0;
    for(int i = 0; i <= 10; i++) {
        // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
	    // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
	    digitalWrite(trigPin, LOW);
	    delayMicroseconds(5);
	    digitalWrite(trigPin, HIGH);
	    delayMicroseconds(10);
	    digitalWrite(trigPin, LOW);

	    // Read the signal from the sensor: a HIGH pulse whose
	    // duration is the time (in microseconds) from the sending
	    // of the ping to the reception of its echo off of an object.
	    pinMode(echoPin, INPUT);
	    duration = pulseIn(echoPin, HIGH);

	    // Convert the time into a distance
	    cm = (duration/2) / 29.1;     // Divide by 29.1 or multiply by 0.0343
	    inches = (duration/2) / 74;   // Divide by 74 or multiply by 0.0135
	    Particle.publish("inches", String(inches)); // This will publish the distance that the Ultrasonic Sensor reads every second
	    if (inches > 10) {
	        counter++;
	    }
	    delay(2000);
    }
    
    if (counter < 2){
        shouldActivate = true;
    }
    
    if( shouldActivate ){
        doSolenoid(  );
        shouldActivate = false;

    }
    delay( 100 );

}

void doSolenoid(  ){
    digitalWrite(solPin, HIGH);
    delay( 5000 ) ;
    digitalWrite(solPin, LOW);
    delay( 100 );
}


int activateSolenoid( String command ){
    shouldActivate = true;
    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