I am getting a error code and i dont know why!

My error code is 'ping_cm' was not declared in this scope
The highlighted piece of code is this "if (ping_cm > 7) {"
I have included my code below
The idea is that when item gets within a curtain range of the ultrasonic sensor it will send a signal and turn on a led and no matter what, When i try to compile it I get that error code. Please help
I am using 2 arduino wifis and i know that the sensor is wired correctly
This is the sender code.
Thank you

#include <WiFiNINA.h>
#include <NewPing.h>

#define SONAR_NUM 1      // Number of sensors.
#define MAX_DISTANCE 30 // Maximum distance (in cm) to ping.

NewPing sonar[SONAR_NUM] = {   // Sensor object array.
  NewPing(3, 2, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping. 
};


char ssid[] = "Netcomm 3240";
char pass[] = "Ninazoyope";

int status = WL_IDLE_STATUS;

char server[] = "10.0.1.16";

WiFiClient client;

void setup() {

  Serial.begin(9600);
  Serial.begin(115200);

  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(10000);
  }
  digitalWrite(LED_BUILTIN, HIGH);

  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void loop() {
    for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
    delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
    Serial.print("cm");
    Serial.print("=");
    Serial.print(sonar[i].ping_cm());
    Serial.print("cm ");
  }
  Serial.println();

    if (ping_cm > 7) {
    if (client.connect(server, 80)) {
      Serial.println("connected");
      client.println("GET /red HTTP/1.0");
      client.println();
    }
  } else 
    if (client.connect(server, 80)) {
      Serial.println("connected");
      client.println("GET /green HTTP/1.0");
      client.println();
    }
  } else {
    Serial.println("problem");
  }

// ---- TO HERE

  if (client.connected()) {
    client.stop();
  }

ping_cm is a member of the sonar structure (sonar[i].ping_cm()). It makes no sense to use ping_cm by itself.

What are you trying to do there. Find if any distance measured by the array of sonars is > 7?

its just 1 sensor that im using and a few weeks ago (with some code that i lost) i made it so that only using ping_cm it would trigger something...
I am trying to figure out if one sensor detects something that is over 7 cm

Hi,

  if (ping_cm > 7) {

Aren't you looking for

if (sonar[i].ping_cm() > 7)

Where in your code i == 1.

Tom... :grinning: :+1: :coffee:

Error "I was not delcared in this scope

Hi,
ping_cm() is not declared
BUT
sonar[i].ping_cm() is declared.

You are not using ping_cm(), you are using sonar[i].ping_cm() because you are setup for more than one sonar unit.

Tom... :grinning: :+1: :coffee: :australia:

i only lives (is in scope) in the for loop. Once the for loop completes i no longer exists.

You have only one element in the sonar array so use
if (sonar[0].ping_cm() > 7)

Why do you even declare an array if it has only one element?

i am kinda mashing together code that i found online into my project. It also now gives the error code "Expected unqulified-id before 'else'"

Post the code.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Arduino: 1.8.19 (Windows 10), Board: "Arduino Uno WiFi Rev2, None (ATMEGA4809)"

Sender_garage:57:5: error: expected unqualified-id before 'else'

} else

 ^~~~

Sender_garage:63:3: error: expected declaration before '}' token

} else {

^

exit status 1

expected unqualified-id before 'else'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Post the code.

#include <WiFiNINA.h>
#include <NewPing.h>

#define SONAR_NUM 1      // Number of sensors.
#define MAX_DISTANCE 30 // Maximum distance (in cm) to ping.

NewPing sonar[SONAR_NUM] = {   // Sensor object array.
  NewPing(3, 2, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping. 
};


char ssid[] = "Netcomm 3240";
char pass[] = "Ninazoyope";

int status = WL_IDLE_STATUS;

char server[] = "10.0.1.16";

WiFiClient client;

void setup() {

  Serial.begin(9600);
  Serial.begin(115200);

  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(10000);
  }
  digitalWrite(LED_BUILTIN, HIGH);

  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void loop() {
    for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
    delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
    Serial.print("cm");
    Serial.print("=");
    Serial.print(sonar[i].ping_cm());
    Serial.print("cm ");
  }
  Serial.println();

    if (sonar[0].ping_cm() > 7)
    if (client.connect(server, 80)) {
      Serial.println("connected");
      client.println("GET /red HTTP/1.0");
      client.println();
    }
    } else //I GET THE ERROR HERE
    if (client.connect(server, 80)) {
      Serial.println("connected");
      client.println("GET /green HTTP/1.0");
      client.println();
    }
  } else {
    Serial.println("problem");
  }

// ---- TO HERE

  if (client.connected()) {
    client.stop();
  }

Wait
Thats a entire new error
Sorry
Nothing to do with the first one
please still help thou

Use the IDE autoformat tool (ctrl-t or Tools, Auto format). You should see where you have 2 extra curly brackets.

That curly bracket is extra.

So is the first one on that line.

You can simplify a bit since y9ou are only using one ultrasonic sensor:

#include <WiFiNINA.h>
#include <NewPing.h>

#define MAX_DISTANCE 30 // Maximum distance (in cm) to ping.

NewPing sonar(3, 2, MAX_DISTANCE);

char ssid[] = "Netcomm 3240";
char pass[] = "Ninazoyope";

int status = WL_IDLE_STATUS;

char server[] = "10.0.1.16";

WiFiClient client;

void setup()
{
  Serial.begin(115200);

  while (status != WL_CONNECTED)
  {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(10000);
  }
  digitalWrite(LED_BUILTIN, HIGH);

  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void loop()
{
  delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  Serial.print("cm");
  Serial.print("=");
  Serial.print(sonar.ping_cm());
  Serial.print("cm ");
  Serial.println();

  if (client.connect(server, 80))
  {
    Serial.println("connected");
    if (sonar.ping_cm() > 7)
      client.println("GET /red HTTP/1.0");
    else
      client.println("GET /green HTTP/1.0");
    client.println();
  }
  else
  {
    Serial.println("problem: Client didn't connect");
  }

  if (client.connected())
  {
    client.stop();
  }
}