# Skill Dev IV - weiliang

Created: December 18th, 2020

0

## Intention

In this project, we would like to build the connection between information, or data, and actuation. We will use the actuators we studied in this course to create an ambient alert or alarm.

0

0

## The code is shown as follows. I modified the loop to fit in the ultrasound sensor. Also, I simplified the doSolenoid function - the fan will just be turned on for 5 seconds instead of doing a loop for 0.5 second on/off five times.

0
``````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
0

0
Skil Dev IV
Wei Liang