Problem in Millis

Hi can you help me with my codes, where should i include millis if i want the program to continue after it ring the buzzer for 1 sec and it to ring again when the program ends. ( It does not work when i put it above the buzzer coding.)

#include <SFE_BMP180.h>

#include <FastLED.h>
#include <Wire.h>


SFE_BMP180 BMP180;
const int buzzer = 9; //buzzer to arduino pin 9
const int ledPin = 13;
const int ldrPin = A0;
#define LED_PIN 7
#define NUM_LEDS 20
CRGB leds[NUM_LEDS];

void setup() {
Serial.begin(9600);
bool success = BMP180.begin();
if (success) {
Serial.println("BMP180 init success");
Serial.println("Start"); 
}

pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ldrPin, INPUT);
pinMode(10,OUTPUT);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
}
unsigned long event = 2000;
void loop() { 
Serial.println(millis());


{
{if(millis() >= event)
tone(buzzer, 1000); // Send 1KHz sound signal...
delay(1000); // ...for 5 sec
noTone(buzzer); // Stop sound...
delay(30000); // ...for 30 sec


{
  
int ldrStatus = analogRead(ldrPin);
if (ldrStatus <= 400)
{
digitalWrite(ledPin, HIGH);
Serial.print("Its Dark, Turn on the LED:");
Serial.println(ldrStatus);
digitalWrite(10,HIGH);
delay(1000);
digitalWrite(10,LOW); // do stuff if the condition is true
Serial.println("ON LED");
}
else
{
digitalWrite(ledPin, LOW);
Serial.print("Its Bright, Turn off the LED:");
Serial.println(ldrStatus);
leds[0] = CRGB(255, 0, 0);
FastLED.show();
delay(0); // do stuff if the condition is false
Serial.println("OFF LED");
}
// put your main code here, to run repeatedly:
digitalWrite(10,HIGH);
delay(1000);
digitalWrite(10,LOW);
char status;
double T, P;
bool success = false;
status = BMP180.startTemperature();
int PressureValve;
if (status != 0) {
delay(4000);
status = BMP180.getTemperature(T);
if (status != 0) {
status = BMP180.startPressure(3);
if (status != 0) {
delay(status);
status = BMP180.getPressure(P,T);
if (status != 0) {
Serial.print("Pressure: ");
Serial.print(P);
Serial.println(" hPa");
Serial.print("Temperature: ");
Serial.print(T);
Serial.println(" C");


}
}
}
}
}
}
}
}

First auto format your code in the IDE ( Ctrl + T ), to make it look neater! Maybe this simple millis() tutorial can help you..!

Auto formated code:

#include <SFE_BMP180.h>

#include <FastLED.h>
#include <Wire.h>


SFE_BMP180 BMP180;
const int buzzer = 9; //buzzer to arduino pin 9
const int ledPin = 13;
const int ldrPin = A0;
#define LED_PIN 7
#define NUM_LEDS 20
CRGB leds[NUM_LEDS];

void setup() {
  Serial.begin(9600);
  bool success = BMP180.begin();
  if (success) {
    Serial.println("BMP180 init success");
    Serial.println("Start");
  }

  pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(ldrPin, INPUT);
  pinMode(10, OUTPUT);
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
}
unsigned long event = 2000;
void loop() {
  Serial.println(millis());


  {
    { if (millis() >= event)
        tone(buzzer, 1000); // Send 1KHz sound signal...
      delay(1000); // ...for 5 sec
      noTone(buzzer); // Stop sound...
      delay(30000); // ...for 30 sec


      {

        int ldrStatus = analogRead(ldrPin);
        if (ldrStatus <= 400)
        {
          digitalWrite(ledPin, HIGH);
          Serial.print("Its Dark, Turn on the LED:");
          Serial.println(ldrStatus);
          digitalWrite(10, HIGH);
          delay(1000);
          digitalWrite(10, LOW); // do stuff if the condition is true
          Serial.println("ON LED");
        }
        else
        {
          digitalWrite(ledPin, LOW);
          Serial.print("Its Bright, Turn off the LED:");
          Serial.println(ldrStatus);
          leds[0] = CRGB(255, 0, 0);
          FastLED.show();
          delay(0); // do stuff if the condition is false
          Serial.println("OFF LED");
        }
        // put your main code here, to run repeatedly:
        digitalWrite(10, HIGH);
        delay(1000);
        digitalWrite(10, LOW);
        char status;
        double T, P;
        bool success = false;
        status = BMP180.startTemperature();
        int PressureValve;
        if (status != 0) {
          delay(4000);
          status = BMP180.getTemperature(T);
          if (status != 0) {
            status = BMP180.startPressure(3);
            if (status != 0) {
              delay(status);
              status = BMP180.getPressure(P, T);
              if (status != 0) {
                Serial.print("Pressure: ");
                Serial.print(P);
                Serial.println(" hPa");
                Serial.print("Temperature: ");
                Serial.print(T);
                Serial.println(" C");


              }
            }
          }
        }
      }
    }
  }
}

TheUNOGuy:
First auto format your code in the IDE ( Ctrl + T ), to make it look neater! Maybe this simple millis() tutorial can help you..!

i did try out on my coding but it did not work as i wanted. Not quite sure how to put in as it is different from my coding.
Could you help me to put millis() in my coding. Thanks.

See How to use millis() instead of delay()

bryan021:
i want ... it to ring again when the program ends.

That doesn't make sense, since the very nature of loop() in an Arduino sketch is that it never ends.... well, unless you pull the power anyway.
Can you elaborate on what you mean?

fionning_macara:
That doesn’t make sense, since the very nature of loop() in an Arduino sketch is that it never ends… well, unless you pull the power anyway.
Can you elaborate on what you mean?

It ring the buzzer again before it ends the whole coding.

before it ends the whole coding

But that's my point... there is no end.

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

fionning_macara:
But that's my point... there is no end.

Can you please advise me on how to end the program, I am not that good in Arduino. Thanks for helping.

What do you mean by end the program? Programs don't end, although they might stop doing whatever they do and wait.

Take for example your microwave oven; there will be a microcontroller in there with a program running all the time waiting for you to input some details on the keypad. So you put some food in the oven, shut the door and enter what you want on the keypad, the oven heats up the food, stops then you take out the hot food. Job done. The program doesn't stop, it keeps running waiting for next time.

It might be better if instead of asking for help with the code as presented, you (bryan021) describe in English (not code) what you actually want to achieve.

fionning_macara:
It might be better if instead of asking for help with the code as presented, you (bryan021) describe in English (not code) what you actually want to achieve.

Oh, my bad.
I would like to continue the program after the buzzer ring for 1 sec and turn off which would go on to the **ldr sensor to switch on the led **(posted below) and ending the program once the buzzer ring again.
*Buzzer *

{[color=#222222][/color]
    { [color=#222222][/color]
      tone(buzzer, 1000); // Send 1KHz sound signal...[color=#222222][/color]
      delay(1000); // ...for 5 sec[color=#222222][/color]
      noTone(buzzer); // Stop sound...[color=#222222][/color]
      delay(30000); // ...for 30 sec

Ldr sensor

 {[color=#222222][/color]
[color=#222222][/color]
        int ldrStatus = analogRead(ldrPin);[color=#222222][/color]
        if (ldrStatus <= 400)[color=#222222][/color]
        {[color=#222222][/color]
          digitalWrite(ledPin, HIGH);[color=#222222][/color]
          Serial.print("Its Dark, Turn on the LED:");[color=#222222][/color]
          Serial.println(ldrStatus);[color=#222222][/color]
          digitalWrite(10, HIGH);[color=#222222][/color]
          delay(1000);[color=#222222][/color]
          digitalWrite(10, LOW); // do stuff if the condition is true[color=#222222][/color]
          Serial.println("ON LED");[color=#222222][/color]
        }[color=#222222][/color]
        else[color=#222222][/color]
        {[color=#222222][/color]
          digitalWrite(ledPin, LOW);[color=#222222][/color]
          Serial.print("Its Bright, Turn off the LED:");[color=#222222][/color]
          Serial.println(ldrStatus);[color=#222222][/color]
          leds[0] = CRGB(255, 0, 0);[color=#222222][/color]
          FastLED.show();[color=#222222][/color]
          delay(0); // do stuff if the condition is false[color=#222222][/color]
          Serial.println("OFF LED");[color=#222222][/color]
        }[color=#222222][/color]
        // put your main code here, to run repeatedly:[color=#222222][/color]
        digitalWrite(10, HIGH);[color=#222222][/color]
        delay(1000);[color=#222222][/color]
        digitalWrite(10, LOW);[color=#222222][/color]
        char status;[color=#222222][/color]
        double T, P;[color=#222222][/color]
        bool success = false;[color=#222222][/color]
        status = BMP180.startTemperature();[color=#222222][/color]
        int PressureValve;[color=#222222][/color]
        if (status != 0) {[color=#222222][/color]
          delay(4000);[color=#222222][/color]
          status = BMP180.getTemperature(T);[color=#222222][/color]
          if (status != 0) {[color=#222222][/color]
            status = BMP180.startPressure(3);[color=#222222][/color]
            if (status != 0) {[color=#222222][/color]
              delay(status);[color=#222222][/color]
              status = BMP180.getPressure(P, T);[color=#222222][/color]
              if (status != 0) {[color=#222222][/color]
                Serial.print("Pressure: ");[color=#222222][/color]
                Serial.print(P);[color=#222222][/color]
                Serial.println(" hPa");[color=#222222][/color]
                Serial.print("Temperature: ");[color=#222222][/color]
                Serial.print(T);[color=#222222][/color]
                Serial.println(" C");[color=#222222][/color]
[color=#222222][/color]
[color=#222222][/color]
              }[color=#222222][/color]
            }[color=#222222][/color]
          }[color=#222222][/color]
        }[color=#222222][/color]
      }[color=#222222][/color]
    }[color=#222222][/color]
  }[color=#222222][/color]
}

bryan021:
Oh, my bad.
I would like to continue the program after the buzzer ring for 1 sec and turn off which would go on to the ldr sensor to switch on the led (posted below) and ending the program once the buzzer ring again.

Please just describe how the entire program should work in English and without any references to code. That will make it much easier for us to understand your problem.

It may even highlight the problem and solution for you.

Normally an Arduino program never ends until you disconnect the power.

...R

Robin2:
Please just describe how the entire program should work in English and without any references to code. That will make it much easier for us to understand your problem.

It may even highlight the problem and solution for you.

Normally an Arduino program never ends until you disconnect the power.

…R

The program would start by uploading it to the Arduino Uno board and it would ring the buzzer and ldr sensor would start to sense if its dark or bright and if its is dark ,the led will lit up or if is bright it would not light the led up.
If its bright it the led will not lit up and the ldr will continue sensing for light till there no light (continue below).
If is dark the led will be lit up for 1 minutes and at the same time using the pressure sensor to detect the pressure and temperature, after it reaches 1 min the buzzer will ring and the led will be switch off and end.

I think it's this which is confusing us:

and end

Programs don't end as such: it probably seems in your mind that they do, but what really happens is the code continues to run thru loop() as long as there's power. It's just that you'll have a state variable called (say) state_IDLE where it's loop()-ing but doing nothing. (As suggested above in the microwave example, the food's cooked, and it seems as if there's no code running in the processor: there is, it's just not doing anything you can see or hear (or smell :wink: )).

bryan021:
The program would start by uploading it to the Arduino Uno board and it would ring the buzzer and ldr sensor would start to sense if its dark or bright and if its is dark ,the led will lit up or if is bright it would not light the led up.
If its bright it the led will not lit up and the ldr will continue sensing for light till there no light (continue below).
If is dark the led will be lit up for 1 minutes and at the same time using the pressure sensor to detect the pressure and temperature, after it reaches 1 min the buzzer will ring and the led will be switch off and end.

IMHO the following way of describing that makes things a lot clearer and provides a basis for writing code to implement the requirement.

Power up
buzzer sounds (for how long?)
repeat from here
    check LDR value
        if bright
            do nothing
        if dark and LED is off
            turn LED ON
            save the time
    
    if LED is ON
        detect pressure
        detect temperature

    if duration since time was saved > 1 minute
        turn LED off
        sound buzzer (for how long?)
repeat

...R

Analyze the logic behind the program like Robin did...Knowing the syntax is not the only thing in programming...We gave the hints. Now write your code according to that. If you are lazy and don't want to write the code, post your problem in Gigs and Collaborations ( which I would highly not recommend; I encourage people to write the code themselves )...

Robin2:
IMHO the following way of describing that makes things a lot clearer and provides a basis for writing code to implement the requirement.

Power up

buzzer sounds (for how long?)
repeat from here
   check LDR value
       if bright
           do nothing
       if dark and LED is off
           turn LED ON
           save the time
   
   if LED is ON
       detect pressure
       detect temperature

if duration since time was saved > 1 minute
       turn LED off
       sound buzzer (for how long?)
repeat




...R

Can i ask whats the meaning "save the time"? I follow the basis you have written it does helps in my coding.

bryan021:
Can i ask whats the meaning “save the time”? I follow the basis you have written it does helps in my coding.

“Usually” it means to save the value of millis() to a variable.