Image

Homework 04 -NoiseMaker- Dennis MacAvaney

5 Nov

Homework 04 -NoiseMaker- Dennis MacAvaney

/*While the button is pressed, the buzzer makes a tone determined by the potentiometer.
If you double click the button, the buzzer locks on until you double click again.
If you turn the pitch up too high, the buzzer will turn off unless the button is being held down.*/

int count=0;                      //determines speed range of double click (see below)
int val=0;                           //button state
int oldVal=0;                    //previous button state
int state=0;                      //state toggled by a double click (buzzer locks on)
int pitch=0;
int sensorRead=0;         //reads from potentiometer
int button=7;                  //input, turns buzzer on while pushed
int green=8;
int mic=9;                       //buzzer
int red=10;

void setup(){
pinMode(button,INPUT);
pinMode(green,OUTPUT);
pinMode(red,OUTPUT);
pinMode(mic,OUTPUT);
}

void loop(){

val=digitalRead(button);                //reads button

if(state==1){                                    //checks state of buzzer
digitalWrite(green,HIGH);             //OUTPUT (1)
sensorRead = analogRead(A0);
pitch = map(sensorRead, 0, 1023, 120, 1410);                 //map pitch
tone(mic, pitch, 10);                                                             //play pitch
if(pitch>1400){                 //if the pitch is too high, it switches the buzzer off unless the button is being pressed
digitalWrite(red,HIGH);
state=0;
}
}else{
digitalWrite(green,LOW);                                   //OUTPUT (0)
digitalWrite(red,LOW);
}
if(val==HIGH){                                                     //if the button is pushed once:
digitalWrite(green,HIGH);                                 //OUTPUT (1)
sensorRead = analogRead(A0);
pitch = map(sensorRead, 0, 1023, 120, 1410);   //map pitch
tone(mic, pitch, 10);                                               //play pitch
if(pitch>1400){                                                        //pitch regulator
digitalWrite(red,HIGH);
state=0;
}
oldVal=val;
delay(15);d
for(count=0; count<30; count++){                 //Loop waits for a second button click, for about 1/2 second
val=digitalRead(button);
if(val==LOW){
oldVal=val;                                                         //records previous value
if(state==0){
digitalWrite(green,LOW);                                //OUTPUT (0)
digitalWrite(red,LOW);
}
}else{
digitalWrite(green,HIGH);                                                     //OUTPUT (1)
sensorRead = analogRead(A0);
pitch = map(sensorRead, 0, 1023, 120, 1410);                 //map pitch
tone(mic, pitch, 10);                                                             //play pitch
if(pitch>1400){                                                                      //pitch regulator
digitalWrite(red,HIGH);
state=0;
}
}
if((val==HIGH)&&(oldVal==LOW)){                                    //checks if button is released and pressed again
oldVal=val;
state=1-state;                                                                      //toggles state of buzzer (lock on/off)
if(state==1){
digitalWrite(green,HIGH);//OUTPUT (1)
sensorRead = analogRead(A0);
pitch = map(sensorRead, 0, 1023, 120, 1410);             //map pitch
tone(mic, pitch, 10);                                                         //play pitch
if(pitch>1400){                                                                  //pitch regulator
digitalWrite(red,HIGH);
state=0;
}
}else{
digitalWrite(green,LOW);                          //OUTPUT (0)
digitalWrite(red,LOW);
}
delay(15);
}else{
delay(15);               //the loop delays for 15ms either way, and is repeated 30 times
}
}//End for loop
}//End if statement (before for loop)
}//End void loop

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s