Calculating Pi

Calculating Pi number by using John Wallis series!!

  • 959 views
  • 1 comment
  • 1 respect

Components and supplies

Abx00004 iso both
Arduino MKR1000 & Genuino MKR1000
×1
Matrix keypa
×1
5110 lcd
×1

About this project

There are many ways to calculate Pi! most common way is using one of many series that are available! a series is consist of infinite number of therms, as we use more terms of series, our Pi number will be more correct!

I want to build a device that asks user how many terms of series user want to calculate! then microcontroller will calculate Pi number as exact as we want! i want to use a series that is fined by John Wallis!

at first we have to wire our components! as is shown in schematic!

Now we have to code! for receiving numbers from key pad we write the code in loop. key pad give us chars, for converting char to integer we have to mines 48 from what we receive. and when we press *, the number of terms will go to next function!

 char customKey = customKeypad.getKey(); 
 if (customKey){ 
     if (customKey=='*'){ 
   pi(m); //calculating pi number with m terms!! 
   m=0; 
 } 
 else 
 { 
   Serial.println(customKey); 
   m*=10; 
   m+=(int(customKey)-48); // convert char to int by -48!! 
   }; 
 } 

For calculating Pi we will write a function that receive number of required terms, then calculate the Pi and show it on LCD.

void pi(int m){ 
 double result=4; 
 for (int x=1; x<m; x++){ 
   result*=(2*x); 
   result/=(2*x+1); 
   result*=(2*x+2); 
   result/=(2*x+1); 
   } 
   Serial.println(result,76); 
 display.clearDisplay(); 
 display.setTextSize(1); 
 display.setTextColor(BLACK); 
 display.setCursor(0,0); 
 display.println(result,76); //showing pi number on lcd 
 display.setTextColor(WHITE, BLACK); 
 display.display(); 
 } 

now we want to test it!!

At the first time we gave 1 to key pad, and mkr1000 calculate only one term of series! and the result will be 4!

and if we calculate 5 terms! the Pi will be 3.3023! and so on...

Code

PiArduino
int m=0;  // number of sets!
#include <Keypad.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
Adafruit_PCD8544 display = Adafruit_PCD8544(4, 5, 3, 2, 1);

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','F'}
};
byte rowPins[ROWS] = {13, 12, 11, 10}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 


void setup(){
  Serial.begin(9600);
  display.begin();
  display.setContrast(50);
  display.clearDisplay();
}
  
void loop(){
  char customKey = customKeypad.getKey();
  
  if (customKey){
      if (customKey=='*'){
    pi(m); //calculating pi number with m terms!!
    m=0;
  }
  else
  {
    Serial.println(customKey);
    m*=10;
    m+=(int(customKey)-48); // convert char to int by -48!!
    };
  }
}

void pi(int m){
  double result=4;
  for (int x=1; x<m; x++){
    result*=(2*x);
    result/=(2*x+1);
    result*=(2*x+2);
    result/=(2*x+1);
    }
    Serial.println(result,76);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor(0,0);
  display.println(result,76); //showing pi number on lcd
  display.setTextColor(WHITE, BLACK);
  display.display();
  }

Schematics

pi_kBIe4I4gpH.fzz

Comments

Similar projects you might like

Turn Lights On When Approaching Home

Project tutorial by Andrei Florian

  • 2,244 views
  • 5 comments
  • 11 respects

Arduino Oscilloscope (6-Channel)

by Meeker6751

  • 2,397 views
  • 5 comments
  • 17 respects

Gesture Control Robot

Project tutorial by Raunak Oberai

  • 1,810 views
  • 6 comments
  • 9 respects

Matrix Orbital GTT TFT Thermometer Stage 1

Project tutorial by MatrixOrbital

  • 3,985 views
  • 0 comments
  • 11 respects

Air Quality Monitoring

Project tutorial by Ruchir Sharma

  • 499 views
  • 0 comments
  • 6 respects

Temperature and Humidity Sensor

by Amarantowy

  • 353 views
  • 1 comment
  • 3 respects
Add projectSign up / Login