Skip to content

Instantly share code, notes, and snippets.

@RobertSasak
Created March 11, 2017 23:39
Show Gist options
  • Save RobertSasak/bc76c514bd5b29eb787e9016f6737f98 to your computer and use it in GitHub Desktop.
Save RobertSasak/bc76c514bd5b29eb787e9016f6737f98 to your computer and use it in GitHub Desktop.
int windPin = D0;
int powerPin = D1;
int numOfAverageValues = 5;
void setup() {
setupWind();
}
void setupWind() {
pinMode(powerPin, OUTPUT);
digitalWrite(powerPin, HIGH);
pinMode(windPin, INPUT_PULLDOWN);
}
void loop() {
bool cellReady = Cellular.ready();
bool cloudReady = Particle.connected();
if (cellReady) {
loopWind();
}
delay(1000);
}
void loopWind() {
getAverage();
delay(10000);
System.sleep(SLEEP_MODE_DEEP, 30*60);
}
void getAverage() {
int nonZero = numOfAverageValues;
int zero = numOfAverageValues;
unsigned long value;
unsigned long values[numOfAverageValues];
while (true) {
value = getWind();
if (value==0) {
zero--;
} else {
nonZero--;
values[nonZero] = value;
}
if (zero==0) {
publishWind(0);
break;
}
if (nonZero==0) {
unsigned long sum = 0;
for (int i = 0; i < numOfAverageValues; i++) {
sum += values[i];
}
publishWind(sum/numOfAverageValues);
break;
}
delay(500);
}
}
void publishWind(unsigned long speed) {
char publishString[40];
sprintf(publishString,"%u", speed);
Serial.printlnf("Publishing speed: %s", publishString);
Particle.publish("windSpeed", publishString, PRIVATE);
}
unsigned long getWind() {
unsigned long duration = pulseIn(windPin, LOW);
Serial.printlnf("%d us", duration);
if (duration < 10) {
duration = 0;
}
return duration;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment