Wont get out of while loop, first program.

Hi, ive just started a programming course, and i am trying to do an appointment.
The code i have, that i cant get to work is:

void intast_navn() {
Serial.println(“Hvad er dit navn?”);

while (y == 0) {
int ch;
if (Serial.available()) {
ch = Serial.read();

if (ch != 32) {
ord = ch;

  • i++;*
  • }*
  • if (ch == 10) {*
  • for (int q = 0; q < 10; q++) {*
  • Serial.print((char)ord[q]);*
  • }*
  • if (q == 9)*
  • y = 1;*
  • }*
  • }*
  • }*
    }
    As i understand, my while loop should not break before, y is not equal to 0.
    In my code that means that it should break at the last line only.
    I have to move the y=1 statement, and i just cant get it to work.
    If it is placed 1 place, it will not run the Serial.print line, and if it is placed another place, it will not break the while statement.
    is there something wrong with my understanding of the language, and what can i do to help my self in a situration like this?
    this is the forst program i have ever started on.
    And sorry if my english is bad, i can be more precise if it is not understandable
    test.ino (2.37 KB)

Please use code tags when posting code so web browsers don't mangle it and make it unreadable.

Where are the two places? Which one causes which problem?

I will do that from now on.
As it is now it wont break the loop
If i put the y=1, after the next bracked, i want make it do the Serial.println.

If i move it, so it is:

if (ch == 10) {
      for (int q = 0; q < 10; q++) {
      Serial.print((char)ord[q]);
      if (q == 9){
      y = 1;

it wont write it either.

As it is now i cant get it to break the loop, i could before, but ive have fotten how :frowning:

for (int q = 0; q < 10; q++) {
          Serial.print((char)ord[q]);
        }                                      // for loop ends here, q is destroyed. 
        if (q == 9)
          y = 1;

Do you have another variable named q in your code? Either you do or you copied this wrong or it won’t compile.

The q you create in the for loop only exists in the for loop. It is destroyed at the end of the block when it goes out of scope. Google “C++ scope” for more explanation. So when you test q outside of the for loop, it can’t be the same q you were setting in the for loop. So here, unless you’ve got another variable named q somewhere that’s equal to 9, won’t set y to 1.

You can write a for loop like this:

int q;
for (q = 0; q < 10; q++) {
          Serial.print((char)ord[q]);
        }
        if (q == 9)
          y = 1;

where q is created outside of the for loop and it will continue to exist after the for loop finishes because it is scoped outside the for loop.

if (ch == 10) {
      for (int q = 0; q < 10; q++) {
      Serial.print((char)ord[q]);
      if (q == 9){
      y = 1;

There’s not enough of that showing for me to tell you why it doesn’t print. It looks like it should, but without seeing your program it’s impossible to tell if there is an error elsewhere.

Delta_G:
There’s not enough of that showing for me to tell you why it doesn’t print. It looks like it should, but without seeing your program it’s impossible to tell if there is an error elsewhere.

Code is attached!

To end an while() or for() loop, you can use the break statement, like this:

while (1) {
    int ch;
    if (Serial.available()) {
      ch = Serial.read();

      if (ch != 32) {
        ord = ch;
        i++;
      }
      if (ch == 10) {
        for (int q = 0; q < 10; q++) {
          Serial.print((char)ord[q]);
        }
        break;
      }
    }
  }
}

So, your loop will end when a New Line char is received and after the if() is executed.

Yes i have definded a global int q.
And that ofcorurse means that the int q, in the for() ive made, is unnessesary.

ill post my program:

int a; /
int q; 
int gns; 
int g = 9; 

int i;  
char ord[10];  
int y = 0; 

unsigned long btid, stid, rtid, reaktid1[10];


void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(A0, INPUT_PULLUP);

  Serial.println("Tryk paa S for at starte program");
  Serial.println("Tryk paa T for at se tidligere resultater");
  Serial.println("Tryk paa R for at nulstille programmet");
}

void intast_navn() {
  Serial.println("Hvad er dit navn?");

  while (y == 0) {
    int ch;
    if (Serial.available()) {
      ch = Serial.read();

      if (ch != 32) {
        ord[i] = ch;
        i++;
      }
      if (ch == 10) {
        
        for (q = 0; q < 10; q++) {
          Serial.print((char)ord[q]);
          
        }
         if (q == 9){
          y = 1;
         
      }
      
    }
  }
  }
}

void Reaktid() {
  if (a == 0) {
    delay(random(1000, 1500));
    digitalWrite(13, HIGH);
    millis();
    btid = millis();
    a = 1;
  }


void loop() {

  if (Serial.available()) {
    if (Serial.read() == 83) {
      intast_navn();
    }
    if (Serial.read() == 84) {
      tidligere_resultater();
    }
    if (Serial.read() == 82) {
      reset_program();
    }
  }

  Reaktid();
  if (reaktid1[g] != 0) {
    gennemsnit();
  }
}

I had somenotes in the program, that told that i were doing, but as they are written in danish, i guess they arent worth much.

Ive made alot that i havent used yet.
That might be wrong to do it that way, but i am totally now to this.

You should try your best to avoid having two variables in the same scope with the same name. Until you understand how to do that, it's probably best just to completely avoid having two variables with the same name period.

And single letter names for global variables is also bad. What is a? what is q? what does it represent? Good variable names tell you what they are. Like numberOfUnits, or howManyChickens, or speedOfCar.

When you learn to use good names for variables and functions code gets a lot easier to read. It starts to read like prose instead of code. I like when my code reads out in English exactly what it does.

How much easier is it to understand this code:

if(dogIsHungry){
    feedDog();
}

than this code

if(b){
   func1();
}

If the purpose of the WHILE loop is to read data from Serial have a look at Serial Input Basics - simple reliable ways to receive data.

If the sole purpose of the lines

      if (q == 9){
          y = 1;

is to cause the WHILE to terminate, why not just use

      if (q == 9){
             break;

Also code is much easier to understand and debug if you use meaningful names for variables rather than single letters.

...R

I see what you mean about better names, ill try to make it more readable, and see if i can understand what it is i am doing.
And i did not know about the break function, thanks for that one :wink:

Thank you for your help!

matlerbru:
And i did not know about the break function, thanks for that one :wink:

The difference with the break command, and it wouldn't matter here as it is at the end of the loop, is that it immediately exits the while loop. So anything AFTER the break won't happen.

int count = 0;
boolean keepGoing = true;

while (keepGoing) {
    count++;
    if(count == 8){
        break;
    }
    Serial.println(count);
}

will only print out 1 to 7 because the break stops the loop before you get to print the 8.

whereas:

int count = 0;
boolean keepGoing = true;

while (keepGoing) {
    count++;
    if(count == 8){
        keepGoing = false;
    }
    Serial.println(count);
}

will print 1 through 8 since keepGoing gets set to false but the while loop has to finish before it is checked again.

The difference is subtle, but can sometimes be important.