0

My code stops at the first iteration for the phot_val "if" statement.

/////// 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, but I have no Idea why it stop Edit: Fixed title, and added more code of the functions. So basically it runs tru but when "if(phot_val >= .5){play_song(song1,N);}" becomes true it stops until the song it's played entirety then it continues to read data again.

3 Answers 3

1

You aren't checking the value of the analog pin in the for loop so it blocks everything until the entirety of the song finishes. All you need to do is update the for loop to check the value of the input.

Replace your for loop with something like:

for(isong=0;isong<n;isong++){  
     tone(SPKR,song[isong]);
     delay(500); 
     phot_val = read_analogn(2, nreadings);
     if(phot_val < .5){break;} //or whatever value you want it to stop at
}

This will stop the song when your sensor goes below the threshold

Sign up to request clarification or add additional context in comments.

1 Comment

When using the blocking mode of tone(), delay(500); will disturb the performance of the song considerably.
0

I do not see any for loop , perhaps read_analogn(); is a function you declared which contains a for loop? In that case you should check the function in order to make sure the for loop does not fall into an infinite loop , perhaps place some serial outputs in different parts of the code to see if it reaches that part , print out variables to check if different parts of the code are operating and so on.

Nevertheless it is hard to say what is happening without seeing the for loop / function which is causing the trouble so I suggest you place the functions code and the for loop itself in your question.

2 Comments

Sorry, I got confused making the title, it's a else if, not a for loop.
Im not sure this is the exact problem , but the 2 first else_if's are never reached : if MIC is larger than 30 , the if happens , but the next 2 else_if's can never occur since that would mean MIC<30 and at the same time MIC>= 35 (or 40) . Also, the next 2 else_if's have the same condition but have different setColor calls ?
0

The first else if in your code is at this questionable location:

 if(MIC() >= 30){ setColor(255, 0, 0); }    //Red Color
     else if(MIC() >= 35) {
       // comes here only if the first call of MIC() returns less than 30
       // and the second one returns 35 or more.
       // ???
     }  

This is not an answer, but hard to read as a comment.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.