buzzer sound for 10sec when the state of a input pin changes

Hi,

I want to sound a buzzer for 10sec when the state of a input pin changes. I have written following code using millis() function. There are no compilation errors. But when I am changing the pin state buzzer is not giving any sound. I chose digital pin 8 (referred as a) as input, digital pin 13 as output(buzzer). 'b' and 'c' are states of the input pin at currentmillis and previousmillis. Can you please have a look at the code and give me some sugggestions?

int b;
const int a = 8;
int c;
unsigned long previousMillis = 0;
const long interval = 1000;
void setup() {
// put your setup code here, to run once:
pinMode(a, INPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);

}

void loop() {

unsigned long currentMillis = millis();

Serial.println(“previousMillis”);
Serial.println(previousMillis);
Serial.println(“currentMillis”);
Serial.println(currentMillis);
if(currentMillis – previousMillis >= interval) {
b = digitalRead(a);
Serial.println(“b”);
Serial.println(b);
previousMillis = currentMillis;
c = digitalRead(a);
if(b != c) {
digitalWrite(13, HIGH);
delay(10000);
}
else {
digitalWrite(13, LOW);
}
Serial.println(“c”);
Serial.println(c);
}
delay(1000);
}

Thanking you.

NOTE - Your code will be easier for us to read if you enclose it in code tags (click the </> button when typing/editing).

if(currentMillis - previousMillis >= interval) {
b = digitalRead(a);
Serial.println("b");
Serial.println(b);
previousMillis = currentMillis;
c = digitalRead(a);
if(b != c) {
digitalWrite[/quote]

You are reading the switch, printing-out a couple of things, reading the switch again almost immediately, and then delaying.

If the switch-state doesn’t change within that very-short time window you won’t catch it. The odds of that happening are slim.

You need to be looping “fast” without any delays while looking for a state change.

From your text description, there is no need for the 1 second delay, and you’ve got two of them. (One using millis() and one using delay().

If you need a 1 second delay and a 10 second delay you can make two (or more) timers with millis(). i.e. Interval1 and PreviousMillis1, plus Interval10 and PreviousMillis10. (Of course, there is only one current time).

…You’ve also got an extra variable (you only need to compare the current state to the previous state and make the previous state equal to the current state before re-starting the loop).

And, your variables should be more descriptive - Instead of ‘a’ you can use ‘InputPin’ and ‘b’ you can use ‘CurrentButtonState’, etc. Whatever makes it easier for someone to read & understand your code, and something that will make it easier for you to understand when you come back to it a month later.

And, comments like - // put your setup code here, to run once are hints for you (the programmer) before you start. They are not helpful once you start programming and you should delete it. You could say something similar like, “This code runs once during start-up.” But, it’s not really necessary since every Arduino programmer should know what setup() does.

You can add comments explaining what variables are used for, or the purpose of a loop, or function etc. A comment at top explaining the purpose/function of the program like your text explanation in you post here can be nice too.

HINT - Start simpler! Read the switch and print the state (in a loop). Then add the code to look for a state-change and print-out a message ever time the state changes, or sound the beeper. Don’t worry about the beeper timing, and you don’t even have to worry about turning the beeper off (except it might be annoying) until you’ve figured-out how to reliably detect a switch state change.

Hello,
Thank you very much for your valuable suggestions. I am a beginner. i will change my code according to your comments and will check.