Hi guys, i've been struggling with a problem in my code. What i want to do is basicaly an on/off method that start/stop the loop. From what i find, i need to put a button that is connected to a input pin and check if the button is pressed or not at the beginning of the loop, but the thing is that i want to turn on/off the program any time while the program is running, but i'm not sure how can i implement that any other way than by simply checking the state of the button over and over again in the loop. Maybe someone can help me with a much simpler way to solve this.
int led[3] = { 3, 4, 5}; // Assign the pins for the leds
int Audio = A0;
int s[3],sm,i,Vth;
void setup() {
for (i = 0; i < 3; i++)
pinMode(led[i], OUTPUT);
}
void loop() {
for(i = i; i < 3; i++) {
digitalWrite(led[i], LOW);}
retry:
long interval = 5000;
long currentMillis = 0;
for(i=1;i<=3;i++){
s[i] = analogRead(Audio);
currentmillis=millis();
while(s[i]<Vth){
s[i] = analogRead(Audio);
if(millis()-currentmillis>interval)
goto retry;
}
Serial.println(s[i]);
}
sm=(s[1]+s[2]+s[3])/3;
sm = sm / 85; //Change the sensitivity by changing Denominator
if (sm == 0) {
for(i = 0; i < 3; i++) {
digitalWrite(led[i], LOW);
}
}
else {
for (i = 0; i < sm; i++) {
digitalWrite(led[i], HIGH);
delay(40);
}
for(i = i; i < 3; i++) {
digitalWrite(led[i], LOW);
}
}
delay(3000);
}