How to have a speaker read from multiple inputs

Hi! So I'm creating a small project wherein I have a wooden kitchen that has doors to an oven, washing machine and microwave ect. when the doors are closed, they will press against a button. when they are open, (button is unpressed, low) I want them to trigger a sound corresponding to whatevers being opened. e.g. a ping when microwave opens.

I have one 8ohm speaker and four buttons to activate 4 separate 10 second sounds. thing is, with how i have written this programme (unfinished) will the sounds repeat continuously when the doors are left open? I just want the sound to play once until the door is closed then opened again. and will one speaker with 4 inputs work like this? the sounds are 10 seconds so will i need to put a delay on of sorts to hold it? I can't fully test this out yet as still waiting for some parts but
any advice is welcome please, i'm totally new to programming but know a good thing or two on electrics, thanks!


#include "SD.h"
#define SD_ChipSelectPin 4
#include "TMRpcm.h"
#include "SPI.h"
const int stovebutton1 = 2; 
const int stovebutton2 = 3; 
const int microwavebutton= 4; 
const int washerbutton = 5;
pinMode(stovebutton1, INPUT);//boiling sound
pinMode(stovebutton2, INPUT);//frying sound
pinMode(microwavebutton, INPUT);//ding sound
pinMode(washerbutton, INPUT);//washing sound
TMRpcm tmrpcm;

void setup(){
tmrpcm.speakerPin = 9;
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}
tmrpcm.setVolume(6);
}

void loop(){  }
// read the state of the pushbutton value:
  buttonState = digitalRead(stovebutton1);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == LOW) {
    // turn boiling sound on
    tmrpcm.setVolume(6);
    tmrpcm.play("boiling.wav");
  } 
  buttonState = digitalRead(stovebutton2);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == LOW) {
    // turnfrying sound on
    tmrpcm.setVolume(6);
    tmrpcm.play("frying.wav");
  } 
    
  buttonState = digitalRead(microwavebutton);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == LOW) {
    // turn boiling sound on
    tmrpcm.setVolume(6);
    tmrpcm.play("ding.wav");
  }

I hope you have shielded, twisted pair, wire coming that has very good insulation covering. You are adding stuff in really nasty environments.

Wire the switches between ground and an Arduino pin that your code has defined as input pullup.

Do you have the sound working right now?

Hello tina220800

Do you have experience with programming in C++?

This task can be realised with a scalable button manager easily.
An object contains all the relevant information, such as pinport address, button name and the time information for debouncing the button.
A service evaluates this information and executes the corresponding function.

One shot logic looks like:

read the current state
if the current state is not the same as the old state
{
  old state = current state
  respond to the changed state
}

Hiya, this is a toy wooden kitchen. I didn't make that clear. I currently don't have the sound at all as I'm waiting on a delayed speaker but I know it won't work this way. I have the switches wired up between ground and the corresponding pins.

No I don't, I've only used basic programme functions previously for simple projects.

Did you consider and try the approach in reply #4?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.