arduino counter

hi im new with arduino and my current project is to light a LED with sound detector. im using a digital sound detector and arduino uno.

this is my code

int sound1=0;
int counter=0;

void setup()
{
Serial.begin (9600);
pinMode(2,INPUT);
pinMode(3,OUTPUT);
}

void loop()
{
sound1=digitalRead(2);
if (sound1==0)
{
Serial.println(“sound detected”);
digitalWrite(3,HIGH);
delay (750);
}
else
{
Serial.println(“no sound detected”);
digitalWrite(3,LOW);
counter = 0;
}
delay(250);
}

but i want the LED to light up after it triggers a 3 “sound detected” notice on serial monitor. like this on on the attachment

can somebody please help me. i think it is a counter but i don’t know how to do that on my code. thanks in advance

An important part of the solution has already been done for you:

counter = 0;

You have to increment this counter when sound is detected. You also have to switch a led depending on the value the variable counter.

yes i think it was the counter++; i tried it for so many times but sometimes it works and sometimes it doesn't

negative004: yes i think it was the counter++; i tried it for so many times but sometimes it works and sometimes it doesn't

Post what you tried

UKHeliBob: Post what you tried

...in code tags

negative004: yes i think it was the counter++; i tried it for so many times but sometimes it works and sometimes it doesn't

Not bad. However, just as important as important as finding the right statement is finding the right place to put it in.

int sound1=0; int counter=0;

void setup() { Serial.begin (9600); pinMode(2,INPUT); pinMode(3,OUTPUT); }

void loop() { sound1=digitalRead(2); if (sound1==0) { Serial.println(counter++); delay(1000);

if (counter==3) { Serial.println(counter++); digitalWrite(3,HIGH); delay (5000); } else { counter++; } } else { Serial.println("no sound detected"); digitalWrite(3,LOW); counter = 0; } }

i've tried this one but it is still not working as i want it to be.. can you guys give me advice please that will be a great help thanks in advance

You appear still to be struggling with code tags. Try the icon marked </> in the post editor to see what an improvement it makes. You get a nice box like:

 // sample code
 counter++;
 int sound1=0;
int counter=0;

void setup() 
{
 Serial.begin (9600);
 pinMode(2,INPUT);
 pinMode(3,OUTPUT); 
}

void loop() 
{
sound1=digitalRead(2);
if (sound1==0)
{ 
 Serial.println(counter++);
 delay(1000);
 
 if (counter==3)
 {
   Serial.println(counter++);
   digitalWrite(3,HIGH);
   delay (5000); 
 }
 else
 {
   counter++;
 }
}
else
{
 Serial.println("no sound detected");
 digitalWrite(3,LOW);
 counter = 0;
}
}
 sound1 = digitalRead(2);  //test for sound  
  if (sound1 == 0)    //if there is currently a sound
  {
    Serial.println(counter++);  //print the value of the counter then increment it
    delay(1000);  //wait 1 second
    if (counter == 3)  //if the counter equals 3
    {
      Serial.println(counter++);  //print the value of the counter then increment it (why increment it ?)
      digitalWrite(3, HIGH);  //make pin 3 HIGH
      delay (5000);  //wait 5 seconds
    }
    else  //else if counter does not equal 3
    {
      counter++;  //increment it (again)
    }
  }

You are incrementing counter when you detect a sound, when it equals 3 and when it does not equal 3.

Once you have established that it equals 3 you should take the required actions and set counter back to zero otherwise its value will continue to be incremented and it will take a long while until it equals 3 again.

Incidentally, counter and other variables that will never be larger than 255 should really be declared as byte rather than int to avoid wasting memory.

Try printing counter after each time you use it and you should see what is going on.

hello sir i've tried the code that you gave me. thank you very much but i still have an issue regarding the sensor input. because when i tried to make sound and stop at the middle of the counter like if i have 5 counters and it starts with 0, 2 and stops the sound before 4, the input on the serial monitor stays there. is it possible to make the counter go back at 0, whenever the counter doesn't reach the fifth counter. thank you so much for helping me.

int sound1= 0;
int counter = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(2,INPUT);
  pinMode(3,OUTPUT);
}

void loop()
{
 sound1 = digitalRead(2);  //test for sound  
  if (sound1 == 0)    //if there is currently a sound
  {
    Serial.println(counter++);  //print the value of the counter then increment it
    delay(1000);  //wait 1 second
    if (counter == 5)  //if the counter equals 3
    {
      Serial.println("sound detected");  //print the value of the counter then increment it (why increment it ?)
      digitalWrite(3, HIGH);  //make pin 3 HIGH
      delay (5000);  //wait 5 seconds
    }
    else  //else if counter does not equal 3
    {
      counter++;  //increment it (again)
    }
  }
  else if (counter>5)
  {
    counter = 0; 
  }
  else
  {
    digitalWrite(3,LOW);
  }
}

like if the counter doesn't counter up for 1 second or 2 seconds(time duration) the counter will go back to 0. is that possible? thank you so much

negative004: like if the counter doesn't counter up for 1 second or 2 seconds(time duration) the counter will go back to 0. is that possible? thank you so much

Your problem will now be the delay statements in your code. To set a timer, you normally do something like:

unsigned long myTimer = millis() ;
. . .
. . .
if ( millis() - myTimer > 1567 ) {  // do something if 1567 mS has elapsed
    counter = 0 ;  // reset counter
}

However, you can't do that so nicely if you have delay statments.

hi sir i saw a millis() code on others and tried to put it in my program. am i still missing something i just put the soundalarmtime*3. i don’t know why but i just did that.

byte sound1= 0;
byte counter = 0;
unsigned long lastsounddetecttime;
int soundalarmtime = 500;
void setup()
{
  Serial.begin(9600);
  pinMode(2,INPUT);
  pinMode(3,OUTPUT);
}

void loop()
{
 sound1 = digitalRead(2);  //test for sound  
  if (sound1 == 0)    //if there is currently a sound
  {
    lastsounddetecttime = millis();
    Serial.println(counter++);  //print the value of the counter then increment it
    delay(1000);  //wait 1 second
    if (counter == 7)  //if the counter equals 3
    {
      Serial.println("sound detected");  //print the value of the counter then increment it (why increment it ?)
      digitalWrite(3, HIGH);  //make pin 3 HIGH
      delay (5000);  //wait 5 seconds
    }
    else  //else if counter does not equal 3
    {
      counter++;  //increment it (again)
    }
  }
  else if ( (millis()-lastsounddetecttime) > (soundalarmtime*3))
  {
    Serial.println("no sound detected");
    counter = 0; 
  }
  else
  {
    digitalWrite(3,LOW);
  }
}

You did something and you don’t know why? That’s not encouraging.