Untitled

mail@pastecode.io avatar
unknown
c_cpp
7 months ago
1.2 kB
2
Indexable
Never
#include <SPI.h>     // include Arduino SPI library
#include <SD.h>      // include Arduino SD library
#include "TMRpcm.h"  // include TMRpcm library

#define forceSensorPin A0
#define SD_ChipSelectPin 4
TMRpcm audio;

File file;

void setup() {
  //read SD card
  Serial.begin(9600);
  while (!Serial);
  Serial.print("Initializing SD card...");
  if (!SD.begin(SD_ChipSelectPin)) {
      Serial.println("SD card intializing failed!");
  }
  else {
    Serial.println("SD card intializing succeeded!");
  }

  file = SD.open("snare_converted.wav");

  audio.speakerPin = 9;  // set speaker output to pin 9
  audio.setVolume(7);    //   0 to 7. Set volume level
  audio.quality(0);      //  Set 1 for 2x oversampling Set 0 for normal
}

void playSound() {
  audio.play(file.name());
  Serial.println("Playing file: snare_converted.wav");
}

void loop() {
  //general reading (0 to 1023)
  int reading = (analogRead(forceSensorPin));
  Serial.println(reading);

  bool pre_reading = (reading == 0);
  delay(50);
  bool post_reading = (analogRead(forceSensorPin) == 0);

  if ((pre_reading == true) && (post_reading == false)) {
    Serial.println("THIS SHOULD PLAY A SOUND!!!");
    playSound();
  }
  //delay(50);
}
Leave a Comment