Compost Monitor Code
/*
******************************************************************************
* Copyright (c) 2015 Particle Industries, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
******************************************************************************
*/
/* HC-SR04 Ping / Range finder wiring:
* -----------------------------------
* Particle - HC-SR04
* GND - GND
* VIN - VCC
* D2 - TRIG
* D6 - ECHO
*/
//if A0 and not A1 then open. if not then closed...
#include "application.h"
int ledPin = D1;
// Our button wired to D0
int buttonPin = D4;
int buttoncount=0;
void setup() {
Serial.begin(115200);
pinMode( buttonPin , INPUT_PULLUP); // sets pin as input
// We also want to use the LED
pinMode( ledPin , OUTPUT ); // sets pin as output
}
void loop() {
// Trigger pin, Echo pin, delay (ms), visual=true|info=false
int buttonState = digitalRead( buttonPin );
if( buttonState == LOW )
{
// turn the LED On
// Monitor height of Compost
digitalWrite( ledPin, LOW);
int prevDist = ping(D2, D6, 250, false);
delay(5000);
int currDist = ping(D2, D6, 250, false);
if (currDist < prevDist) {
Particle.publish("Compost height increased");
buttoncount = buttoncount +1;
if( buttoncount == 5){
Particle.publish("Please stir the Compost");
buttoncount = 0;
}else{ //bin open
// Counting times bin is opened/closed
digitalWrite( ledPin, HIGH);
//Particle.publish("Compost has been added!");
}
delay(5000);
}
}
}
int ping(pin_t trig_pin, pin_t echo_pin, uint32_t wait, bool info)
{
uint32_t duration, inches, cm;
static bool init = false;
if (!init) {
pinMode(trig_pin, OUTPUT);
digitalWriteFast(trig_pin, LOW);
pinMode(echo_pin, INPUT);
delay(50);
init = true;
}
/* Trigger the sensor by sending a HIGH pulse of 10 or more microseconds */
digitalWriteFast(trig_pin, HIGH);
delayMicroseconds(10);
digitalWriteFast(trig_pin, LOW);
duration = pulseIn(echo_pin, HIGH);
/* Convert the time into a distance */
// Sound travels at 1130 ft/s (73.746 us/inch)
// or 340 m/s (29 us/cm), out and back so divide by 2
// Ref: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
inches = duration / 74 / 2;
cm = duration / 29 / 2;
if (info) { /* Visual Output */
Serial.printf("%2d:", inches);
for(int x=0;x<inches;x++) Serial.print("#");
Serial.println();
} else { /* Informational Output */
Serial.printlnf("%6d in / %6d cm / %6d us", inches, cm, duration);
}
return inches;
//delay(wait); // slow down the output
}
Kami Chin and Josh Somers
(2018)
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .