Hey yall!
So a friend and I are working on a project for an instrumentation class and had to set up a very simple PIR sensor. We currently have it writing to Thingspeak for a usable graph. Our sensor is constantly sending data about every 30 seconds though and its not very useful. Help please! We’re well above our heads on this. Here is our code, it’s honestly mostly copied and pasted from things we found online because we’re so clueless when it comes to coding. We also eventually need to get the event to trigger a light on another photon but we’ll worry about that once this is working.
int inputPin = D0;
int ledPin = D1;
int pirState = LOW;
int val = 0;
int calibrateTime = 5000;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT);
}
void loop() {
// if the sensor is calibrated
if (calibrated()) {
// get the data from the sensor
readTheSensor();
// report it out, if the state has changed
reportTheData();
}
}
void readTheSensor() {
val = digitalRead(inputPin);
}
bool calibrated() {
return millis() - calibrateTime > 0;
}
void setLED(int state) {
digitalWrite(ledPin, state);
}
void reportTheData() {
if (val == HIGH) {
// the current state is no motion
// i.e. it's just changed
// announce this change by publishing an event
if (pirState == LOW) {
// we have just turned on
Particle.publish("PhotonMotion", "Motion Detected", PRIVATE);
// Update the current state
pirState = HIGH;
setLED(pirState);
}
} else {
if (pirState == HIGH) {
// we have just turned of
// Update the current state
Particle.publish("PhotonMotion", "Off", PRIVATE);
pirState = LOW;
setLED(pirState);
}
}
String data = String(10);
Particle.publish("thingSpeakWrite_", data, PRIVATE);
delay(6000);
#define publish_delay 6000
unsigned int lastPublish = 0;
unsigned long now = millis();
if ((now - lastPublish) < publish_delay) {
return;
}
int value = analogRead(D0);
Particle.publish("thingSpeakWrite_D0", "{ \"1\": \"" + String(value) + "\", \"k\": \"HS87S50T6LUAGQDS\" }", 60, PRIVATE);
lastPublish = now;
}
https://build.particle.io/build/58e1305ea6cb8f44a20013cf
@ScruffR I would really appreciate your help
we also keep getting these errors in our logs: