Simple SD Audio and AltSoftSerial

Is there a possibility that these libraries work together? Sadly, I need AltSoftSerial but if there are another library that work together, i accept then.

I run a robot. There are many sensors and i command robot with bluetooth. Sensors can work together and the phone is receiving all reactions. The problem is when I adding the SD Audio codes, Bluetooth don’t work. There is another library like TMRPCM, but there will be multiple definition errors.

How can i fix this? This two libraries must be work together or i need another library. Can i change the Simple SD Audio library? But how? Do I need to change pins? This is so important for me.

There are some parts of codes;

"#include <AltSoftSerial.h>
AltSoftSerial BTserial;

#include <SimpleSDAudio.h>"

“int engel_oku1;
int engel_oku2;
int engel_oku3;”

"{

SdPlay.setSDCSPin(4);

if (!SdPlay.init(SSDA_MODE_FULLRATE | SSDA_MODE_MONO | SSDA_MODE_AUTOWORKER))

{ while(1); }

if(!SdPlay.setFile(“1.wav”))

{ while(1); }
}"

“{engel_oku2 = digitalRead(24);
if (engel_oku2 == LOW) {SdPlay.play();BTserial.print(”<L,1,0>" );BTserial.print("<L,2,1>" );BTserial.print("<L,3,0>" );BTserial.print("<L,4,0>" );
digitalWrite(23,LOW);digitalWrite(25, HIGH);digitalWrite(27, HIGH);digitalWrite(29, LOW);sistem_test();dur();BTserial.print("<L,2,0>" );}

}"

There are some “While” commands. Is problem occuring from this? Sorry for my English…

Can you please post your whole code using code tags? See this thread if you're not sure about how to do that. Also use CTRL + T to auto format your code to make it easier to see.
Which Arduino board are you using?
Was the robot working correctly before you added the SD Audio?

#include <AltSoftSerial.h>
AltSoftSerial BTserial;

#include <SimpleSDAudio.h>

#include <ServoTimer2.h>
int aci=90,i;
ServoTimer2 myservo; 
int gecikme=40;

// Variables used for incoming data
const byte maxDataLength = 20;
char receivedChars[21] ;
boolean newData = false;

// Pins de HC-SR04
const int trigger_sol = 33;
const int echo_sol = 35;

// Pins de HC-SR04
const int trigger_sag = 37;
const int echo_sag = 39;

// Constants for hardware
const byte m_PIN[] = {23,25,27,29};
boolean m_State[] = {false,false,false,false};

const int on_sol = 22;
const int on_orta = 24;
const int on_sag =  26;

const int hareket_sensoru_sol = 38;
const int hareket_sensoru_sag = 40;

int durum;
int engel_oku1;
int engel_oku2;
int engel_oku3;

void setup() {

   Serial.begin(9600);
    myservo.attach(10);
  // Configuración de los pines
    pinMode(trigger_sol, OUTPUT);
    pinMode(echo_sol, INPUT);


   pinMode(trigger_sag, OUTPUT);
   pinMode(echo_sag, INPUT);


  //Engel switchler pull-up olacak


    for (byte pin = 0; pin < 4; pin++) 
    {
         
         
       // Set the LED pins for output and make them LOW
       pinMode(m_PIN[pin], OUTPUT);  digitalWrite(m_PIN[pin],LOW);
    }
    
    pinMode(22,INPUT_PULLUP); // on_sol
    pinMode(24,INPUT_PULLUP); //on_orta
    pinMode(26,INPUT_PULLUP); 


    pinMode(38,INPUT);
    pinMode(40,INPUT);
    
    
  
    

 kafa_test();
 ileri_git(); delay(1000);dur();
 geri_git(); delay(200);dur();
 sola_don(); delay(200);dur();
 saga_don(); delay(200);dur();

 // open serial communication for debugging
    Serial.begin(9600);
    Serial.print("Sketch:   ");   Serial.println(__FILE__);
    Serial.print("Uploaded: ");   Serial.println(__DATE__);
    Serial.println(" ");
     
    //  open software serial connection to the Bluetooth module.
    BTserial.begin(9600); 
    Serial.println("AltSoftSerial started at 9600"); 
          
    newData = false;
{

SdPlay.setSDCSPin(4);

if (!SdPlay.init(SSDA_MODE_FULLRATE | SSDA_MODE_MONO | SSDA_MODE_AUTOWORKER))

{ while(1); }

if(!SdPlay.setFile("1.wav"))

{ while(1); }
}
}
//***********************************************************
//***********************************************************
//***********************************************************
//***********************************************************

void loop() {

   
    
    
   {engel_oku1 = digitalRead(22);

   if (engel_oku1 == LOW) {BTserial.print("<L,1,0>" );BTserial.print("<L,2,0>" );BTserial.print("<L,3,1>" );BTserial.print("<L,4,0>" ); 
   digitalWrite(23,LOW);digitalWrite(25, HIGH);digitalWrite(27, LOW);digitalWrite(29, HIGH);sistem_test();dur();BTserial.print("<L,3,0>" );}

   

   }
   
    {engel_oku2 = digitalRead(24);
   if (engel_oku2 == LOW) {SdPlay.play();BTserial.print("<L,1,0>" );BTserial.print("<L,2,1>" );BTserial.print("<L,3,0>" );BTserial.print("<L,4,0>" ); 
   digitalWrite(23,LOW);digitalWrite(25, HIGH);digitalWrite(27, HIGH);digitalWrite(29, LOW);sistem_test();dur();BTserial.print("<L,2,0>" );}

      
      }
   
    {engel_oku3 = digitalRead(26);
  
   if (engel_oku3 == LOW) {BTserial.print("<L,1,0>" );BTserial.print("<L,2,0>" );BTserial.print("<L,3,0>" );BTserial.print("<L,4,1>" ); 
   digitalWrite(23,HIGH);digitalWrite(25, LOW);digitalWrite(27,HIGH);digitalWrite(29, LOW);sistem_test();dur();BTserial.print("<L,4,0>" );}

    }
         
    recvWithStartEndMarkers();                // check to see if we have received any new commands
    if (newData)  {   processCommand();  }    // if we have a new command do something about it

   { Serial.println(" Hareket Kontrol Ediliyor ");
int buttonState_sol = digitalRead(38);
int buttonState_sag = digitalRead(40);

if (buttonState_sol == LOW) { Serial.println(" SOLDA ENGEL ");
BTserial.print("<L,1,0>" );BTserial.print("<L,3,1>");BTserial.print("<L,2,0>" );BTserial.print("<L,4,0>" ); 
   digitalWrite(25, HIGH);digitalWrite(29, HIGH);digitalWrite(23, LOW);digitalWrite(27, LOW);delay(2000);sistem_test();dur();BTserial.print("<L,3,0>");}

else if(buttonState_sag == LOW)

{ Serial.println(" SAGDA ENGEL ");
BTserial.print("<L,2,0>" );BTserial.print("<L,4,1>" );BTserial.print("<L,1,0>" );BTserial.print("<L,3,0>"); 
   digitalWrite(23, HIGH);digitalWrite(27, HIGH);digitalWrite(25, LOW);digitalWrite(29, LOW);delay(2000);sistem_test();dur();BTserial.print("<L,4,0>");}
   }


}


//***********************************************************
//***********************************************************
//***********************************************************
//***********************************************************


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++


void sola_don()   // sola dön
{
digitalWrite(23, HIGH);digitalWrite(27, HIGH); delay(100);Serial.println("     SOLA donuyorum    "); durum=1;
}

void saga_don()
{
digitalWrite(25, HIGH);digitalWrite(29, HIGH); delay(100); Serial.println("     SAGA donuyorum    ");durum=1;
}


void ileri_git()  // geri git
{
digitalWrite(23, HIGH);digitalWrite(29, HIGH); delay(100);Serial.println("     ilerliyorum    ");durum=1;
}

void geri_git() // ileri olacak
{
digitalWrite(25, HIGH);digitalWrite(27, HIGH); delay(100);Serial.println("     GERi Gidiyorum    ");durum=1;
}

void dur()
{
    digitalWrite(23, LOW);
    digitalWrite(25, LOW);
    digitalWrite(27, LOW);
    digitalWrite(29, LOW);
    delay(500); Serial.println("     DURDUM    ");
}
//*******************************************************//


void kafa_test()
{
  
  for(i=1;i<=15;i=i+1)
  {
  aci=aci+5;
  myservo.write(aci); 
    // sets the servo position according to the scaled value
  delay(gecikme);   
  }
  Serial.print("1= "); Serial.println(aci); delay(500);
  
    for(i=1;i<=30;i=i+1)
  {
  aci=aci-5;
  myservo.write(aci);  
  delay(gecikme);   
  }
Serial.print("2= "); Serial.println(aci); delay(500);
    for(i=1;i<=15;i=i+1)
  {
    aci=aci+5;
  
  myservo.write(aci);   
   // sets the servo position according to the scaled value
  delay(gecikme);   
  }

Serial.print("3: "); Serial.println(aci); delay(500);
  delay(2000); 
   
  
}

void processCommand()
{
     Serial.print("receivedChars = ");   Serial.println(receivedChars);

    if (receivedChars[0] == 'L')      // do we have a LED command?
    {
        // we know the LED command has a fixed length "L10"
        // and the value at pos 1 is the LED and the value at pos 2 is 0 or 1 (on/off). 
        // 0 and 1 is the same as LOW and HIGH so we can use 0/1 instead of LOW/HIGH
        
        byte mnum = receivedChars[1] - 48;          // convert ascii to value by subtracting 48
        boolean mstatus = receivedChars[2] - 48;
        
        digitalWrite(m_PIN[mnum-1],mstatus);
        m_State[mnum-1] = mstatus;
    }

    receivedChars[0] = '\0';
    newData = false;
}


void sistem_test()
{
  
  for(i=1;i<=5;i=i+1)
  {
  aci=aci+5;
  myservo.write(aci); 
    // sets the servo position according to the scaled value
  delay(gecikme);   
  }
  Serial.print("test1= "); Serial.println(aci); delay(500);
  
    for(i=1;i<=5;i=i+1)
  {
  aci=aci-5;
  myservo.write(aci);  
  delay(gecikme);   
  }
Serial.print("test2= "); Serial.println(aci); delay(500);
  
}

void recvWithStartEndMarkers()
{
     static boolean recvInProgress = false;
     static byte ndx = 0;
     char startMarker = '<';
     char endMarker = '>';
     char rc;
 
     if (BTserial.available() > 0) 
     {
          rc = BTserial.read();
          if (recvInProgress == true) 
          {
               if (rc != endMarker) 
               {
                    receivedChars[ndx] = rc;
                    ndx++;
                    if (ndx > maxDataLength) { ndx = maxDataLength; }
               }
               else 
               {
                     receivedChars[ndx] = '\0'; // terminate the string
                     recvInProgress = false;
                     ndx = 0;
                     newData = true;
               }
          }
          else if (rc == startMarker) { recvInProgress = true; }
     }
}

Trigger and Echo can be ignored, loop codes aren’t on this page. The main problem is Simple SD Audio and AltSoftSerial Library confusion. If I can do this, i will write. :slight_smile:

By the way, can i use Bluetooth without AltSoftSerial? When I erase that part;

#include <AltSoftSerial.h>
AltSoftSerial BTserial;”

then codes aren’t compiling (BTserial is important for this code and this does not work by alone) When I use the SoftWare Serial then codes are compiling but this time Bluetooth doesn’t work. How can i fix this? My Bluetooth Android Program was created by AppInvertor. Is AltSoftSerial necessary for AppInvertor Apps?

The SoftWare Serial did work. When I add this codes;

"{
tmrpcm.speakerPin = 11;
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
}
}

"

Bluetooth doesn't work. Then erase just this part, everything is ok for bluetooth.

Can main problem be Serial.begin? (9600) How can i fix this? Now, the "only" problem is just this.