Project tutorial

# Generating Tones of Different Frequencies Using Mathematics © GPL3+

Beauty of mathematical music!

• 2,491 views
• 13 respects

## Components and supplies

 Arduino UNO & Genuino UNO
×1
 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 Resistance value of > 221 can also be used just to make circuit secure from any sort of damage
×1
 Male/Male Jumper Wires
×2

New journey has begun where ideas can be easily implementedusing open source community (thanks to Arduino). So here is a way:

· Look around yourself andobserve your surrounding

· Discover problems that need to be solved

· Think, try, and keep learning and share

· Most important, what I call is KEEP IT SIMPLE :-)

· Repeat

My intention was to make a musical-based project using Arduino platform.

So I searched over internet and found amazing creative projects but I wanted to create my own stuff!

So I came across this tone() function from arduino library.Below is a link you can find descriptive information about it

Basically tone() function as you would have read generates sound of some frequency you pass it in a parameter. So I thought how can I use this single function to generate different kinds of sound?

The answer was in mathematics. We all use math everyday. It's logic is about solving mysteries of this universe (sentence from famous Numb3rs TV series)...

The idea is to make sound which can be generated using mathematical equations. Let’s call it "MathMusic."

In this project, I used:

· Area’s equation of simple geometrical shapes (square, rectangle, parallelogram, triangle, and circle)

· Quadratic equation and Fibonacci seres is

To generate sound. Now you can expand this project and discover some new music using different equations or formula…

## Code

##### MathematicalMusicArduino
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
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");

}

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;

case 7:

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

break;

}
}

}

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

## Schematics

schematic_8ONdg33104.fzz

• 5 projects
• 21 followers

August 25, 2018

#### Members who respect this project

and 7 others

See similar projects
you might like

#### Generating Audio with an Arduino and a Resistor Ladder DAC

Project showcase by 3 developers

• 3,995 views
• 11 respects

#### How to make music with an Arduino

Project tutorial by Code_and_Make

• 2,092 views
• 7 respects

#### Arduino Keyboard

Project in progress by Gabriele Scordamaglia

• 7,281 views
• 15 respects

#### Keyboard V2.0

Project in progress by Gabriele Scordamaglia

• 5,154 views
• 14 respects

#### Music Adaptive RGB LED with Arduino and Processing

Project in progress by ounane mohamed elfarouk

• 2,955 views