Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

if-statement - 否则,如果在第一次迭代时停止[Arduino](else if stops at first iteration [Arduino])

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

(您无需在for循环中检查模拟引脚的值,因此它会阻塞所有内容,直到整个歌曲播放完毕。)

All you need to do is update the for loop to check the value of the input.

(您需要做的就是更新for循环以检查输入的值。)

Replace your for loop with something like:

(用以下内容替换您的for循环:)

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

(当您的传感器低于阈值时,这将停止播放歌曲)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...