here’s a program I’ve worked on and off on for the past couple of months. When connected as explained in the code, twisting a pot will change the tone and twisting a different pot changes the length of the note. Code:
– The Basic Arduinstrument –
The following code lets the user determine the pitch and delay for a tone to play out of a speaker.
When [potentiometers are connected as described below to pins 19 and 18 they allow the user
to determine the pitch of a tone and the length of a tone for a range of tones calculated
in this program. To expand the range of tones, change maxTone at the beginning.
For volume control, connect a potentiometer with terminal 1 to pin 9, wiper to the positive
wire on your speaker, and terminal 3 to ground. This will attenuate the volume of the
tone generated by the arduino.
- Buttons - this code could easly be modified to change the maxTone depending on what
momentary switch is pressed, or to only play a tone when a button is pressed.
- LED’s - connect a series of LED’s to pins on the arduino and have their value correlate
to the value of the different potentiometers. An LED could pulse with HIGH and LOW.
- Anything else - this code is yours to do with what you want; please give me some credit.
int speakerPin = 9; // 8 ohm speaker; positive to pin 9, negative to Gnd
int pitchPot = 19; // potentiometer to determine pitch; left terminal to +5v, wiper to analog 5, right terminal to Gnd
int pitchVal = 0; // variable to hold the value of pitchPot
int pitchDelay = 0; // variable to hold the final value to delay between pulses HIGH and LOW
int maxTone = 1000; // lower = higher pitch, higher = lower pitch
int waitPot = 18; // potentiometer to determine delay between beats; left terminal to +5v, wiper to analog 4, right terminal to Gnd
int waitVal = 0; // variable to hold the value of waitPot
int waitDelay = 0; // variable to hold hte final value to wait between beats
int maxDelay = 1000; // maximum time per beat (in milliseconds)
unsigned long startTime = 0; // variable to hold the start time of pulsing the speaker
unsigned long endTime = 0; // variable to hold the end time of pulsing the speaker
pinMode(speakerPin, OUTPUT); // sets the speaker as an output
pinMode(pitchPot, INPUT); // allows a value to be determined from pitchPot
pinMode(waitPot, INPUT);// allows a value to be determined from waitPot
startTime = millis(); // takes the time
while(endTime - startTime < waitDelay) // while the time difference between the current time and the time before the loop is less than the desired time
endTime = millis(); // samples the current time
pitchVal = analogRead(pitchPot); // reads the potentiometer hooked up to analog 5
pitchDelay = maxTone - (pitchVal * (maxTone / pitchVal)); // calculates how long to delay between pulses
waitVal = analogRead(waitPot); // reads the potentiometer hooked up to analog 4
waitDelay = - maxDelay + (waitVal * (maxDelay / waitVal)); // calculates how long between tones
digitalWrite(speakerPin, HIGH); // pulses the speaker HIGH
delayMicroseconds(pitchDelay); // waits; the time it waits determines the pitch (longer = lower)
digitalWrite(speakerPin, LOW); // pulses the speaker LOW
delayMicroseconds(pitchDelay); // waits; the time it waits determines the pitch
delayMicroseconds(waitDelay); // waits for the same amount of time the speaker played a tone
endTime = 0; // resets endTime