Notifications
Clear all

Issue with simple sketch

10 Posts
4 Users
9 Likes
1,139 Views
(@tperry724)
Member
Joined: 4 years ago
Posts: 33
Topic starter  

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 reacted
Quote
(@starnovice)
Member
Joined: 5 years ago
Posts: 110
 

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

Pat W

Pat Wicker (Portland, OR, USA)


   
Squish and tperry724 reacted
ReplyQuote
(@tperry724)
Member
Joined: 4 years ago
Posts: 33
Topic starter  

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


   
Squish reacted
ReplyQuote
Squish
(@squish)
Member
Joined: 4 years ago
Posts: 33
 

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 reacted
ReplyQuote
ZoolanderMicro
(@zoolandermicro)
Member
Joined: 4 years ago
Posts: 144
 

@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 reacted
ReplyQuote
ZoolanderMicro
(@zoolandermicro)
Member
Joined: 4 years ago
Posts: 144
 

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 reacted
ReplyQuote
ZoolanderMicro
(@zoolandermicro)
Member
Joined: 4 years ago
Posts: 144
 

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 reacted
ReplyQuote
(@tperry724)
Member
Joined: 4 years ago
Posts: 33
Topic starter  

@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)
Member
Joined: 4 years ago
Posts: 144
 

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 reacted
ReplyQuote
(@tperry724)
Member
Joined: 4 years ago
Posts: 33
Topic starter  

@zoolandermicrob  Thank you for the recommendations.  


   
ReplyQuote