Hello,
I am very new to Arduino so please bear that in mind.
I am essentially looking at making a sci fi blaster that does the following.
Trigger that when pressed lights an LED and plays a sound file.
Twist Dial that when turned lights 2 red LEDs and plays a sound file.
On / off button that turns whole unit on and off.
When module is turned on I want a green LED to turn on and a sound file to play.
Currently i have made this up of the following:
- Arduino Nano
- 9v Battery
- SD card Reader
- Trigger Switch
- Main power switch
- 2 x red LEDS
- 1 x green LED
- 1 x white LED
- PAM8403 Amplifier
- Potentiometer
- a bunch of resisters.
Anyway my issue at the moment is that i cant work out how to get the sound to play correctly when the trigger is pressed.
I seem to be able to get the sound file to play on release of the button. However obviously with a trigger I need the sound to play when the trigger is pressed not when it is released.
When I tried flipping the trigger status around I can then get the sound to play on press of the button however it stops if the button is released.
I just need the sound to play when the button is pressed but then to play out regardless of weather or not the button has been released. Each time the button is pressed a new sound file should interrupt the old one so that you can fire multiple shots.
Any help on this would be much appreciated.
See attached an image of the setup and the code atm.
// Pin Definitions
#define LEDW_PIN 3
#define LEDR_PIN 4
#define MICROSWITCH_PIN_COM 2
#define TRIMMER_PIN_SIG A4
#define SpeakerPin 9
#include "SD.h"
#define SD_ChipSelectPin 10
#include "TMRpcm.h"
#include "SPI.h"
TMRpcm tmrpcm;
void setup()
{
// put your setup code here, to run once:
pinMode(LEDW_PIN, OUTPUT);
pinMode(LEDW_PIN, OUTPUT);
pinMode(LEDR_PIN, OUTPUT);
pinMode(MICROSWITCH_PIN_COM, INPUT);
pinMode(TRIMMER_PIN_SIG, INPUT);
tmrpcm.speakerPin=9;
Serial.begin(9600);
if(!SD.begin(SD_ChipSelectPin))
{
Serial.println("SD fail");
return;
}
tmrpcm.setVolume(5);
tmrpcm.play("Onv3.wav");
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(MICROSWITCH_PIN_COM) == HIGH) {
digitalWrite(LEDW_PIN, HIGH);
tmrpcm.setVolume(5);
tmrpcm.play("DL1v4.wav");
}
else {
digitalWrite(LEDW_PIN, LOW);
}
int potentiometerValue = analogRead(TRIMMER_PIN_SIG);
int brightness = potentiometerValue / 4;
analogWrite(LEDR_PIN, brightness);
}