Audio Lopping Glitch

Hello! I am new to Arduino (no coding knowledge yet except simple commands), and I needed some help with a code. I am making a machine that lets me know when my laundry basket is full by a weight sensor, among other things. I am having the code turn on a light (which I have already figured out) and play a sound. The issue is ( I think) that the audio is playing every time the code loops. It is only playing a little bit of the file. When the scale senses the weight to be above x, it will play the entire sound file but the main issue is the looping bit. I will include the code below (I apologize that the code is muddled and I know that you are not usually supposed to do multiple things.) Thanks for everything!

code
#include <SD.h> // need to include the SD library
#define SD_ChipSelectPin 10 //connect pin 4 of arduino to cs pin of sd card
#include <TMRpcm.h> //Arduino library for asynchronous playback of PCM/WAV files
#include <SPI.h> // need to include the SPI library
#include “HX711.h”
#define calibration_factor 11760 //This value is obtained using the SparkFun_HX711_Calibration sketch
#define LOADCELL_DOUT_PIN 18
#define LOADCELL_SCK_PIN 19
int LEDL = 16;
HX711 scale;
#include <LiquidCrystal.h>
#include “DHT.h”
#define DHTPIN 8
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
TMRpcm tmrpcm; // create an object for use in this sketch
int temp = 1;
unsigned long previousMillis1 = 0;
const long interval1 = 86400000;
unsigned long previousMillis2 = 0;
const long interval2 = 86400000;
int LED = 17;
int reed_switch1 = 15;
int reed_switch2 = 14;
int reed_status1;
int reed_status2;
int timer = 0;
bool state = 0;
void setup()

{
pinMode(LEDL, OUTPUT);
Serial.begin(9600);
Serial.println(“HX711 scale demo”);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
Serial.println(“Readings:”);
lcd.begin(16, 2);
dht.begin();
tmrpcm.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) // returns 1 if the card is present
{
Serial.println(“SD fail”);
return;
}{
pinMode(LED, OUTPUT);
pinMode(reed_switch1, INPUT);
pinMode(reed_switch2, INPUT);
}

}

void loop()

{

Serial.print(“Reading: “);
Serial.print(scale.get_units(), 1); //scale.get_units() returns a float
Serial.print(” lbs”); //You can change this to kg but you’ll need to refactor the calibration_factor
Serial.println();
{ if (scale.get_units() < 5.0) {
digitalWrite(LEDL, LOW);
tmrpcm.play(“basket.wav”);

}

else {
  digitalWrite (LEDL, HIGH);


}{

  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // read humidity
  float h = dht.readHumidity();
  //read temperature in Fahrenheit
  float f = dht.readTemperature(true);

  if (isnan(h) || isnan(f)) {
    lcd.print("ERROR");
    return;
  }{
    lcd.setCursor(0, 0);
    lcd.print("F:");
    lcd.print(f);
    lcd.setCursor(9, 0);
    lcd.print("H:");
    lcd.print(h);
  }
}

unsigned long currentMillis1 = millis();
reed_status1 = digitalRead(reed_switch1);


// if the LED is off turn it on and vice-versa:

if (reed_status1 == 0)  {
  while (currentMillis1 - previousMillis1 >= interval1) {
    previousMillis1 = currentMillis1;
    digitalWrite(LED, HIGH);
    
  }
}    else  {
  if  (reed_status1 == 1)
    digitalWrite(LED, LOW);
    tmrpcm.play("drawer.wav");
}

}
}

Hello
please publish your sketch by using the code tags </> as above shown in this editor.

Your post has been moved to a more suitable section of the forum.

https://create.arduino.cc/editor/anlambdy/c7bc6f95-aeaf-453e-a5b0-9631a8fd2366/preview

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