'If', 'elseif' statements guidance

Hi,

I'm pretty new to Arduino but I am familiar with other coding languages like Matlab.

Can anyone provide an example of using if statements?
Like I'm using a sensor to measure temperature and display it on a tft screen. I've included a short code below of how I think it would be formatted but looking for some guidance if that is how it's done.

In addition, I also want to average the readings over 10 seconds. Is that possible? I do not have an SD card or such to store measurements though.

Example code:

void loop() {
sensors_event_t humidity, temp;

aht.getEvent(&humidity, &temp);

if temp.temperature < 10,
tft.textEnlarge(2);
tft.textSetCursor(110, 150);tft.textColor(CYAN, RA8875_BLACK);tft.textEnlarge(2);tft.print(temp.temperature);
else temp.temperature > 10,
tft.textEnlarge(2);
tft.textSetCursor(110, 150);tft.textColor(RED, RA8875_BLACK);tft.textEnlarge(2);tft.print(temp.temperature);
}

Thank you.

Edit:
I know the syntax above is wrong. That was not my question. It is not suppose to represent any correct code. Just an example of what sort of thing I was looking for cause English is not my first language and I describe well with examples.

My question has already been answered and it was regarding where to find information about if statements. Thanks.

1 Like

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

@ezrax123 - you need to start reading code, and coming to grips with the fact that spelling and punctuation and grammar and syntax count. And unlike Mrs. Smith, the compiler will spend no time trying to figure out what you might have meant when you stray.

You never ever saw an if in any functioning code that began like this:

if temp.temperature < 10,

a7

Thanks, exactly what I was looking for. Don't know why it wasn't appearing. :slight_smile:

Not helpful, thank you for your time though.

Sry, I thought it was very helpful to point out that making stuff up isn't going to be an effective way forward for you.

a7

How do you want to communicate correctly and completely with the compiler?
The compiler expects the syntax and rules required to write a programme for the Arduino to be adhered to.

Exactly why I did not write it as a code but an example code of what I was looking for and my best way of quickly describing it. Either way, I respect your opinion. Just not helpful for me. Thanks.

There are some issues in your code. But first, the general format of the if-else structure is:

if (expression == TRUE) {
// execute these statements if expression is TRUE
} else {
// Otherwise, execute these statements if expression is FALSE
}

Rewriting your code fragment:

void loop() {
sensors_event_t humidity, temp;

aht.getEvent(&humidity, &temp);

if (temp.temperature < 10) {
   tft.textEnlarge(2);                         // Duplicated
   tft.textSetCursor(110, 150);                // Dupe
   tft.textColor(CYAN, RA8875_BLACK);
   tft.textEnlarge(2);                         // Dupe
   tft.print(temp.temperature);                // Dupe
} else {
   tft.textEnlarge(2);                         // Dupe
   tft.textSetCursor(110, 150);                // Dupe
   tft.textColor(RED, RA8875_BLACK);
   tft.textEnlarge(2);                         // Dupe
   tft.print(temp.temperature);                // Dupe
}

This can still be improved because there are unnecessary function calls in both statement blocks. (Statement blocks should be started with '{' and closed with '}'.) They are marked with a Dupe comment. I would change the code to this form:

void loop() {
sensors_event_t humidity, temp;
int textColor;                            // New variable

aht.getEvent(&humidity, &temp);

if (temp.temperature < 10) {
   textColor = CYAN;
} else {
   textColor = RED;
}
tft.textEnlarge(2);                        
tft.textSetCursor(110, 150);         
tft.textColor(textColor, RA8875_BLACK);
tft.textEnlarge(2);                       
tft.print(temp.temperature);       

This form clearly shows that the intent of the if-else statement block code is to change the text color on the display. It is much easier to read, uses less memory, and would execute marginally faster.

1 Like

Thanks a lot and for your time! :slight_smile:

I forgot to mention one more potential problem. What if the temperature measures exactly 10. The first if statement block won't execute because it's False. In your original code, you then tested to see if the temperature was greater than 10. That, too, is False so in the original code the text color would not be set. In my code, the first expression is False, so the (default?) text color becomes RED since the first expression is False. That may or may not be what you want when the temperature is exactly 10. If that's the "desired" temperature, I'd make that text color GREEN. If the temp is too high, I'd set the color to RED (i.e.., too hot) and if the temp is too low, I'd make the color BLUE (i.e., too cold). Use the colors to convey information at a glance.

If you're interested in learning C in the Arduino IDE environment, I'd suggest Beginning C for Arduino (Amazon 1484209419). Read the reviews and see if this might be helpful. Disclaimer: I'm the author.

2 Likes

Yes, in my particular case- I'm not too worried about the value being exactly 10. But definitely an instance I did not even consider.

If it is the desired temperature, I'm guessing that I'd have to introduce a

if (temp.temperature < 10) {
   textColor = CYAN;
} 
elseif (temp.temperature = 10) {
   textColor = GREEN;
}
else {
   textColor = RED;
}

I do have multiple variables and I noticed that my code runs fine if the variable does not have a number. In other words, when I tried "int textColor_5", my screen did not display anything but when I changed it to "int textColor_a", it worked.

So I'm guessing that the variable set up cannot be numerical.

I'm not very familiar with C (at all, actually), thanks for the recommendation though! I will definitely check it out if I dabble more in software side of things.

Thanks a lot! :slight_smile:

elseif (temp.temperature = 10)

Definitely not exactly that, unless you want to set temp.temperature to 10 instead of comparing it with 10

1 Like
else if (temp.temperature == 10)

:slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.