No Errors but still not running

i have the following code which will play a random tune in Buzzer, but i don’t know why my Redboard Uno doesn’t do anything!

Code:

const int buzzerPin = 9;


int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};



int tempo = 150;

char song[] = {'c', 'd',' ', 'e', 'f',' ', 'g', 'a', 'b', 'C',' '};
const byte songLength = sizeof(song) / sizeof(song[0]);
char notes[31]; 


void setup() 
{
  pinMode(buzzerPin, OUTPUT);
  pinMode (13,OUTPUT);
  Serial.begin(9600);
  





}


void loop() 
{


char song[] = {'c', 'd',' ', 'e', 'f',' ', 'g', 'a', 'b', 'C',' '};
char notes[100];
int lengthh=20;

int i;

for(i = 0;i <= lengthh; i++){
  
randomSeed(analogRead(0));

 notes[i]=song[rand() % 10 + 1];
}
 
 
 int duration;
  
  for (i = 0; i < lengthh; i++) 
  {
    duration = beats[i] * tempo;  
    
    if (notes[i] == ' ')         
    {
      delay(duration);            
    }
    else                          
    {
      tone(buzzerPin, frequency(notes[i]), duration);
      delay(duration);          
    }
    delay(tempo/10);            
  }
  


}





int frequency(char note) 
{
  
  
  int i;
  const int numNotes = 8;  
  


  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
  

  
  for (i = 0; i < numNotes; i++)  
  {
    if (names[i] == note)         
    {
      return(frequencies[i]);     
    }
  }
  
  return(0);

Everything in your loop has been remarked out except

char song[] = {'c', 'd',' ', 'e', 'f',' ', 'g', 'a', 'b', 'C',' '};
char notes[100];
int lengthh=20;

int i;

for(i = 0;i <= lengthh; i++){
  
 // randomSeed(analogRead(0));

 notes[i]=song[rand() % 10 + 1];

So all you are doing is defining an array. It’s probably doing that much but there’s no output for you to see.

Your code would be a lot easier to read if you removed all the bits you've commented-out.

KenF:
Everything in your loop has been remarked out except

char song[] = {'c', 'd',' ', 'e', 'f',' ', 'g', 'a', 'b', 'C',' '};

char notes[100];
int lengthh=20;

int i;

for(i = 0;i <= lengthh; i++){
 
// randomSeed(analogRead(0));

notes[i]=song[rand() % 10 + 1];




So all you are doing is defining an array. It's probably doing that much but there's no output for you to see.

Thanks, Its working, but now its only running for 10 seconds then it will automatically stop, then again after 10 seconds again start playing BUT this time playa a single tune ( e ) again stop, again start again hangs at ( g ) and also it plays same tune every run! no randomness!

I am using randomSeed(analogRead(0)); ( i have uncommented it now but still same results every time!)

AWOL: Your code would be a lot easier to read if you removed all the bits you've commented-out.

Removed comments :)

I have written the following code which INFINITELY plays a random tune in buzzer:

But its working like this:

STARTED
TUNE PLAY STARED
PLAYED TUNE “bbce - - bcce -gbe”
(Stuck here Playing tune e for 5 seconds! then tune d for 8 seconds then tune f for 20 seconds PAUSED for 30 seconds tune b for 30 seconds etc. etc.)
AUTOMATICALLY STOPPED!

Why this is happening

Code:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
  
const int buzzerPin = 9;



int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};



int tempo = 150;

char song[] = {'c', 'd',' ', 'e', 'f',' ', 'g', 'a', 'b', 'C',' '};
const byte songLength = sizeof(song) / sizeof(song[0]);
char notes[31];  //allow 1 extra for the NULL


void setup() 
{
  pinMode(buzzerPin, OUTPUT);
  pinMode (13,OUTPUT);
  Serial.begin(9600);
  
    lcd.begin(16, 2);


  lcd.clear();

 





}


void loop() 
{
    lcd.setCursor(0,1);



char song[] = {'c', 'd',' ', 'e', 'f',' ', 'g', 'a', 'b', 'C',' ','c', 'd',' ', 'e', 'f',' ', 'g', 'a', 'b', 'C',' ','c', 'd',' ', 'e', 'f',' ', 'g', 'a', 'b', 'C',' ','c', 'd',' ', 'e', 'f',' ', 'g', 'a', 'b', 'C',' ','c', 'd',' ', 'e', 'f',' ', 'g', 'a', 'b', 'C',' ','c', 'd',' ', 'e', 'f',' ', 'g', 'a', 'b', 'C',' '};



char notes[100];
int lengthh=30;
const int songLength = lengthh;

int i;

for(i = 0;i <= lengthh; i++){
  

 notes[i]=song[rand() % 30 + 1];
}
 
 
 int duration;
  int j=0;
  for (i = 0; i < lengthh; i++)
  {
    j++;
    if(j>13){
        lcd.clear();
          lcd.print(notes[i]);
          j=0;

    }else{
      
          lcd.print(notes[i]);
    }
    duration = beats[i] * tempo;  
    
    if (notes[i] == ' ')       
    {
      delay(duration);         

          lcd.print("-");
    }
    else                          
    {
      tone(buzzerPin, frequency(notes[i]), duration);
      delay(duration);            
    }
    delay(tempo/10);             
  }
  


}

int frequency(char note) 
{

  int i;
  const int numNotes = 8; 
  


  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
 
  
  for (i = 0; i < numNotes; i++)  
  {
    if (names[i] == note)         
    {
      return(frequencies[i]);     
    }
  }
            lcd.print("*");

  return(0); 
}

Not sure why its not looping, but do you realize you can just use a string:

char * song = "cd ef gabC cd ef gabC cd ef gabC cd ef gabC cd ef gabC cd ef gabC " ;

Don't cross post, it really, really pisses me off.

The biggest problem I see is that you are using 20 entries from an array of 'beats' that only contains 18 entries. If the two bogus values you fetch are large they will cause either a long delay or long note.

You are picking random indexes in 'song' from 1 to 10 which leaves out element 0 ('c') so that will never be used. It will work but is a waste.

MarkT: Not sure why its not looping, but do you realize you can just use a string:

char * song = "cd ef gabC cd ef gabC cd ef gabC cd ef gabC cd ef gabC cd ef gabC " ;

I need random string

I need random string

No, you don't. You need a random (or fixed, you decide) number of notes. You COULD get a random note each time it is time to play one. You do NOT need to store the random collection of notes before playing any of them.

Even if you did, that's trivial.