Components and supplies
1
Resistor 221 ohm
2
Male/Male Jumper Wires
1
Speaker: 0.25W, 8 ohms
1
Arduino UNO
Project description
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
1/* 2This Program Generates Tones of different frequencies using simple arthimetic operations 3on mathematical equations and Series. 4Idea is to discover beauty of maths by generating music from it. 5By Jalal Mansoori 6Thanks to 7Youtube tutorials 8Facebook posts 9Helpul webpages 10open source community ... 11*/ 12 13 14 15 16#include "pitches.h" 17#include <string.h> 18#include <math.h> // Math Library for using functions (sqrt(num), square(num) ... ) to solve equations 19 20#define SIZE 10 21#define totalShapes 5 22#define pi 3.142 23 24int speakerPin=8; 25int Area=0; 26 27int firstTerm=0, secondTerm=1, nextTerm=0; 28int numOfTerms=0; 29 30 31int frequency [SIZE]={ 32 NOTE_B0 33, NOTE_C1 34, NOTE_CS1 35, NOTE_D1 36, NOTE_DS1 37, NOTE_E1 38, NOTE_F1 39, NOTE_FS1 40, NOTE_G1 41, NOTE_GS1 }; // Total 10 Frequency of Different values 42 43 44int frequency2[SIZE]={ 45 NOTE_DS6, 46 NOTE_E6 , 47 NOTE_F6 , 48 NOTE_FS6 , 49 NOTE_G6 , 50 NOTE_GS6 , 51 NOTE_A6 , 52 NOTE_AS6 , 53 NOTE_B6 , 54 NOTE_C7 55 56 }; 57String shapesName[totalShapes]={"Square", "Rectangle", "Parallelogram", "Triangle", "Circle"}; 58 59void setup() { 60 // put your setup code here, to run once: 61 Serial.begin(9600); // Initializing our Serial monitor bits per second data transfer 62 Serial.println("Press Keys for Generating tones of these shapes"); 63 64 for(int counter=0; counter<5; counter++) 65 { 66 Serial.print(counter+1); 67 Serial.print(" "); 68 Serial.println(shapesName[counter]); 69 } 70 71 Serial.println("Series and Equations"); 72 Serial.println("6 Fibonacci Series"); 73 Serial.println("7 Quadratic Equation"); 74 75} 76 77void loop() { 78 // put your main code here, to run repeatedly: 79 if(Serial.available()> 0) 80 { 81 int incomingByte=Serial.read() - 48 ; // Because IT returns ascii value of key you press 82 switch(incomingByte) 83 { 84 // 1 for Generating Sound from Area of Square=s^2 85 case 1: 86 for(int index=0; index<SIZE; index++) 87 { 88 Area=square(frequency[index]); 89 playAreaSound(Area); 90 } 91 break; 92 93 // 2 for Generating Sound from Area of Rectangle=l*w 94 case 2: 95 for(int index=0; index<SIZE; index=index+2) //This for loop runs 5 times if you observe the condition 96 { 97 Area=frequency[index]*frequency[index+1]; 98 playAreaSound(Area); 99 } 100 break; 101 102 // 3 for Generating Sound from Area of Parallelogram=b*h 103 case 3: 104 105 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 106 { 107 Area=frequency[index]*frequency[index-1]; 108 playAreaSound(Area); 109 } 110 break; 111 112 // 4 for Generating Sound from Area of Triangle=(1/2)*b*h 113 case 4: 114 115 for(int index=0; index<SIZE-1; index++) 116 { 117 Area=(frequency[index]*frequency[index+1])/2; 118 playAreaSound(Area); 119 } 120 break; 121 122 // 5 for Generating Sound from Area of Circle=pi*r^2 123 case 5: 124 125 for(int index=0; index<SIZE; index++) 126 { 127 Area=pi*square(frequency[index]); 128 playAreaSound(Area); 129 } 130 131 break; 132 133 // 6 for Fibonacci Series in which next term =sum of Two preceding Terms (1, 1, 2, 3, 5, 8, 13 ...and so on) 134 case 6: 135 136 Serial.println("Starting Ten Terms of Fibonacci Series ! "); 137 138 while(numOfTerms < 10) // Starting 10 Terms will Generate fibonacci sound 139 { 140 nextTerm=firstTerm+secondTerm;// These three statements generates next Term 141 firstTerm=secondTerm; 142 secondTerm=nextTerm; 143 Area=nextTerm; 144 Serial.print(Area); 145 Serial.print(","); 146 playAreaSound(Area*frequency[numOfTerms]); 147 numOfTerms++; 148 } 149 Serial.println(""); 150 //initializing variables to its inital values 151 numOfTerms=0; 152 firstTerm=0; 153 secondTerm=1; 154 nextTerm=0; 155 156 break; 157 158 //For Quadratic Equation 159 case 7: 160 161 for(int index=0; index<SIZE; index++) 162 { 163 playAreaSound(frequency2[index]); 164 } 165 166 break; 167 168 169 } 170 } 171 172} 173 174void playAreaSound(int freq) 175{ 176 tone(speakerPin, freq, 500); 177 delay(10); 178} 179
Downloadable files
Circuit Diagram
Circuit Diagram
Circuit Diagram
Circuit Diagram
Comments
Only logged in users can leave comments