Pages: [1]   Go Down
Author Topic: How do I stop Time.Alarm in my sketch?  (Read 841 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In this sketch I need to add a command  ,so I can stop  TimeAlarm  triger  spinning()  every time i reset timer  (or I add hours with a seccond button).
I need spinning triggered  60 sec after turn on or reset  and after that ,every hour.
thank you

Code:
#include <Time.h>
#include <TimeAlarms.h>
#include <Stepper.h>
     
      int  MYType;
      int resetPin = A2;
      int addHourPin = A3; 
      int addMinPin = A4;
   
   
    const int stepsPerRevolution = 48; 
    Stepper stepper(stepsPerRevolution, 8, 9, 10, 11);
   
   
   
   
   
void setup(){
 
                 
                  stepper.setSpeed(150);
                   setTime(0,0,0,1,1,11);
     
                 Alarm.timerOnce(60, spinning);  //step motor spins 60 sec after boot
                 Alarm.timerRepeat(3600, spinning);  //step motor spins after an hour
         }
 
 
void loop(){
 
            //  RANDOM COMMANDS

 /*=========== GENERAL reset pin. Everithing resets to zero when presed=============*/
 
             if(digitalRead(resetPin) == HIGH){
                                      Resetingeverithing ();
//command to turn off Alarm.timers
                                       setTime(0,0,0,1,1,11);  //at this point  Alarms are trigered always
//command to turn on Alarm.timers
                     }//end of reset
         }  //END of LOOP

void Resetingeverithing () {
 
                     Alarm.delay (2000);
                     lcd.clear();
                     lcd.print("timer will be set back to zero");
                     //going back to loop
         }
                     
                     
void spinning (){
                           stepper.step(100);
         }
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why do you need a timer at all? Can't you use a clock and determine when to do things?

Read, understand, and embrace the blink without delay example.
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4261
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am not clear what you want to do exactly, and your current code does nothing as far as I can see, apart from constantly setting the time to the same values every time through loop().  The alarms will not trigger because you have no call to Alarm.delay() in loop() and even your Resetingeverithing() function does nothing but wait 2 seconds, clear the LCD and then print a message.

If you want to turn alarms on and off you need to know their ID, which you get like this
Code:
ID = Alarm.timerOnce(60, spinning);
Then, to disable/enable an alarm do this
Code:
  disable(ID);  //-  prevent the alarm associated with the given ID from triggering   
  enable(ID); // -  enable the alarm
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

my code is  redused to minimal so you can understand what I need.

so my code needs to be like this ?
Code:
#include <Time.h>
#include <TimeAlarms.h>
#include <Stepper.h>
     
      int  MYType;
      int resetPin = A2;
      int addHourPin = A3; 
      int addMinPin = A4;
     
    int ID ;
    int ID2 ;
   
    const int stepsPerRevolution = 48; 
    Stepper stepper(stepsPerRevolution, 8, 9, 10, 11);
     
   
void setup(){
                 
                  stepper.setSpeed(150);
                   setTime(0,0,0,1,1,11);
      ID = Alarm.timerOnce(60, spinning);
      ID2=Alarm.timerRepeat(3600, spinning);
      //           Alarm.timerOnce(60, spinning);  //step motor spins 60 sec after boot
      //           Alarm.timerRepeat(3600, spinning);  //step motor spins after an hour
         }
 
void loop(){
 
             RANDOM COMMANDS ;

 /*=========== GENERAL reset pin. Everithing resets to zero when presed=============*/
 
             if(digitalRead(resetPin) == HIGH){
                                      Resetingeverithing ();
//command to turn off Alarm.timers
disable(ID);
disable(ID2);
                                       setTime(0,0,0,1,1,11);  //at this point  Alarms are trigered always
//command to turn on Alarm.timers
enable(ID);
enable(ID2);
                     }//end of reset
         }  //END of LOOP

void Resetingeverithing () {
 
                     Alarm.delay (2000);
                     lcd.clear();
                     lcd.print("timer will be set back to zero");
                     //going back to loop
         }
                     
void spinning (){
                           stepper.step(100);
         }


because  it gives me  this
Code:
sketch_feb21a:41: error: 'disable' was not declared in this scope
sketch_feb21a:45: error: 'enable' was not declared in this scope
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4261
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

enable and disable are functions of TimeAlarms so they need to be identified as such like any other functions in the library.  Call them like this.
Code:
Alarm.disable(ID);

I am still not sure what your code tries to do, partly because it is not formatted.  Please use Tools/Auto Format from the IDE menu.
Why do you set the current time over and over in loop() ?

What may be happening is that when you set the current time in loop() it is in the past so the alarm conditions are met immediately, but I am not sure.


Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for your awsers .
As I told you before  I chopped  my originally sketch  so it is more easy to understand my problem
Originally this sketch is an egg incubbator  and timer needed for egg spinning smiley-razz
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Tools + Auto Format is YOUR friend, too.

          That code
     that jumps
                      all over the
         page
is a real
                          challenge to read.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes thank for your advise .Already done smiley
Logged

Pages: [1]   Go Up
Jump to: