Pages: [1]   Go Down
Author Topic: Arduinstrument  (Read 517 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
what would i do without arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys,
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:
Quote

 
/*
-- 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.

 Modifications:
1. 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.
2. 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.
3. 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


void setup()
{
  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
}

void loop()
{
  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
}


 
Logged

Pages: [1]   Go Up
Jump to: