1

I've tried many codes for this to work but anything isn't helping at all, so my code should work like this: If a sound sensor (3 pins, A2) sends 5 signals into the Arduino within 10 seconds (the time gap of every signal varies, but doesn't matter as long as it sends 5 signals), the LED (13) will light up. This is my recent code if it could help

int led=13;
int sensor=A2;

void setup()
{ 
   pinMode(led, OUTPUT);
    pinMode(sensor, INPUT);
    Serial.begin(9600);
}

void loop()
{
    unsigned long StartMillis = millis();
    unsigned long CurrentMillis = millis();
    int signal_count=digitalRead(sensor);
    int signal_limit=5;
    Serial.println(signal_count);

    while ((CurrentMillis - StartMillis)==5000)
    if (digitalRead(sensor)=0)
    {     signal_count ++;
        Serial.println(signal_count);
    }
    if (signal_count>signal_limit) {
        digitalWrite(led, HIGH);
    }
    else {
    digitalWrite(led,LOW);
    }
}
5
  • think about the first two program lines inside the loop() block .... what do they do?
    – jsotola
    Feb 29 '20 at 18:31
  • 1
    What sound sensor are you using? Is its output analog or digital? And what do you mean by "signal"? What counts as a "signal" for you?
    – chrisl
    Feb 29 '20 at 19:30
  • I'm using digital sound detector sensor module. I don't know but it prolly uses analog (that's what I got from website references). The signal I referred to is the signal sent by the sound sensor to the arduino (HIGH or LOW) Mar 1 '20 at 9:55
  • Still you didn't explain what a "signal" is. Does it mean, that the sensor should go high and then low ( a pulse)? Or how do you want to count "signals"?
    – chrisl
    Mar 1 '20 at 10:28
  • I just wanted to count the HIGH signals, then use the number of signals recieved in a short amount of time to trigger a led to light up Mar 1 '20 at 16:11
1

Your handling of millis() is wrong. With these two lines:

unsigned long StartMillis = millis();
unsigned long CurrentMillis = millis();

StartMillis will always have the same value as CurrentMillis. (Ok, I guess its possible for millis() to return a value that's increased by 1 the second time it's called, but that is extremely unlikely. The 2 lines takes much less than a millisecond to execute.)

Your

while ((CurrentMillis - StartMillis)==5000) 

will never evaluate to true, so the code in the while block will never execute.

This if statement is also wrong:

if (digitalRead(sensor)=0)

You want == (comparison) not = assignment. I'm surprised the compiler even allowed that to compile, since you are trying to set the function digitalRead(sensor) to 0, which doesn't make sense.

Change that code to read:

if (digitalRead(sensor)==0)

(Assuming you want the body of the if statement to execute when your sensor value is 0.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.