Loading...
Logo
Processing Forum
So I'm trying to write some code for a basic alarm programme. The alarm uses an m4v file to play an audio file when triggered, and I want to make it so that when my alarm plays there is a delay of 8 seconds. In addition the delay time in numbers ( int values) should be displayed in the console window along with a print statement to mark the alarm as on e.g:

Copy code
  1. //Alarm
  2. //Counting: 1 2 3 4 5 6 7 8 

  3. My code so far:

  4. import processing.video.Movie;

  5. int alarm_hour;
  6. int alarm_minute;
  7. int alarm;
  8. int current_time;

  9. alarm_hour = 11;
  10. alarm_minute = 44;

  11. alarm = alarm_hour + alarm_minute;
  12. current_time = hour() + minute();

  13. //Where I'm attempting to place a delay that prints out the number of seconds (8 seconds) in int values
  14. if( alarm == current_time ){
  15.       println("ALARM!!!");
  16.       delay(8000);
  17. }

  18. new Movie(this,"alarm.m4v"); 


Replies(4)

Using "delay()" is bad, because it stops the whole program. So you won't be able to print or display anything in that time.

A better way was to store the time when your alarm was triggerd and then compare the current time to that value.
Something like this:
Copy code
  1. int t, count;
    boolean alarm;

    void draw() {
      if (alarm) {
        // increase the count-variable every second
        if ((millis()-t)/1000>count) {
          print(++count+" ");
        }
        // disable alarm after 8 seconds
        if (millis()-t > 8000) {
          alarm = false;
        }
      }
    }

    void mouseReleased() {
      // trigger ne alarm
      if (!alarm) {
        println();
        println("ALARM!!!");
        print("Counting: ");
        t = millis();
        count = 0;
        alarm = true;
      }
    }
Firstly thanks for the response, I've been looking at the code you recommended and trying to incorporate it into my code, but I'm still having some issues. Firstly with regard to the calculation for the timer:

    if ((millis()-t)/1000>count) {
      print(++count+" ");

I'm a little confused as to how this is working. My understanding is that the value of count is increased (c++) if t the timer, in this instance minus t, is divide by1000 if  t is less than than the value of count.

I see that the template of the sketch example you demonstrated uses void: draw() and mouseReleased() functions. I neglected to include this information in my first question but the code I'm writing is meant to run without the use of void functions or classes. I should note that the task I am trying to achieve is one set by a  tutor  as an exercise for understanding basic ideas of loops and if statements. These restrictions are imposed to restrict the code written to consist of only basic functions and loops.
Hi,

i try to explain these lines:
Copy code
  1.     if ((millis()-t)/1000>count) {
          print(++count+" ");
        }
millis() reutns the time(in milliseconds) that passed since the program started.
Inside of the mouseReleased-function i stored that value in "t".
So "millis()-t" is the difference between now and the point when the alarm was triggered.
If you divide it by 1000 you have the time in seconds.
When the time passed is 1 second, the condition gets true, because we set "count" to 0 inside mouseReleased().
Now we increment "count" so the condition is false again until the time passed exceeds 2 seconds.
This code does the same, but is a little longer:
Copy code
  1.     int now = millis();
        int timePassed = now-t;
        int secondsPassed = timePassed/1000;
        if (secondsPassed > count) {
          count = count +1;
          print(count+" ");
        }
Regarding the functions:
1. You need draw() !
This function is executed once every frame. So every sketch that changes over time, needs that function.

mouseReleased()
is a basic function too, it is called every time when you release a mouse-button. "void" is just the return-type and means that there is nothing returned. I used this function to trigger the alarm, but you could do it in a different way. For example with the variable mousePressed, if you don't want to use functions. But this is less elegant.




Thanks again for the help, I was having a lot of difficulty understanding that part of the code. Again sorry if I wasn't clear with my questions in the first place; just tying to get my head around the basics. With regards to the functions draw() mouseReleased(),  mousePressed()   etc.   I'd been advised against the use of these by my tutor and had been strictly instructed to write the code in the longer method to try and help me understand the logic/basic processes better. Thanks again for the help; greatly appreciated.