Need to limit a variable

Hey Guys,
I wrote a program here for determining the the color bands on a resister from 1 ohm to 999 ohms. The math in the program only works for between 1&999 ohms, but I can’t seem to figure out how to limit the variable R so that if anything below 1 or anything above 999 is enetered, that it instead displays an error message.

``````int Hplace;
int Tplace;
int Oplace;
int n;
int R;
int temp;
void setup(){
Serial.begin(9600);
}

void loop(){

while (Serial.available() == 0){

}

if (Serial.available() >= 0){

R = Serial.parseInt();

Hplace = (R/100);
Tplace = (R/10) % 10;
Oplace = R%10;
Serial.print("A"); Serial.print(" "); Serial.print(R); Serial.print(" "); Serial.print("Ohm resistor has a color code of:");
Serial.println("");

for(n =1; n<=3; n++){
if(n==1){
temp = Hplace;
}
else if(n==2){
temp = Tplace;
}
else{
temp = Oplace;
}

switch(temp){

case 0:
Serial.println("Black");
break;

case 1:
Serial.println("Brown");
break;

case 2:
Serial.println("Red");
break;

case 3:
Serial.println("Orange");
break;

case 4:
Serial.println("Yellow");
break;

case 5:
Serial.println("Green");
break;

case 6:
Serial.println("Blue");
break;

case 7:
Serial.println("Violet");
break;

case 8:
Serial.println("Gray");
break;

case 9:
Serial.println("White");
break;
}

}

}

}
``````

OK i’ve split the action of getting input into it’s own function. Here’s the result

``````int Hplace;
int Tplace;
int Oplace;
int n;
int R;
int temp;
void setup(){
Serial.begin(9600);
}

void loop(){

getValue();

Hplace = (R/100);
Tplace = (R/10) % 10;
Oplace = R%10;
Serial.print("A"); Serial.print(" "); Serial.print(R); Serial.print(" "); Serial.print("Ohm resistor has a color code of:");
Serial.println("");

for(n =1; n<=3; n++){
if(n==1){
temp = Hplace;
}
else if(n==2){
temp = Tplace;
}
else{
temp = Oplace;
}

switch(temp){

case 0:
Serial.println("Black");
break;

case 1:
Serial.println("Brown");
break;

case 2:
Serial.println("Red");
break;

case 3:
Serial.println("Orange");
break;

case 4:
Serial.println("Yellow");
break;

case 5:
Serial.println("Green");
break;

case 6:
Serial.println("Blue");
break;

case 7:
Serial.println("Violet");
break;

case 8:
Serial.println("Gray");
break;

case 9:
Serial.println("White");
break;
}

}

}

void getValue()
{
R=0;
while((R<1)||(R>999))
{
//wait for data
while (!Serial.available());
//get the value
R=Serial.parseInt();
//This next bit discards any newline characters
//or other rubbish that may still be in the buffer
while (Serial.available())
if((R<1)||(R>999))
Serial.println("Now be sensible! between 1 and 999");
}
}
``````
``````         case 0:
Serial.println("Black");
break;

case 1:
Serial.println("Brown");
break;

case 2:
Serial.println("Red");
break;
``````

It would be much simpler to use an array to hold the names of the colours.

The IDE has CTRL-T for automatic layout. makes the code style more consequent => better readable. Check it out

``````   if((R<1)||(R>999))
Serial.println("Now be sensible! between 1 and 999");
``````

you might use

``````  R = constrain(R, 1, 999); // force R between two values.
``````

robtillaart:
The IDE has CTRL-T for automatic layout. makes the code style more consequent => better readable. Check it out

OK I'll check it out. Can't promise I'll ALWAYS use it though

fair enough, but I expect that if you get used to readable code you will start to miss it when not used.

FYI, I use mostly CTRL-DOWN T S U CTRL-UP (format save upload) ....