
Hello.
Two weeks ago, I purchased the Arduino Duemilanove. I flashed a LED, played with LCD, and then through the list from the Examples folder. He stopped at the standard example of
Play Melody and decided to develop it a bit. I’ll say at once that I’m a teapot in creating and processing sound, therefore the terms and effects for the future synthesizer (if you can call it that) are invented in the process.
')
Functions

Keys
Only four keys with a difference of about 130 Hz, the frequency increases from left to right. More buttons were not at hand, I used small ones that came with Arduino.
Effect
This button changes the frequency multiplier function. The function changes with a given step and at a given interval, and even if the key is not clamped, the effect will continue. For example, if you select the function X * X (where X changes from 0 to 1) you get something like a disco-shot. So far there are only three effects in the code: sin (x) (0; 3.14), x * x (0; 1), 1-x (0; 1).
Record
Everything is simple here, at each iteration we write the frequency value to the array, and when the array is full, we reproduce its values in a loop. True, playing against the background of the received “sample” does not work, playing the keys creates an additional delay and it slows down a lot (whoever has a solution, I will be glad). Later added a LED next to the button that glows while recording.
Bit
Changes the gap between the individual double signals and the width of the signals. If you hold down the key and twist B, the sound is similar to the gradual stopping of the jumping ball and vice versa.
Frequency
Changes the intervals between individual signals.
Scheme
Six buttons, two potentiometers, a speaker and a LED + Arduino. The speaker and buttons are connected in succession to the digital inputs 2, 3, 4, 5, 6, 7, 8, LED to 13; potentiometers to analog inputs 0 and 1;
Code
#include <Math.h> float M=1; float x=0; int F,N,i,pl,d1,d2,d3,d4,d5,d6; int eff=0; int limit=0; float st; int arr[700]; int timer=0; int rec=0; int buf=0; int beat=0; void play(int F,int N, float M){ for(i=0;i<N/50;i++){ digitalWrite(2, HIGH); delayMicroseconds((F+1000)*2*M); digitalWrite(2, LOW); delayMicroseconds((F+1000)*2*M); } if(N!=50){ delay(N*0.1); } } void button(int num){ F-=150*(num-1); pl=1; eff=buf; if(eff!=0){ x=0; } if(rec==1){ arr[timer]=F+1000; } } void setup() { pinMode(8, INPUT); pinMode(7, INPUT); pinMode(6, INPUT); pinMode(5, INPUT); pinMode(4, INPUT); pinMode(3, INPUT); pinMode(2, OUTPUT); pinMode(13, OUTPUT); } void loop() { F = analogRead(A0); N = analogRead(A1); d1 = digitalRead(3); d2 = digitalRead(4); d3 = digitalRead(5); d4 = digitalRead(6); d5 = digitalRead(7); d6 = digitalRead(8); if(x>limit){ if(buf<4){ buf=eff; } else { buf=0; } eff=0; x=0; } if(rec==1){ timer++; arr[timer]=0; if(pl==0){ delayMicroseconds(F); } digitalWrite(13, HIGH); } else if(rec==2){ timer++; } if(eff==0){ pl=0; }else{ x+=st; } if(N<50){ N=50; eff=buf; } if(d1==1){ button(1); } if(d2==1){ button(2); } if(d3==1){ button(3); } if(d4==1){ button(4); } if(d5==1){ if(buf>3){ buf=0; } eff=buf; eff++; pl=1; } if(d6==1){ if(rec==0){ rec=1; }else if(rec==2){ rec=0; timer=0; } } if(rec==2 && arr[timer]!=0){ play(arr[timer],N,1); } if(pl==1){ if(eff==0){ play(F,N,1); } else if(eff==1){ limit=3.14; st=0.025; M=sin(x); } else if(eff==2){ limit=1.2; st=0.01; M=x*x; } else if(eff==3){ limit=1; st=0.025; M=x+0.5; } play(F,N,M); } if(rec==1 & timer>698){ timer=0; rec=2; digitalWrite(13, LOW); } if(timer>698){ timer=0; } }
Housing

Since this does not pretend to a finished device, I made a cardboard case, assembled the circuit on the breadboard and placed it inside. Soldering iron remained untouched.
Improvements
An obvious improvement in keys is to connect a keyboard, where each key changes resistance to a single analog input. You can add a mini-jack output for recording directly to a computer or connecting to an amplifier, make up a whole library of effects ... the list is limited by your imagination.