While loop

Good evening gentleman,
I have a little problem making a while loop work , ive tried other examples of while or dowhile loops but that didnt work either.

here is the source code:

There is more to the code than this, but the programs does not seem to get out of the loop once the value of the sensor (senzorSpate) is > than 500.

Any ideas why ? I dont necessarily want to use while, i can use dowhile aswell, I tried it with that aswell but it did not work. I know Im doing something wrong...but dont know what, other eyes may see it better(for sure) as Im a newbie.
Thanks, and sorry for the bad english.

while(senzorSpate < 500)
  {
    int senzorSpate

You've introduced a new variable, so the one controlling the loop cannot be accessed and therefore updated in the body of the loop.
Lose the "int"

Please use code tags when posting code.

Perhaps this might help explain:

void setup(){
  Serial.begin(9600);
  }
void loop(){
  int senzorSpate = 490;          // The first definition of senzorSpate, call it Scope 1
  while(senzorSpate < 500)
  {
    int senzorSpate = 510;       // The second definition of senzorSpate, call it Scope 2
    Serial.print("SenzorSpate:");
    Serial.println(senzorSpate,DEC);
  }

}   // Closing while brace

You have defined senzorSpate just before the while loop. Let's say that variable ends up at memory address 100. Inside the while loop, you do a second definition of senzorSpate, and let's assume it ends up at memory address 200. Any time you define two variables within the same program using the same name, the most recent definition is usually the one that is "in scope". That is, it is the one that the compiler is currently using. In your code, however, senzorSpate defined within the while loop "dies" when the closing brace of the while loop is reached. This means that the senzorSpate with the memory address of 100 is controlling the loop. This is why the sample program continues to execute the while loop even though we have assigned the Scope level 2 senzorSpate a value of 510.

Thanks for the reply econjack and AWOL, it is working now and I understood why....I really appreciate the help ! Have a nice day/evening :slight_smile:

Good evening/day
Its me again, I have another problem with a "while loop".
Everything works fine, but the last "while" loop does not want to start, the program simply stops before it, Im missing something over here, and I dont know what, could anybody please take a look ? If any other information is needed I will provide it.
Thanks in advance.
Have a pleasant day!

Think about what caused the previous while loop to exit.

(Your indentation could do with some work - try using the auto-format tool)

  if (encoder=true){

Thislooksuseful. Not!

The previous loop exited because senzorFata was > than 500. The last loop is about senzorSpate, if thats what you mean.
They are not related, or is it something else that you are trying to tell me and I`m too dumb to see it ? ^^
After the next to last "while" loop I make the robot to turn, activating one of the motors as you can observe, so none of the sensors (senzorSpate,senzorFata) are > than 500.
Have a nice day !

rolilas:
The previous loop exited because senzorFata was > than 500. The last loop is about senzorSpate, if thats what you mean.

There are THREE loops in your code. The first and third loops have the same loop condition, so if the condition is right to terminate the first loop it will also be right to terminate the third loop.

rolilas:
They are not related, or is it something else that you are trying to tell me and I`m too dumb to see it ? ^^

The assignment operator (=) is not interchangeable with the equality operator (==).

Ok, so I managed to make the loop work by putting a "senzorSpate = 0;" before it. Thank you all for helping a newbie out on his 1st project, have a nice day/evening gentleman