'Ui' was not declared in this scope

Hi,
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?
Thanks
Adam

#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(i);
    Serial.print("=");
    Serial.print(sonar[i].ping_cm());
    Serial.print("cm ");

    Ui = sonar[i].ping_cm();

  }
  Serial.println();
}

ERROR:

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.
Paul

1 Like

Thanks.
"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

Paul_KD7HB

and there is the user

shanren

but there is no user

Paul_KD7HBshanren

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 ?

2 Likes

Thanks.
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

yes.

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

Thanks.
The question here is how to store the result?

Could that be the answer?

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