In the following program, all numbers and variables should be positive. But, some print negative. I have heard of NEW math. But something is not right here.
The code for the sample program follows:
#include <MeMegaPi.h>
#include <stdio.h> /* math functions */
#include <Arduino.h>
#include <SoftwareSerial.h>
void setup() {
Serial.begin(115200);
}
void loop() {
int i;
for (i = 0; i < 100; i++) {
Serial.print (i);
Serial.print (",");
Serial.print (i * 500 / 400);
Serial.println(".");
delay (100);
}
}
Below is a list of the data types commonly seen in Arduino, with the memory size of each in parentheses after the type name. Note: signed variables allow both positive and negative numbers, while unsigned variables allow only positive values.
boolean (8 bit) - simple logical true/false
byte (8 bit) - unsigned number from 0-255
char (8 bit) - signed number from -128 to 127. The compiler will attempt to interpret this data type as a character in some circumstances, which may yield unexpected results
unsigned char (8 bit) - same as ‘byte’; if this is what you’re after, you should use ‘byte’ instead, for reasons of clarity
word (16 bit) - unsigned number from 0-65535
unsigned int (16 bit)- the same as ‘word’. Use ‘word’ instead for clarity and brevity
int (16 bit) - signed number from -32768 to 32767. This is most commonly what you see used for general purpose variables in Arduino example code provided with the IDE
unsigned long (32 bit) - unsigned number from 0-4,294,967,295. The most common usage of this is to store the result of the millis() function, which returns the number of milliseconds the current code has been running
long (32 bit) - signed number from -2,147,483,648 to 2,147,483,647
float (32 bit) - signed number from -3.4028235E38 to 3.4028235E38. Floating point on the Arduino is not native; the compiler has to jump through hoops to make it work. If you can avoid it, you should. We’ll touch on this later.
This way, you are not multiplying by 500; you are only multiplying by 5. This will keep the numbers small, and will help you avoid the kind of problem you are having.
If you really want to use big numbers, then try:
Serial.print (i * 500L / 400);
The letter L means that you want long integers to be used.
Long integers can go up to about 2 billion, so you are unlikely to have this problem with them.