Delta_G:
Yeah we got that. But you were asked what actually happens. We are trying to understand what your problem is.
And don't call me bro. That's for you kids and your little friends. I'm a professional. I'm not your bro. Try to have a little bit of respect for people volteering their time to help you for free.
Led works well .. But buzzer still turns on even after the 3 seconds .
And I'm very sorry to talk with you like that .. I Should be more polite .
//8 second led blink
// and 2 second buzzer when led goes on
//1 oct 2017
//forum thread 503188
byte ledPin = 2;
int ledInterval = 2000; //should be 8000, 2000 for quick test
bool ledState = 0;
unsigned long ledPreviousMillis = 0;
byte buzzerPin = 3;
int buzzerInterval = 500; //should be 2000, 500 for quick test
unsigned long buzzerPreviousMillis = 0;
unsigned long currentMillis;
void setup()
{
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
}//setup
void loop()
{
currentMillis = millis();
if (currentMillis - ledPreviousMillis >= ledInterval)
{
ledState = !ledState;
digitalWrite(ledPin, ledState);
ledPreviousMillis = currentMillis;
if (ledState == HIGH) //turn buzzer on if led just came on
{
digitalWrite(buzzerPin, HIGH);
buzzerPreviousMillis = currentMillis; //buzzer went on at..
}
}
if (ledState == HIGH) //see if it's time to turn buzzer off
{
if (currentMillis - buzzerPreviousMillis >= buzzerInterval)
{
digitalWrite(buzzerPin, LOW); //buzzer only on for first part of led on
}
}
}//loop
That was stupid - static variables are initialized only once.
But on the other hand you never update previous2 in the OP. Try to use ctrl+T to reformat the sketch. I think
if ((millis() - previous2)>=6000)
should not be part of
if ((millis() - previous) >= 8000).
if (ledState == HIGH) //turn buzzer on if led just came on
{
digitalWrite(buzzerPin, HIGH);
buzzerPreviousMillis = currentMillis; //buzzer went on at..
}
}
if (ledState == HIGH) //see if it's time to turn buzzer off
{
if (currentMillis - buzzerPreviousMillis >= buzzerInterval)
{
digitalWrite(buzzerPin, LOW); //buzzer only on for first part of led on
}
}