Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Mahdi Hajebi
Published

Calculating Pi

Calculating Pi number by using John Wallis series!!

BeginnerProtip1 hour5,777
Calculating Pi

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
×1
Matrix keypa
×1
5110 lcd
×1

Story

Read more

Schematics

Pi

Pi

Code

Pi

Arduino
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();
  }

Credits

Mahdi Hajebi

Mahdi Hajebi

3 projects • 24 followers
studying mechanical engineering at hormozgan university.

Comments