Compilation error: expected primary-expression before '.' token

Hello people! I've been working on this code and (since I am not super familiar with coding or arduino in general) I cannot figure out - even after reading through a bunch of similar issues - what I can do to get rid of that error. Pls, if you can, help me!! Thanks!

#include <HCSR04.h>
#include <DistanceSensor.h>

#define GREEN_led 5
#define BLUE_led 4
#define distanceSensor

const byte triggerPin = 9; 
const byte echoPin = 10;
UltraSonicDistanceSensor sensor(triggerPin, echoPin);

void setup () {
  Serial.begin(9600);
  pinMode(GREEN_led, OUTPUT);
  pinMode(BLUE_led, OUTPUT);
}

void loop () {
  float distance = distanceSensor.measureDistanceCm();
    Serial.println(distance);
    delay(500);

  if(distance_cm < 5)
  {
    digitalWrite(GREEN_led, HIGH); 
    digitalWrite(BLUE_led, LOW);
  }
  else if (distance_cm < 10)
  {
    digitalWrite(GREEN_led, LOW); 
    digitalWrite(BLUE_led, HIGH);
  }
  else 
  {
    digitalWrite(GREEN_led, HIGH); 
    digitalWrite(BLUE_led, HIGH);
  }

  delay(250); 
}

Start by posting the complete error message from the IDE, verbatim.

Welcome to the forum

You have declared an instance of the DistanceSensor library named sensor

UltraSonicDistanceSensor sensor(triggerPin, echoPin);

but later in the code you appear to refer to the object as distanceSensor

    float distance = distanceSensor.measureDistanceCm();

Change that to

    float distance = sensor.measureDistanceCm();

In addition to that, this line

#define distanceSensor

will cause problems. Delete it

That line says to replace the word distanceSensor anywhere with nothing.

So this line:

At compilation turns into:

float distance = .measureDistanceCm();

And there's nothing before the dot. This the compiler message.

As @UKHeliBob has pointed out, you named your instance just sensor so that's what you should use there.

The "if" conditions might confuse also.......(<5, <10)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.