Triggering sound with input/sensor/button/switch

Hi there,

I'm pretty new to Arduino and coding in general so please forgive my questions, but your help is appreciated. I tried out the sample codes for a button and for playing a melody but am unsure how to combine the 2. Basically - I would like to output a sound when a button is pressed or switch turned on. I'm not trying to generate any kind of complicated sounds - the sound the Arduino can generate is pretty sufficient for what I'm trying to do.

It's simply an idea like pressing a button and a buzzer or bell goes off. I'd also like to apply this somehow to a force sensor when it is touched. There's gotta be some code out there that already exists for this simple act - I'm simply having a hard time finding it. Again - any help or direction is extremely appreciated.

Since you won't be doing anything until the button is pressed and the sound starts playing (assuming you want to ignore any subsequent presses until the sound is done), you can do a fairly simple loop, something like this:

void loop()
{
  if (digitalRead(btn_pin) == LOW)
  {
     playSound();
  }
}

NB: Depending on how you've wired up the button, the low might need to be replaced with high. But that really should be as simple as you can get.

That's funny, I'm also a beginner, but this was the first excersize I tried when my arduino came in. Unfortunately, I didn't save the sketch code once it worked (just moved on =). However, the uCHobby "hello world" sound sketch and a simple de-bouncing sketch combined worked perfectly for me.

Debounce tutorial:

http://www.arduino.cc/en/Tutorial/Debounce

"Hello world" sound tutorial:

http://www.uchobby.com/index.php/2007/11/14/arduino-sound-part-2-hello-world/

NOTE 1: you need to be using a speaker for this example, not a buzzer. Wire the "hot" lead from a miniature speaker to SOUNDOUT_PIN, and put a resister or variable resister in line on the "hot" lead to control volume

NOTE 2: make sure you use a pull-down resistor on the switch.

#define SOUNDOUT_PIN 9
#define BUTTON_PIN 10

int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

unsigned long time = 0;         // the last time the output pin was toggled
unsigned long debounce = 200;   // the debounce time, increase if the output flickers


void setup(void){
//Set the sound out pin to output mode
 pinMode(SOUNDOUT_PIN,OUTPUT);
 pinMode(BUTTON_PIN, INPUT);
}

void loop(void){

  reading = digitalRead(BUTTON_PIN);
  
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    //Set the speaker pin high and delay for 1/2 a cycle of 1KHz, 500uS.

  //Generate sound by toggling the I/O pin High and Low
  //Generate a 1KHz tone. set the pin high for 500uS then
  //low for 500uS to make the period 1ms or 1KHz.

    digitalWrite(SOUNDOUT_PIN,HIGH);
    delayMicroseconds(500);

    //Set the speaker pin low and delay for 1/2 a cycle of 1KHz, 500uS.
    digitalWrite(SOUNDOUT_PIN,LOW);
    delayMicroseconds(500);
    
    time = millis();
    
  }

 previous = reading;

}

I don't have arduino here at the office to test this with, but if my memory works correctly, that should work just fine (it compiles just fine =)

!c