user51936: My robot working fine but when I added buzzer and melody into code then is something wrong. Melody is playing when accelerometer is activated but rest of the code need to wait to end of melody. I want to play melody in background when program works. How to do it? There is my code: 
#include <RedBot.h>
#include <RedBotSoftwareSerial.h>
RedBotAccel accel;
RedBotMotors motors;
int CM;        
long CZAS;     
bool started = false;
const int buzzer = A0;

int shortTone = 80;
int longTone = 200;
int standardDelay = 50;
int delayBetweenBars = 50;

void setup() 
{  
   Serial.begin(9600);
   accel.enableBump();
   accel.setBumpThresh(150);
   pinMode(A2, OUTPUT);  //Trig
   pinMode(A3, INPUT);   //Echo 
   pinMode(buzzer, OUTPUT);  //Buzzer
}


void pomiar_odleglosci()
{
   digitalWrite(A2, HIGH);      
   delayMicroseconds(10);
   digitalWrite(A2, LOW);
   CZAS = pulseIn(A3, HIGH);
   CM = CZAS / 58;                
}

void loop(){



   while (!accel.checkBump() && !started) delay(100);
   started = true;

   if(started = true)
   {   DarudeSandstorm();
   }


   pomiar_odleglosci();               
   Serial.print("Odleglosc: ");      
   Serial.print(CM);
   Serial.println(" cm");

   if(CM>10)                 
   {
      motors.drive(100);
   }
   else     
   {
      motors.drive(-100);
      delay(400);
      motors.stop();

      delay(400);

      motors.rightMotor(100);
      delay(700);
      motors.stop();

      motors.drive(100);
   }

}
//melody


user18565: You are using delay() in many parts of your code, which makes it very trick. I see a couple of possibilities here:
1 - if you have extra time and patience, get rid of all delays and find your around it counting millis() or some similar technique.
2 - if you have some extra bucks, get another controller just to play the melody, an ATtiny85 should be cheap and practical for this.
There is also the super-lazy, almost-no-work-at-all, relatively cheap solution: use a small media/MP3 player module, which also would allow you to have more than one melody and eventually adtiional noises.

51936: <p>My robot working fine but when I added buzzer and melody into code then is something wrong. Melody is playing when accelerometer is activated but rest of the code need to wait to end of melody. I want to play melody in background when program works. How to do it? There is my code: </p>

<pre><code>#include &lt;RedBot.h&gt;
#include &lt;RedBotSoftwareSerial.h&gt;
RedBotAccel accel;
RedBotMotors motors;
int CM;        
long CZAS;     
bool started = false;
const int buzzer = A0;

int shortTone = 80;
int longTone = 200;
int standardDelay = 50;
int delayBetweenBars = 50;

void setup() 
{  
   Serial.begin(9600);
   accel.enableBump();
   accel.setBumpThresh(150);
   pinMode(A2, OUTPUT);  //Trig
   pinMode(A3, INPUT);   //Echo 
   pinMode(buzzer, OUTPUT);  //Buzzer
}


void pomiar_odleglosci()
{
   digitalWrite(A2, HIGH);      
   delayMicroseconds(10);
   digitalWrite(A2, LOW);
   CZAS = pulseIn(A3, HIGH);
   CM = CZAS / 58;                
}

void loop(){



   while (!accel.checkBump() &amp;&amp; !started) delay(100);
   started = true;

   if(started = true)
   {   DarudeSandstorm();
   }


   pomiar_odleglosci();               
   Serial.print("Odleglosc: ");      
   Serial.print(CM);
   Serial.println(" cm");

   if(CM&gt;10)                 
   {
      motors.drive(100);
   }
   else     
   {
      motors.drive(-100);
      delay(400);
      motors.stop();

      delay(400);

      motors.rightMotor(100);
      delay(700);
      motors.stop();

      motors.drive(100);
   }

}
//melody
</code></pre>
 18565: <p>You are using delay() in many parts of your code, which makes it very trick. I see a couple of possibilities here:</p>

<p>1 - if you have extra time and patience, get rid of all delays and find your around it counting millis() or some similar technique.</p>

<p>2 - if you have some extra bucks, get another controller just to play the melody, an ATtiny85 should be cheap and practical for this.</p>

<p>There is also the super-lazy, almost-no-work-at-all, relatively cheap solution: use a small media/MP3 player module, which also would allow you to have more than one melody and eventually adtiional noises.</p>
