-1

I am applying a while loop, but the loop does not stop. Also, the serialmonitor stops writing values. Why is that? Also the stop command is not working. Below is my code

while (x <= Y) //Y is a constant integer, x is defined formulaically earlier 
  {
    digitalWrite(SPin, HIGH);
    //motor running code  
    digitalWrite(dir1,HIGH);
    digitalWrite(dir2,LOW);
    analogWrite(speedPin,mSpeed);
    Serial.println(x);
    delay(50);
  }
tiktok
  • 21
  • 5
  • 3
    `while (x <= 250)` Where does `x` get updated? AFAICT, it does not - resulting in an endless `while` loop. – Johnny Mopp Feb 28 '22 at 14:31
  • If you're expecting the value of `x` to be recomputed according to your formula every time it is used, that's not how C++ works. – molbdnilo Feb 28 '22 at 14:39
  • @molbdnilo I was actually assuming just that. How do I correct it? – tiktok Feb 28 '22 at 14:41
  • @JohnnyMoppI have updated my code. Please check where I could be wrong. – tiktok Feb 28 '22 at 14:41
  • You need to compute the value on each iteration. Encapsulate the formula in a function and call it. (Also, you should stop assuming and get yourself one of [these](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Feb 28 '22 at 14:42
  • @molbdnilo How can I compute the value in each iteration when the iteration has to happen upon the calculated value? – tiktok Feb 28 '22 at 14:48
  • @tiktok `while (computed_value() <= 250)`. – molbdnilo Feb 28 '22 at 14:51
  • @tiktok *I was actually assuming just that* -- The `x` is just a `double` variable. It is set once before the loop, and no other time after that. In other words, the code you have written is doing exactly as you wrote it. There is no hidden "updates" of variables going on -- if C++ did work that way, it would be one of the most unpredictable languages to write programs in. – PaulMcKenzie Feb 28 '22 at 14:57

1 Answers1

0

You need to update the variable "x". for example if you want to repeat the loop 250 times just add 1 to x every loop.

 while (x <= y) {
    digitalWrite(solenoidPin, HIGH);//Switch Solenoid ON
    //motor running code  
    digitalWrite(dir1,HIGH);
    digitalWrite(dir2,LOW);
    analogWrite(speedPin,mSpeed);
    Serial.println(x);
    delay(50); 
    x = x+1; //this makes it so that every time the loop repeats x goes up
  }

if you are using a value from an input you must read that input during the while loop (C++ doesn't automatically update variables):

while (x <= y) {
    digitalWrite(solenoidPin, HIGH);//Switch Solenoid ON
    //motor running code  
    digitalWrite(dir1,HIGH);
    digitalWrite(dir2,LOW);
    analogWrite(speedPin,mSpeed);
    Serial.println(x);
    delay(50); 
    x = analogWrite(youranalogpin);
  }

However if you want to force-stop the loop you can use:

break;

use this only inside a if-condition, or else the loop will never work. Example:

while (x <= y) {
  if(stop /*== true*/)
    break;
}

Remember that every value must be read and stored

V.G.
  • 16
  • 2
  • But since x is drawn through a pressure sensor I should not manually alter it, it must change on its own when the pressure increases, but only until the value 250. I have updated my code, please check. – tiktok Feb 28 '22 at 14:44
  • @tiktok where is the analogRead? Which pin are you reading from? – V.G. Feb 28 '22 at 14:46
  • @tiktok Values of variables don't just change automatically. That is not how C++ or imperative programming languages in general work. You _must_ learn programming (in C++) in a structured way, for example with [one of these books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). You cannot learn it by guessing. – user17732522 Feb 28 '22 at 14:48
  • @V.G. got it working. thanks. Just one part of the query remains. When the command is "0" the process should stop. But I guess since the loop is already functioning it is not stopping. How do I terminate the function midway? – tiktok Feb 28 '22 at 15:11
  • good question @tiktok , adding it to my answer. – V.G. Feb 28 '22 at 16:01