I'm using the HC-SR04 Ultrasonic Sensor, with the 'NewPing' library.
#include <NewPing.h>
#define TRIGGER_PIN 7
#define ECHO_PIN 6
#define MAX_DISTANCE 500
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup()
{
Serial.begin(9600);
}
void loop()
{
int uS = sonar.ping();
int distance = uS / US_ROUNDTRIP_CM;
Serial.println(distance);
if(distance > 20)
{
ACTION
}
}
When i open Serial Monitor, i see that most of the readings are fair incorrect, for instance, when i get closer to the sensor it shows 196 cm, and when i go away it shows 21cm.
But when i have ONLY the sensor on the code, without the "action", it works normally.
What is going on? Can my "action" (which is a led to turn on), be affecting the readings of my sensor?