Logical Flaw In Your Code
You need to have a clear understanding on how overflow/underflow works,
if (studentsNUMBER < 0 or studentsNUMBER > 32767){
...
}
You have declared studentsNUMBER as an integer whose max value can be 32767. Now you are comparing studentNUMBER > 32767 which is will never be the case as studentNUMBER will never be able to cross the limit.
Suggestions
Mind that once there has been overflow/underflow, Undefined Behavior is occurred and your program can do anything abnormal.
One of many approaches to overcome such situation can be like,
long result,
int studentNUMBER, keyENTERED;
...
result = (studentsNUMBER * 10) + keyENTERED;
if (result > 32767)
{
// overflow occurred
}
Hope you will get a concept.
Answer from Sazzad Hissain Khan on Stack OverflowWeird Arduino math....
If you look in the log window you will see the compiler complains about an integer overflow calculating (410*410). It is treating 410 as a 16 bit integer, so overflows at 65534. You need to tell the compiler that you are using the long type.
unsigned long Potencia = long(410)*long(410); or unsigned long Potencia = (410L*410L);
Interesting problem. If you watch log window while compiling and you see red text flying by, it means there is a compiler warning. You can click in the log window, press <Ctrl>A then <Ctrl>C to copy the log. Then you can paste into Notepad with <Ctrl>V so you can review the results.
warning: integer overflow in expression [-Woverflow]
Potencia = (410*410) ;
^ More on reddit.com bluetooth - integer overflow in expression [-Woverflow] - TinyCircuit - Arduino Stack Exchange
c++ - integer overflow in expression of type 'int' results - Arduino Stack Exchange
Integer overflow error help
Im getting weird results... is my Arduino nano about to die??
i've fixed the reading to 410 just in case i had a bad reading on the potentiometer. soy you can disregard the first part.
Serial.print("vision logaritimca: _pot1:");
int Reading= analogRead(ch1l);
Serial.print(Reading);
Serial.print(" a la potencia de 2: ");
unsigned long Potencia = (410*410) ;
Serial.println(Potencia);
Serial.print("Resultado: ");
Serial.print(potencia/4104);Acording to mi calculator, 410 by 410 is 168100
but this is what im geting intstead.... What im doing wrong, sorry im tired debugging and this is beyond my actual comprehension, i hope this has a simple answer
If you look in the log window you will see the compiler complains about an integer overflow calculating (410*410). It is treating 410 as a 16 bit integer, so overflows at 65534. You need to tell the compiler that you are using the long type.
unsigned long Potencia = long(410)*long(410); or unsigned long Potencia = (410L*410L);
Interesting problem. If you watch log window while compiling and you see red text flying by, it means there is a compiler warning. You can click in the log window, press <Ctrl>A then <Ctrl>C to copy the log. Then you can paste into Notepad with <Ctrl>V so you can review the results.
warning: integer overflow in expression [-Woverflow]
Potencia = (410*410) ;
^
410*410=-28508 due to int16 overflow - and int16 is the default arduino integer.
But when you write negative into unsigned long, it results in uint32 overflow and stored number is 4294967296-28508.
So everything is fine and makes perfect sense. To get proper number, first store 410 in unsigned long, and then multiply that variable