Go Down

Topic: Problem writing code to assign a .wav file on SD card to a button. (Read 48 times) previous topic - next topic

Mar 28, 2015, 10:37 pm Last Edit: Mar 28, 2015, 11:39 pm by pcwizzy16 Reason: Even after I enabled pullups it still has problem
I am new to arduino, I just got the Arduino Due in October. I am trying to modify SimpleAudioPlayer to play 1.wav when I press a pushbutton attached to digital pin 1 or play 2.wav off an SD card when I press button 2.
Can someone help me fix this problem?

here is the code:

Code: [Select]

// modded audio player
#include <SD.h>
#include <SPI.h>
#include <Audio.h>
int buttonPin = 2;
int buttonPin2 = 3;

void setup()
{

  Serial.begin(9600);

  // setup SD-card
  Serial.print("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println(" failed!");
    return;
  }
  Serial.println(" done.");
  // hi-speed SPI transfers
  SPI.setClockDivider(4);
 
 
  Serial.print("set pin 1 mode...");
   pinMode(buttonPin, INPUT_PULLUP);
   Serial.print("Done!");
  Serial.println("set pin 2 mode...");
   pinMode(buttonPin2, INPUT_PULLUP);
  Serial.print(" Done! ");

  // 44100Khz stereo => 88200 sample rate
  // 256 mSec of prebuffering.
  Audio.begin(88200, 256);
  Serial.println("audiobegin");


}

void loop()
{


 
  int count = 0;
  // open wave file from sdcard
 int buttonState = digitalRead(buttonPin) ;
 int buttonState2 = digitalRead(buttonPin2) ;
  File myFile;
 if (buttonState = 1) {
 File myFile = SD.open("1.wav"); Serial.println("button 1 on!"); }
 if (buttonState2 = 1) {
 File myFile = SD.open("2.wav"); Serial.println("button 2 on!"); }
 if (buttonState = 0) {Serial.println("button 1 off!"); }
 if (buttonState2 = 0) {Serial.println("button 2 off!"); }
  if (!myFile) {
    // if the file didn't open, print an error and stop
    Serial.println("error opening wave file!") ;
    while (true);
  }

  const int S = 1200 ; // Number of samples to read in block
  short buffer[S] ;

  Serial.print("Playing");
  // until the file is not finished
  while (myFile.available()) {
    // read from the file into buffer
    myFile.read(buffer, sizeof(buffer));

    // Prepare samples
    int volume = 1024;
    Audio.prepare(buffer, S, volume);
    // Feed samples to audio
    Audio.write(buffer, S);

    // Every 100 block print a '.'
    count++;
    if (count == 100) {
      Serial.print(".");
      count = 0;
    }
  }
  myFile.close();

  Serial.println("End of file. Thank you for listening!");
  while (true) ;
 }




And if this helps this is what i get from the serial console

Code: [Select]

Initializing SD card... done.
set pin 1 mode...Done!set pin 2 mode...
 Done! audiobegin
button 1 on!
button 2 on!
error opening wave file!


I also attached the .ino file.

Thanks for the support.


Pulldown resistors missing?
No I enabled internal pulldowns and even used external pulldown resistors and it does the same thing.

jurs

No I enabled internal pulldowns and even used external pulldown resistors and it does the same thing.
There ain't such a thing like "internal pulldowns", Atmega controllers have "internal pullups".
OK, you edited your posting and the posted code after I answered.

But you didn't correct the comparison:
Code: [Select]

if (buttonState = 1)


The equals sign '=' is an assignment.
You wanted to do most likely a comparison with the double equals '==':
Code: [Select]

if (buttonState == 1)


If you use both, internal pullups and external pulldowns, most likely the external pulldown will win if it is 10K or less. I'd better not use both at the same time, but EITHER external pulldown OR internal pullup resistors.

Ok, I  changed the code to the following (added second equals sign and created a status variable so it will not try to restart the song on each loop,)

Code: [Select]

 ´╗┐// modded audio player
#include <SD.h>
#include <SPI.h>
#include <Audio.h>
int buttonPin = 2;
int buttonPin2 = 3;
int statVar;
void setup()
{

  Serial.begin(9600);

  // setup SD-card
  Serial.print("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println(" failed!");
    return;
  }
  Serial.println(" done.");
  // hi-speed SPI transfers
  SPI.setClockDivider(4);
 
 
  Serial.print("set pin 1 mode...");
   pinMode(buttonPin, INPUT_PULLUP);
   Serial.print("Done!");
  Serial.println("set pin 2 mode...");
   pinMode(buttonPin2, INPUT_PULLUP);
  Serial.print(" Done! ");

  // 44100Khz stereo => 88200 sample rate
  // 256 mSec of prebuffering.
  Audio.begin(88200, 256);
  Serial.println("audiobegin");


}

void loop()
{


 
  int count = 0;
  // open wave file from sdcard
 int buttonState = digitalRead(buttonPin) ;
 int buttonState2 = digitalRead(buttonPin2) ;
  File myFile;
 if (statVar == 0) {
 if (buttonState == 0) {
 File myFile = SD.open("1.wav"); Serial.println("button 1 on!"); statVar = 1; }}
  if (statVar == 0) {
 if (buttonState2 == 0) {
 File myFile = SD.open("2.wav"); Serial.println("button 2 on!"); statVar = 1; }}
 if (buttonState = 0) {Serial.println("button 1 off!"); }
 if (buttonState2 = 0) {Serial.println("button 2 off!"); }
 
  if (!myFile) {
    // if the file didn't open, print an error and stop
    Serial.println("error opening wave file!") ;
    while (true);
  }

  const int S = 1200 ; // Number of samples to read in block
  short buffer[S] ;

  Serial.print("Playing");
  // until the file is not finished
  while (myFile.available()) {
    // read from the file into buffer
    myFile.read(buffer, sizeof(buffer));

    // Prepare samples
    int volume = 1024;
    Audio.prepare(buffer, S, volume);
    // Feed samples to audio
    Audio.write(buffer, S);

    // Every 100 block print a '.'
    count++;
    if (count == 100) {
      Serial.print(".");
      count = 0;
    }
  }
  myFile.close();

  Serial.println("End of file. Thank you for listening!");
  while (true) ;
 }


but now when I start up the arduino while pressing button 1, the serial monitor says this:
Code: [Select]

 ´╗┐Initializing SD card... done.
set pin 1 mode...Done!set pin 2 mode...
 Done! audiobegin
button 1 on!
error opening wave file!


Now what do I do?

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy