Problem with "millis"

Hi Guys ,,

I want to make a simple program doing :

every 8 seconds led blinks ... and if the led is ON .. the buzzer turn ON for 2 seconds ...

my code :

int led = 8;
int buzzer = 12;
int ledState = LOW;
int buzzerState = LOW;
void setup() {
  // put your setup code here, to run once:
  pinMode(led, OUTPUT);
  pinMode(buzzer, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  static long previous = 0;
  if ((millis() - previous) >= 8000) {
    previous = millis();
    if (ledState == LOW)
    {
      ledState = HIGH;
    }
    else {
      ledState = LOW;
    }
    digitalWrite(led,ledState);
   static long previous2 = 2000;
    if ((millis() - previous2)>=6000)
    {
     if (buzzerState == LOW)
    {
      buzzerState = HIGH;
    }
    else {
      buzzerState = LOW;
    }
    digitalWrite(buzzer,buzzerState);
    }
  }
}

Thanks

static long previous = 0;

malaa_borg:
Hi Guys ,,

I want to make a simple program doing :

every 8 seconds led blinks ... and if the led is ON .. the buzzer turn ON for 2 seconds ...

This is what you want it to do. What does it actually do?

gfvalvo:
This is what you want it to do. What does it actually do?

No bro ,it doesn't work well

malaa_borg:
every 8 seconds led blinks ... and if the led is ON .. the buzzer turn ON for 2 seconds ...

How about this? Sometimes delay() is the right answer for simple sketches.

const byte LEDpin = 8;
const byte BuzzerPin = 12;

void setup() {
  // put your setup code here, to run once:
  pinMode(LEDPin, OUTPUT);
  pinMode(BuzzerPin, OUTPUT);
}

void loop() {
   digitalWrite(LEDPin , HIGH);
   digitalWrite(BuzzerPin, HIGH);
   delay(2000);
   digitalWrite(BuzzerPin, LOW);
   delay(6000);
    digitalWrite(LEDPin, LOW);
   delay(8000);
}

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 .

johnwasser:
How about this? Sometimes delay() is the right answer for simple sketches.

const byte LEDpin = 8;

const byte BuzzerPin = 12;

void setup() {
 // put your setup code here, to run once:
 pinMode(LEDPin, OUTPUT);
 pinMode(BuzzerPin, OUTPUT);
}

void loop() {
  digitalWrite(LEDPin , HIGH);
  digitalWrite(BuzzerPin, HIGH);
  delay(2000);
  digitalWrite(BuzzerPin, LOW);
  delay(6000);
   digitalWrite(LEDPin, LOW);
  delay(8000);
}

A lot of thanks for your reply ... But delay freezes my program ..and I can't add any timing sentences ...

Try this...

//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

Smajdalf:
static long previous = 0;

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).

kenwood120s:
Try this...

//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

Thank you ..

That's exactly what I want ... It works !!!! _

malaa_borg:
Thank you ..

That's exactly what I want ... It works !!!! _

YVW, but you should work through it if you didn't already, and be sure you understand it.