Hi everybody,
I’m having some problems getting correct readings from a HC-SR04 ultrasonic sensor published via IFTTT to a google sheet. First we connected it all to an Arduino and it was not a problem to get correct values, but as soon as we connedted it to a Spark Core, copied the code to particle.io and published the variable, the readings came out very different and varying widely.
We did convert the float value to a string, in order to publish it.
Hope you guys can help me out
Regards Mads
Code:
//Defining pins
int trigPin = 4;
int echoPin = 2;
//DECLARING VARIABLES
int delaytime = 100; //delaytime between each dataset (milliseconds)
//Declaring variables for the users movement
float dist; //Initializing the variable to hold distance to user value.
String dist_pub;
void setup(){
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop(){
delay(delaytime);
dist = dist_read();
dist_pub = String(dist);
Particle.publish("dist_pub", dist_pub);
}
// This function calculates the distance.
float dist_read(){
//A function that reads the dist of the ultrasound sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
//wait for the sound to come back to the sensor. The disance is calculated below in centimeters
float tempDuration = pulseIn(echoPin, HIGH);
float tempdist = (tempDuration/2) / 29.1;
return tempdist;
}