'Ui' was not declared in this scope

I added a line what I like to have is to sign the distance to U i, i = 0, 1, 3, how should I do it please?

#include <NewPing.h>

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

NewPing sonar[SONAR_NUM] = {   // Sensor object array.
  NewPing(10, 11, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(12, 13, MAX_DISTANCE),
  NewPing(8, 9, MAX_DISTANCE)

uint8_t currentSensor = 0;
uint8_t U = 0;

unsigned int cm[SONAR_NUM];

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.

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("U");
    Serial.print("cm ");

    Ui = sonar[i].ping_cm();



Arduino: 1.8.3 (Windows 7), Board: "Arduino/Genuino Uno"

C:\Users\HUA.DELLV-PC\Documents\Arduino\newping.1\newping.1.ino: In function 'void loop()':

newping.1:35: error: 'Ui' was not declared in this scope

     Ui = sonar[i].ping_cm();


exit status 1
'Ui' was not declared in this scope

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

Ok, show us the line where "Ui" is declared.

1 Like

"U" is declared in front, and ,' i' is declared in for. How to use them together?

Take this Forum as example:
there's the user


and there is the user


but there is no user


The same is true with your variables.

What do you want to get in the end?

1 Like

What value do you want Ui to contain, particularly as it is never used in the sketch ?

Do you perhaps have in mind to declare U as an array and then to store values in it indexed by the value of i ?


The newping gives out distances measured without a name, I want to use the result say U0 / U1 /U2 later on.

than you would need to declare and use an Array

1 Like

Did you mean 'U'?

Edit: slow response.
Please Unlike this response.

1 Like

You cannot invent variable names at runtime but you could use an array U[3] and hold values in the array addresses of U[0], U[1] and U[2]

1 Like


You really should check the return type of ping_cm - 255xm.isnt that far, and a sensor could return a valid, bigger value.
And you really should ping just once, and store the result.

1 Like

The question here is how to store the result?

Could that be the answer?

uint8_t U = 0; changed into: uint8_t U[3] = {0}; works.

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