Help with conditions

Hello,
I'm trying to use some conditions based in an integer value (from a touch screen). Though I have the values changed at my serial monitor I cannot find the proper way ti implement them to my code (not enough knowledge I guess). I tried if, while, do...while statements without success. Any help will be appreciated.


```cpp
int gests = 0;
int gestureData = 0;
float temp = 0;
float hum = 0;


void setup() {
  Serial.begin(115200);
  I2C_16Bit_begin();
#ifdef GFX_EXTRA_PRE_INIT
  GFX_EXTRA_PRE_INIT();
#endif
  gfx->begin();
  gfx->setTextColor(WHITE, BLACK);
  gfx->setTextSize(4);
  gfx->fillScreen(BLACK);

  gfx->draw16bitRGBBitmap(35, 60, (const uint16_t *)TEMP, 28 /*IMG_WIDTH*/, 47 /*IMG_HEIGHT*/);
  gfx->draw16bitRGBBitmap(35, 120, (const uint16_t *)HUM, 32 /*IMG_WIDTH*/, 47 /*IMG_HEIGHT*/);
  gfx->drawRoundRect(90, 200, 60, 30, 25, WHITE);
  gfx->drawRoundRect(88, 198, 64, 34, 25, WHITE);
}


void loop() {
  uint16_t registerData = I2C_16Bit_readFromModule(I2C_ADDR, DATA_REGISTER_ADDR);
  gests = registerData / 100;


  temp = analogRead(A0) / 8.2;
  gfx->setCursor(80, 70);
  gfx->println(temp);

  hum = analogRead(A2) / 3.9;
  gfx->setCursor(80, 130);
  gfx->println(hum);
  gfx->fillCircle(105, 215, 12, BLUE);
  gfx->fillCircle(135, 215, 12, RED);
  // do { gfx->fillCircle(135, 215, 12, BLACK); } while (gestureData == 12);
  // do { gfx->fillCircle(105, 215, 12, BLACK); } while (gestureData == 30);
  while (gestureData == 12) { gfx->fillCircle(135, 215, 12, BLACK); }
  while (gestureData == 30) { gfx->fillCircle(105, 215, 12, BLACK); }
  Serial.println(gests);
  delay(900);
}

Lucas

What is the point of this line - to endlessly redraw the same circle until the data changes?

Welcome to the forum

if (condition == true)
{
  //conditional code here
}

The conditional code will execute once and the program will move on until it encounters the test again and so on

while (condition == true)
{
  //conditional code here
}

The conditional code will execute over and over all the time that the condition is true. The code will not be executed even once unless the condition is true

do 
{
  //conditional code here
}
while (condition == true);

The conditional code will execute over and over all the time that the condition is true. The code will be executed once before the condition is evaluated no matter whether it is true or false

A common mistake is to do this

if (condition == true);
{
  //code here is not conditional and will always be executed
  //because of the semicolon at the end of the test line
}

Can you give an example of a test where you have had a problem

You are right b707, but at this point I'm focused on the change (when gestureData will change).
I will deal with this later, except I'm missing something and this causes the problem at my code.
Lucas

Hi UKHeliBob ,
Thanks for your hospitality.
You are right. This caused the problem.
A common mistake is to do this

** **if (condition == true);** **{** ** //code here is not conditional and will always be executed** ** //because of the semicolon at the end of the test line** **}** **

Can you give an example of a test where you have had a problem
[/quote]

Can you suggest how to solve it ?
Thanks a lot.
Lucas

Remove the semi colon after true)

How can gestureData change in your code if you don't update it?
It's an endless cycle

Hi build_1971,
Lots of help here, thank you All. Do you mean like this:


```cpp
   if (gestureData == 12)  gfx->fillCircle(135, 215, 12, BLACK); 
   if (gestureData == 30)  gfx->fillCircle(105, 215, 12, BLACK); 

Because I just tried it and it doesn't work.
Lucas

As suggested, remove the semicolon. It is the only code whose execution is conditional on the test returning true

To help, always put the conditional code block in curly brackets (as in my examples above), even if is only a single code statement ,but whatever you do, don't put a semicolon after the test unless you really, really know what you are doing. Yes, there are times when you might want to have one there after a while statement but not after an if statement

What's exactly "doesn't work" mean?
The method `fillCircle' doesn't called?
Why it have to call, if you set the value of gestureData to zero and do not update it in the code?

My mistake because of my poor English, I removed the curly braces instead of semicolon.
I will give it a try

gesture data is updated in every cycle b707

please point the line in the code

I guess here:


```cpp
void loop() {
  uint16_t registerData = I2C_16Bit_readFromModule(I2C_ADDR, DATA_REGISTER_ADDR);
  gests = registerData / 100;

These lines affects two variables - registerData and gests. The gestureData variable is not used in the code after its initialization at the beginning of the sketch.

So in your conditions the value is zero and conditions are never match

Oops, So very sorry (probably has to do with my age). Thanks for pointing this and I'm really sorry for wasting your time for something obvious like this.
Lucas

After you will correct the typo with the name of the variables, pay attention to the message #7. The while loop cannot be used like this. Once the program gets into it, it can no longer exit, because the value of the gestureData inside the while loop is not updated.

Thanks a lot.
Which approach would you suggest ?

Like this works fine:


```cpp
switch (gests) {
    case 12: gfx->fillCircle(135, 215, 12, BLACK); break;
    case 30: gfx->fillCircle(105, 215, 12, BLACK); break;

Any opinions ?

it depends on what you need these conditions for.
It's not entirely clear for me what you are trying to do.

But in any case, this using of while loops are incorrect.
Either use the if operator, or put the gestureData reading inside the while loop