if stops at first iteration

My code stops at the first iteration for the phot_val "for" statement.
Tried reading the data using AnalogRead(2); but It still stops.

ARDUINO 1.8.9

/////// Code loop
void loop() {
  double sound = MIC();                            //Declare variable for obtaining microphone data
  double phot_val;
  int nreadings = 100;
  int song1[N]={CN4, DN4, EN4, FN4, GN4, AN4, BN4, CN5};
  int song2[M]= {RT0, RT0, CN4, DN4, CN4, FN4, EN4, RT0,CN4, DN4, CN4, GN4, FN4, RT0, CN4, CN5, AN4, FN4, EN4,DN4, RT0, AS4, AN4, FN4, GN4, FN4, RT0, RT0};
  phot_val = read_analogn(2,nreadings);
Serial.print("Sound: ");Serial.println(sound);    //Testing purposes, Print out sound/mic value
Serial.print("Light: ");Serial.println(phot_val,5);//Testing purposes, Print out light sensor data
 
  if(phot_val >= .5){play_song(song1,N);} //Stops after first 100???
 
/// Else If statements to change RBG colors depending on sound 
 if(MIC() >= 30){ setColor(255, 0, 0); }                        //Red Color
 else if(MIC() >= 35){setColor(0, 255, 0);}                     //Green Color
  else if(MIC() >= 40){setColor(0, 0 , 255);}                   //Blue Color
    else if(MIC() >=25){setColor(255, 255, 255);}               //White Color
      else if(MIC() >=25){setColor(170, 0, 255);}               //White Color
 else {setColor(0, 0, 0);}

}

/////// Read light sensor function
float read_analogn(int p, int n){
float sum = 0;
float avg;
int i;
float voltage;
float phot_val;
  
for(i=0; i<n; i++)
 {
  phot_val = analogRead(p);
  voltage = phot_val*(5.0/1023.0);
  sum += voltage;
 }
 avg = sum/n;
 return (avg);
  }

/////// Color function for RBG Leds
void setColor(int redV, int greenV, int blueV) {
  analogWrite(redP, redV);          //Red value for RBG
  analogWrite(greenP, greenV);      //Green value for RBG
  analogWrite(blueP, blueV);        //Blue value for RBG
}

/////// Play song function
void play_song(int song[], int n){
  int isong;                     //Define variables
  for(isong=0;isong<n;isong++){  //For loop to play the songs
    tone(SPKR,song[isong]);
    delay(500);                  
    }
  noTone(SPKR);
}

}

I can post more code if needed.

EDIT: It's else if, not for.. sorry
Added more code of the functions

There is no for loop in the code you posted. Makes it difficult to debug.

Steve

I assume that the for loop is in the missing play_song() function and who knows what the value of the N variable is in the function call

I fixed title, I must've miss-typed the title while creating the post.

Some more info,
So basically prints all the data of the inputs, but when "if(phot_val >= .5){play_song(song1,N);}" becomes true it stops reading data until the song it's played entirety then it continues to read data again.

Sorry, for the misleading title. Thanks

An if statement isn't a loop. It doesn't iterate. It isn't supposed to.

And since you haven't posted the play_song function code I can only guess that it's some sort of blocking function. So nothing else can happen until it completes I.e. it's doing exactly what you would expect it to do.

Of course if you had read "How to use this forum - please read" at the top of this and every forum and you had posted the complete program as it recommends we'd be able to tell.

Steve

Runylu:
when "if(phot_val >= .5){play_song(song1,N);}" becomes true it stops reading data until the song it's played entirety then it continues to read data again.

That is what the 'play_song()' function does. It plays all of the notes before it returns.
If you want to do other things WHILE the song is playing you will have to write a function that can be called from loop() to change notes when the desired interval has passed. This will require some variables to keep track of how far into the song you are and when the last note started. Look at the "BlinkWithoutDelay" example for a hint on how to do that.

This might work for you:

const int song1[]={CN4, DN4, EN4, FN4, GN4, AN4, BN4, CN5};
const int song2[]= {RT0, RT0, CN4, DN4, CN4, FN4, EN4, RT0,CN4, DN4, CN4, GN4, FN4, RT0, CN4, CN5, AN4, FN4, EN4,DN4, RT0, AS4, AN4, FN4, GN4, FN4, RT0, RT0};

const byte SpeakerPin = 8;

const int *CurrentSong = NULL;
int CurrentSongIndex = 0;
int CurrentSongLength = 0;
unsigned long CurrentSongNoteStartTime = 0;

/////// PlaySong function
void PlaySong(const int *song, int n)
{
  CurrentSong = song;
  CurrentSongIndex = 0;
  CurrentSongLength = n;
  CurrentSongNoteStartTime = 0;
}

// Call as frequently as possible to continue song playing
void ContinueSong()
{
  unsigned long currentTime = millis();
  if (CurrentSong == NULL)
    return;

  if (currentTime - CurrentSongNoteStartTime >= 500)
  {
    // Time to end the current note
    noTone(SpeakerPin);
    if (CurrentSongIndex < CurrentSongLength)
    {
      int pitch = CurrentSong[CurrentSongIndex++];
      if (pitch > 0)
        tone(SpeakerPin, pitch);
      CurrentSongNoteStartTime = currentTime;
    }
    else
    {
      CurrentSong = NULL; // Reached end of song
    }
  }
}

void setup()
{
  PlaySong(song1, sizeof song1 / sizeof song1[0]);
}

void loop()
{
  ContinueSong();

  if (CurrentSong == NULL)
    PlaySong(song2,  sizeof song2 / sizeof song2[0]);
}