My code stops at the first iteration for the phot_val "if" statement.
(我的代码在phot_val“ if”语句的第一次迭代中停止。)
/////// 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. (因此,基本上它运行tru,但是当“ if(phot_val> = .5){play_song(song1,N);}”变为真时,它将停止直到其完整播放为止,然后继续读取数据。)
ask by Runylu translate from so