Chapter 14 – Experiment 07 – MATH IS FUN!

This exercise does not use any sensor or device to interact with the external world, but instead uses the serial port to display some results from the code that is uploaded to the microcontroller.

The calculations are quite easy because is the classic algorithm to calculate the hypoteneuse (using the Pitagora’s theorem). The math functions can be used to compute the input or output managed by the microcontroller, a complex example can be the management of a cryptographic algorithm (to manage a secure communication between two or more endpoints).

If you read the comments in the code you can add more functionalities to the basic hypoteuse calculation.

* Math is fun!

#include “math.h”               // include the Math Library

int a = 3;
int b = 4;
int h;

void setup()                    // run once, when the sketch starts
Serial.begin(9600);           // set up Serial library at 9600 bps

// if analog input pin 0 is unconnected, random analog
// noise will cause the call to randomSeed() to generate
// different seed numbers each time the sketch runs.
// randomSeed() will then shuffle the random function.
// remove the first two characters (//) of the next line
//  randomSeed(analogRead(0));


Serial.print(“a = “);

Serial.print(“b = “);

h = sqrt( a*a + b*b );

Serial.print(“h = “);

void loop()                // we need this to be here even though its empty
// uncomment as mentioned before the comment in randomSeed and remove the first two characters (//) of the next line
//  randomnumbers();

void randomnumbers() {
int randNumber;

// print a random number from 0 to 299
randNumber = random(300);

// print a random number from 10 to 19
randNumber = random(10, 20);

delay(3000); // This value is in milliseconds – 3000 is 3 seconds

How to improve?

Only integer numbers?

Try to use float instead of int type. More info on the different type of data check the chapter “A SMALL PROGRAMMING PRIMER”.

You can use all the math functions, most of them are these:




Random Numbers


1 comment

  1. [...] Chapter 12 – Experiment 05 – PUSHBUTTON Chapter 13 – Experiment 06 – INTEGRATED CIRCUIT Chapter 14 – Experiment 07 – MATH IS FUN! Chapter 15 – Experiment 08 – INTERACTIVITY Chapter 16 – Experiment 09 – STORE DATA FOR [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

istanbul evden eve nakliyat istanbul evden eve nakliyat istanbul evden eve nakliyat sarıyer evden eve nakliyat beylikdüzü evden eve nakliyat şişli evden eve nakliyat