Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Jalal Mansoori
Published © GPL3+

Generating Tones of Different Frequencies Using Mathematics

Beauty of mathematical music!

BeginnerFull instructions provided6,732
Generating Tones of Different Frequencies Using Mathematics

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Speaker: 0.25W, 8 ohms
Speaker: 0.25W, 8 ohms
You can use buzzer as well but i would recomment speaker for good quality of sound
×1
Resistor 221 ohm
Resistor 221 ohm
Resistance value of > 221 can also be used just to make circuit secure from any sort of damage
×1
Male/Male Jumper Wires
×2

Story

Read more

Schematics

Circuit Diagram

Code

MathematicalMusic

Arduino
In this code simply i used geometric shapes area formula's and quadratic equations and fibonacci series to
generate tones from tone() function
/*
This Program Generates Tones of different frequencies using simple arthimetic operations
on mathematical equations and Series.
Idea is to discover beauty of maths by generating music from it. 
By Jalal Mansoori
Thanks to
Youtube tutorials
Facebook posts
Helpul webpages
open source community ... 
*/




#include "pitches.h"
#include <string.h>
#include <math.h> // Math Library for using functions (sqrt(num), square(num) ... ) to solve equations 

#define SIZE 10
#define totalShapes 5
#define pi 3.142

int speakerPin=8;
int Area=0;

int firstTerm=0, secondTerm=1, nextTerm=0;
int numOfTerms=0;
    

int frequency [SIZE]={
  NOTE_B0  
, NOTE_C1  
, NOTE_CS1 
, NOTE_D1  
, NOTE_DS1 
, NOTE_E1  
, NOTE_F1  
, NOTE_FS1 
, NOTE_G1  
, NOTE_GS1 }; // Total 10 Frequency of Different values


int frequency2[SIZE]={
 NOTE_DS6, 
 NOTE_E6  ,
 NOTE_F6  ,
 NOTE_FS6 ,
 NOTE_G6  ,
 NOTE_GS6 ,
 NOTE_A6  ,
 NOTE_AS6 ,
 NOTE_B6  ,
 NOTE_C7  
  
  };
String  shapesName[totalShapes]={"Square", "Rectangle", "Parallelogram", "Triangle", "Circle"};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); // Initializing our Serial monitor bits per second data transfer
  Serial.println("Press Keys for Generating tones of these shapes");
 
  for(int counter=0; counter<5; counter++)
  {
    Serial.print(counter+1);
    Serial.print("                                         ");
    Serial.println(shapesName[counter]);
  }
   
  Serial.println("Series and Equations");
  Serial.println("6                                        Fibonacci Series");
  Serial.println("7                                        Quadratic Equation");
       
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available()> 0)
  {
    int incomingByte=Serial.read() - 48 ; // Because IT returns ascii value of key you press
    switch(incomingByte)
    {
      // 1 for Generating Sound from Area of Square=s^2  
      case 1:
      for(int index=0; index<SIZE; index++)
      {
         Area=square(frequency[index]);
        playAreaSound(Area);
      }
      break;
      
      // 2 for Generating Sound from Area of Rectangle=l*w
      case 2:
      for(int index=0; index<SIZE; index=index+2) //This for loop runs 5 times if you observe the condition
      {
        Area=frequency[index]*frequency[index+1];
        playAreaSound(Area);
      }
      break;
     
      // 3 for Generating Sound from Area of Parallelogram=b*h
      case 3:

      for(int index=9; index > 0; index=index-2) //This for loop runs 5 times if you observe the condition but this time from reverse condition
      {
        Area=frequency[index]*frequency[index-1];
        playAreaSound(Area);
      }     
      break;
    
      // 4 for Generating Sound from Area of Triangle=(1/2)*b*h
      case 4:

      for(int index=0; index<SIZE-1; index++)
      {
        Area=(frequency[index]*frequency[index+1])/2;
        playAreaSound(Area);
      }
      break;
    
      // 5 for Generating Sound from Area of Circle=pi*r^2
      case 5:

      for(int index=0; index<SIZE; index++)
      {
        Area=pi*square(frequency[index]);
        playAreaSound(Area);
      }
      
      break;

      // 6 for Fibonacci Series in which next term =sum of Two preceding Terms (1, 1, 2, 3, 5, 8, 13 ...and so on)
      case 6:
        
      Serial.println("Starting Ten Terms of Fibonacci Series !  ");
      
      while(numOfTerms < 10) // Starting 10 Terms will Generate fibonacci sound 
      {
        nextTerm=firstTerm+secondTerm;// These three statements generates next Term
        firstTerm=secondTerm;
        secondTerm=nextTerm;
        Area=nextTerm;
        Serial.print(Area);
        Serial.print(",");
        playAreaSound(Area*frequency[numOfTerms]);
        numOfTerms++;
      }
      Serial.println("");
     //initializing variables to its inital values 
      numOfTerms=0;
      firstTerm=0; 
      secondTerm=1; 
      nextTerm=0;
      
      break;

      //For Quadratic Equation
      case 7:

      for(int index=0; index<SIZE; index++)
      {
        playAreaSound(frequency2[index]);
      }
      
      break;
   
      
    } 
  }
  
}

void playAreaSound(int freq)
{
  tone(speakerPin, freq, 500);
  delay(10);
}

Credits

Jalal Mansoori
3 projects • 46 followers
BS Computer Science | Passionate blogger | Learn, Create, and Share
Contact

Comments

Please log in or sign up to comment.