Notifications
Clear all

Issue with simple sketch  

  RSS

tperry724
(@tperry724)
Eminent Member
Joined: 1 month ago
Posts: 23
2020-11-13 5:52 pm  

This code is supposed to report that "int mynumber =5" is between 3 and 6.  It reports that it's within all ranges for some reason.  I cannot figure it out.  It must be obvious.  Please let me know what I'm doing wrong.

Best,

Tony

 

int mynumber = 5;
int delaytime = 1000;

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);

}

void loop() {
// put your main code here, to run repeatedly:

if (mynumber > 0 && mynumber <=3); {
Serial.println("You number is between 1 and 3.");
}


if (mynumber > 3 && mynumber <=6); {
Serial.println("You number is between 3 and 6.");
}


if (mynumber > 6 && mynumber <=9); {
Serial.println("Your number is between 6 and 9.");
}


Serial.println("Your number is out of range.");

delay(delaytime);

}

 


Squish liked
Quote
starnovice
(@starnovice)
Estimable Member
Joined: 1 year ago
Posts: 170
2020-11-13 8:35 pm  

Remove the ";" after your if statement and before the "{"

Pat W

Pat Wicker (Portland, OR, USA)


Squish and tperry724 liked
ReplyQuote
tperry724
(@tperry724)
Eminent Member
Joined: 1 month ago
Posts: 23
2020-11-13 8:50 pm  

@starnovice  Thank you.  I'm new to Arduino and coding.


Squish liked
ReplyQuote
Squish
(@squish)
Eminent Member
Joined: 4 months ago
Posts: 38
2020-11-17 7:37 pm  

Thanks, I'm new too, I looked and looked, and it all seemed fine to me.

So glad we have this place to turn to.

 

No such thing as too much energy, it's just un-utilised potential.


tperry724 liked
ReplyQuote
ZoolanderMicro
(@zoolandermicro)
Trusted Member
Joined: 3 months ago
Posts: 74
2020-11-17 8:13 pm  

@tperry724 Hi, Your conditional statements all prove to be true because your variable (mynumber) is greater than 0 AND less than or equal to 9. Also, each 'if' statement is tested even if the first condition is true. Try using 'if, else if, else'.

 
if (condition 1){
         Do this;
}
else if (condition 2){
          Do that;
}
else if (condition 3){
          Do something else;
}
else{
    If all else fails, do this;
}

This works better because it tests to the first true condition and skips the rest. The trailing 'else' executes when all conditions fail.

ZoolanderMicro, where small ideas are a big deal


tperry724 liked
ReplyQuote
ZoolanderMicro
(@zoolandermicro)
Trusted Member
Joined: 3 months ago
Posts: 74
2020-11-17 8:34 pm  

I don't have my Arduino with me so I cannot test this. One of my sketches uses a compound condition written like this:

// If 'i' is greater than 255 and less than or equal to 510
if ((i > 255) && (i <= 510)) { 
      redVal = 510 - i; // Red brightens as 'i' increases
      grnVal = i - 255; // Green dims as 'i' increases
      bluVal = OFF; // Blue is off
    } 

So, it is two conditions with a double ampersand in between to make an 'AND' statement. Both conditions must test true for the statement to execute.

ZoolanderMicro, where small ideas are a big deal


tperry724 liked
ReplyQuote
ZoolanderMicro
(@zoolandermicro)
Trusted Member
Joined: 3 months ago
Posts: 74
2020-11-17 8:58 pm  

A switch case would work even better.

#include <avr/io.h>
#include <util/delay.h>
void setup(){
    Serial.begin(9600);
} void loop(){ int myNumber = 5; int delayTime = 1000; switch(myNumber){ case 0 ... 3: Serial.println("You number is between 1 and 3."); break; case 4 ... 6: Serial.println("Your number is between 4 and 6."); break; case 7 ... 9: Serial.println("Your number is between 7 and 9."); break; default: Serial.println("Your number is out of range."); break; { delay(delayTime); }

ZoolanderMicro, where small ideas are a big deal


tperry724 liked
ReplyQuote
tperry724
(@tperry724)
Eminent Member
Joined: 1 month ago
Posts: 23
2020-11-17 10:04 pm  

@zoolandermicro  What are the following?

  • #include <avr/io.h>
  • #include <util/delay.h>

I've never work with these libraries.

Thank you,

Tony

 


ReplyQuote
ZoolanderMicro
(@zoolandermicro)
Trusted Member
Joined: 3 months ago
Posts: 74
2020-11-18 9:18 pm  

Hi @tperry724

    These include statements are not needed when using the Arduino IDE. Arduino already has the utility and input/output libraries. Some of my sketches have these statements because I was working from the book Make: AVR Programming, and using the author's master library. I have a couple other good programing books worth recommending:  C Programming Absolute Beginner's Guide (Greg Perry) and Beginning C for Arduino, Second Edition (Jack Purdum). 

ZoolanderMicro, where small ideas are a big deal


tperry724 liked
ReplyQuote
tperry724
(@tperry724)
Eminent Member
Joined: 1 month ago
Posts: 23
2020-11-19 12:54 pm  

@zoolandermicrob  Thank you for the recommendations.  


ReplyQuote