Last Updated: 28/07/2021
Lesson 8 - Mathematics
Lessons >> Basics >> Lesson 8 - Mathematics
Lesson Contents
Basic mathematics
Addition
Subtraction
Multiplication
Division
Learning how mathematics can lead to overflows in a numeric variable
The use of a 'long'
How to get a 'long' answer from 'int' variables
Basic errors in changing an 'int' to a 'long'
How to run something once in the main loop using an 'if' statement
Dealing with values after the decimal point
Using a 'float' data type
How to get a 'float' for an 'int'
Common problems in getting a 'float' for an 'int'
Example 1: mathematics1.ino
Click to Download code:mathematics1.ino
Addition)
Data overuns
Creating a 'long' from the addition of int's
Common errors when creating a long from int.
Getting code to run once in the main loop using an 'if' statement
/* 24/07/2021
mathematicsv1
Addition,
how to make something run once in the main loop.
importance of commenting code
How integers can over run with addition
*/
int q;
int w;
int myAnswer;
long myLongAnswer;
void setup() {
// Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
Serial.begin(9600);
//Send script name
Serial.println("mathematicsv1...");
//this will print a blank line
Serial.println(" ");
}
void loop() {
if (q < 1) {
//Example 1
//add 1 to the value of q so the if statement will fail next time round
q++;
Serial.print("q = ");
Serial.println(q);
//Simple addition
Serial.println("Simple addition");
for (w = 0; w < 10; w++) {
myAnswer = w + q;
Serial.print(" w =");
Serial.print(w);
Serial.print(" myAnswer =");
Serial.println(myAnswer);
}
//Example 2
//change the value of q to 32760
q = 32760;
Serial.println("Simple addition...The problem of overflowing the variable type");
Serial.print("q = ");
Serial.print(q);
for (w = 0; w < 10; w++) {
myAnswer = w + q;
Serial.print(" w =");
Serial.print(w);
Serial.print(" myAnswer =");
Serial.println(myAnswer);
}
//Example 3
q = 32760;
Serial.println("Simple addition... 2 integers don't make a long");
Serial.print("q = ");
Serial.print(q);
for (w = 0; w < 10; w++) {
myLongAnswer = w + q;
Serial.print(" w =");
Serial.print(w);
Serial.print(" myLongAnswer =");
Serial.println(myLongAnswer);
}
//Example 4
q = 32760;
Serial.println("Simple addition... 2 integers converted to a long");
Serial.print("q = ");
Serial.print(q);
for (w = 0; w < 10; w++) {
myLongAnswer = long(w) + long(q);
Serial.print(" w =");
Serial.print(w);
Serial.print(" myLongAnswer =");
Serial.println(myLongAnswer);
}
}
}
Example 2: mathematicsv2.ino
Click to Download code:mathematicsv2.ino
Subtraction.
Issues with subtracting a negative number
Negative data overruns
.
/* 24/07/2021
mathematicsv2
Subtraction
How integers can over run with subtraction
*/
int q;
int w;
int myAnswer;
void setup() {
// Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
Serial.begin(9600);
//Send script name
Serial.println("mathematicsv2...");
//this will print a blank line
Serial.println(" ");
}
void loop() {
if (q < 1) {
//Example 1
//make q = 1 so the if statement will fail next time round
q = 1;
Serial.print("q = ");
Serial.println(q);
//Simple addition
Serial.println("Simple subtraction");
for (w = 0; w < 10; w++) {
myAnswer = w - q;
Serial.print(" w =");
Serial.print(w);
Serial.print(" myAnswer =");
Serial.println(myAnswer);
}
//Example 2
q = -10;
Serial.println("Simple subtraction... - negative number double negative = positive");
Serial.print("q = ");
Serial.println(q);
for (w = 0; w < 10; w++) {
myAnswer = w - q;
Serial.print(" w =");
Serial.print(w);
Serial.print(" myAnswer =");
Serial.println(myAnswer);
}
//Example 3
q = -10;
Serial.println("Simple subtraction...negative numbers");
Serial.print("q = ");
Serial.println(q);
for (w = 0; w < 10; w++) {
myAnswer = q - w;
Serial.print(" w =");
Serial.print(w);
Serial.print(" myAnswer =");
Serial.println(myAnswer);
}
//example 4
//change the value of q to 32760
q = -32760;
Serial.println("Simple subtraction...The problem of overflowing the variable type");
Serial.print("q = ");
Serial.println(q);
for (w = 0; w < 10; w++) {
myAnswer = q - w;
Serial.print(" w =");
Serial.print(w);
Serial.print(" myAnswer =");
Serial.println(myAnswer);
}
//make q equal to 1 to make sure the if loop does not run again
q = 1;
}
}
Example 3: mathematicsv3.ino
Click to Download code:mathematicsv3.ino
Multiplication.
Strange values from data over runs
Getting a 'long' answer from multiplication of 'int's'
/* 24/07/2021
mathematicsv3
Multiplication
How integers can over run with multiplication and give very unexpected results
*/
int q;
int w;
int myAnswer;
long myLongAnswer;
void setup() {
// Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
Serial.begin(9600);
//Send script name
Serial.println("mathematicsv3...");
//this will print a blank line
Serial.println(" ");
}
void loop() {
if (q < 1) {
//Example 1
//make q = 1 so the if statement will fail next time round
q = 1;
Serial.print("q = ");
Serial.print(q);
//Simple multiplication
Serial.println("Simple multiplication");
for (w = 0; w < 10; w++) {
// the symbol for multiplication is *
myAnswer = w * q;
Serial.print(" w =");
Serial.print(w);
Serial.print(" myAnswer =");
Serial.println(myAnswer);
}
//change the value of q to 32760
q = 32760;
//the answer can be positive or negative depending on the amount of over run.
Serial.println("Simple multiplication...The problem of overflowing the variable type");
Serial.print("q = ");
Serial.print(q);
for (w = 0; w < 10; w++) {
myAnswer = w * q;
Serial.print(" w =");
Serial.print(w);
Serial.print(" myAnswer =");
Serial.println(myAnswer);
}
//Example 3
//change the value of q to 32760
q = 32760;
Serial.println("Simple multiplication... 2 integers converted to a long");
Serial.print("q = ");
Serial.print(q);
for (w = 0; w < 10; w++) {
myLongAnswer = long(w) * long(q);
Serial.print(" w =");
Serial.print(w);
Serial.print(" myLongAnswer =");
Serial.println(myLongAnswer);
}
}
}
Example 4: mathematicsv4.ino
Click to Download code:mathematicsv4.ino
Division
Problems with results being rounded down
Using the 'float' variable type
Converting divided int's into a float
Common problems when converting numbers to a float.
Increasing the number of values beyind the decimal point when printing to the Serial monitor.
Remember Arduinos don't like numbers with decimal points.
Alternates to using a float by multiplying values by 100 or 1000.
/* 24/07/2021
mathmaticsv4
Division
Understand the issues with division of integers.
learn how to use a float.
Arduinos do NOT like floats, they use a lot of processor time compared to whole numbers.
*/
int q;
int w;
int myAnswer;
float myFloat;
void setup() {
// Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
Serial.begin(9600);
//Send script name
Serial.println("mathematicsv4...");
//this will print a blank line
Serial.println(" ");
}
void loop() {
if (q < 1) {
//Example 1
//add 1 to the value of q so the if statement will fail next time round
q++;
Serial.print("q = ");
Serial.println(q);
//Simple addition
Serial.println("Simple division");
for (w = 0; w < 10; w++) {
// the symbol for division is /
myAnswer = w / q;
Serial.print(" w =");
Serial.print(w);
Serial.print(" myAnswer =");
Serial.println(myAnswer);
}
//Example 2
//change the value of q to 10
q = 10;
//Notice that an integer cannot have a decimal point
Serial.println("Simple division...all the results are rounded down.");
Serial.print("q = ");
Serial.println(q);
for (w = 0; w < 31; w++) {
myAnswer = w / q;
Serial.print(" w =");
Serial.print(w);
Serial.print(" myAnswer =");
Serial.println(myAnswer);
}
//Example 3
//change the value of q to 10
q = 10;
//Notice that an integer cannot have a decimal point
//helps with the issue of dividing and then multiplying
Serial.println("Simple division...multiplying by 100 to use a whole number.");
Serial.print("q = ");
Serial.println(q);
for (w = 0; w < 31; w++) {
myAnswer = w * 100 / q;
Serial.print(" w =");
Serial.print(w);
Serial.print(" myAnswer =");
Serial.println(myAnswer);
}
//Example 4
//change the value of q to 10
q = 10;
//Using a float to get a decimal point
Serial.println("Simple division...Using a float");
Serial.print("q = ");
Serial.println(q);
for (w = 0; w < 31; w++) {
//because the division is between two integers there is no decimal value even when we save the result as a float.
myFloat = w / q;
Serial.print(" w =");
Serial.print(w);
Serial.print(" myFloat =");
Serial.println(myFloat);
}
//Example 4
q = 10;
//Using a flat to get a decimal point
Serial.println("Simple division...Using a float");
Serial.print("q = ");
Serial.println(q);
for (w = 0; w < 31; w++) {
myFloat = float(w) / float(q);
Serial.print(" w =");
Serial.print(w);
Serial.print(" myFloat =");
//Only prints to 2 decimal places by default
Serial.print(myFloat);
Serial.print(" myFloat to 4 decimal points =" );
Serial.println(myFloat,4);
}
}
}
Additional Resource Links
Don't forget to use the Reference in you Arduino IDE and look at Arithmetic for more help on this lesson.Comments
This site has been designed to be child friendly, this means that comments cannot be added to videos or directly to the site. To add a comment or ask a question please email the address in this image: and use Lesson 8 - Mathematics as a reference.