Pages: [1]   Go Down
Author Topic: TimeAlarms and conditons..............Help!?!?  (Read 251 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well I'm trying desperately to step a motor but its not working. Basically I'm using the time and timer alarm libraries to step a motor in intervals during 3 - 5pm. I'm using a repeat timer to call a function called stepnow every five seconds. stepnow has a conditional if statement to check if time is between 3-5 and then steps motor once. Timer then calls stepnow again after five seconds and checks if time is between 3-5pm...... or atleast that's how it's supposed to be...........
So........

Please help..........


// functions to be called when an alarm triggers:
void stepnow(){
  if(hour() > 15 && hour() < 17){
 Serial.println("Stepping");
 myStepper.step(1);
}
}


Code:
/*
 * TimeAlarmExample.pde
  */
 
#include <Time.h>
#include <TimeAlarms.h>
#include <Wire.h>
#include <Stepper.h>
#define DS1307_I2C_ADDRESS 0x68

int ledPin =  13;
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 8,9,10,11);

// for RTC work
byte RTCsecond, RTCminute, RTChour, RTCdayOfWeek, RTCdayOfMonth, RTCmonth, RTCyear;


void setup()
{
  pinMode(ledPin, OUTPUT); // show the status
   myStepper.setSpeed(1);
  Serial.begin(9600);
  Serial.flush();
  Wire.begin();
  
  clkSync();
  
 // create the alarms
 
  Alarm.timerRepeat(5, stepnow);            // timer for every 5 seconds  

}

void  loop(){  
  digitalClockDisplay();
  Alarm.delay(1000); // wait one second between clock display
}

// functions to be called when an alarm triggers:
void stepnow(){
  if(hour() > 15 && hour() < 17){
 Serial.println("Stepping");
 myStepper.step(1);
}
}



void digitalClockDisplay()
{
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println();
}

void printDigits(int digits)
{
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}




void setDateDs1307(byte RTCsecond,        // 0-59
byte RTCminute,        // 0-59
byte RTChour,          // 1-23
byte RTCdayOfWeek,     // 1-7
byte RTCdayOfMonth,    // 1-28/29/30/31
byte RTCmonth,         // 1-12
byte RTCyear)          // 0-99
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.write(decToBcd(RTCsecond));    
  Wire.write(decToBcd(RTCminute));
  Wire.write(decToBcd(RTChour));      
  Wire.write(decToBcd(RTCdayOfWeek));
  Wire.write(decToBcd(RTCdayOfMonth));
  Wire.write(decToBcd(RTCmonth));
  Wire.write(decToBcd(RTCyear));
  Wire.endTransmission();
}

// Gets the date and time from the ds1307
void getDateDs1307(byte *RTCsecond,
byte *RTCminute,
byte *RTChour,
byte *RTCdayOfWeek,
byte *RTCdayOfMonth,
byte *RTCmonth,
byte *RTCyear)
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  *RTCsecond     = bcdToDec(Wire.read() & 0x7f);
  *RTCminute     = bcdToDec(Wire.read());
  *RTChour       = bcdToDec(Wire.read() & 0x3f);  
  *RTCdayOfWeek  = bcdToDec(Wire.read());
  *RTCdayOfMonth = bcdToDec(Wire.read());
  *RTCmonth      = bcdToDec(Wire.read());
  *RTCyear       = bcdToDec(Wire.read());
}

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

void clkSync()
{
  
    getDateDs1307(&RTCsecond, &RTCminute, &RTChour, &RTCdayOfWeek, &RTCdayOfMonth, &RTCmonth, &RTCyear);
    setTime(RTChour,RTCminute,RTCsecond,RTCmonth,RTCdayOfMonth,RTCyear);
}

« Last Edit: February 08, 2013, 12:12:12 pm by ArduiNoooooooB » Logged

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

What happens in practice ?
Does the code that steps the motor and the RTC code work in isolation ?

What I would do is to set a daily alarm that triggered every day at 15:00 whose action function set up an alarm that repeated at 5 second intervals.  Then a second daily alarm that triggered at 17:00 whose action function cancelled the 5 second repeat one.

Your test to see whether the stepper function should be called if flawed though.
Code:
  if(hour() > 15 && hour() < 17)
Consider what will happen when hour() is 15 ?  Will hour() be greater than 15 ?
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: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What happens in practice ?
Does the code that steps the motor and the RTC code work in isolation ?

What I would do is to set a daily alarm that triggered every day at 15:00 whose action function set up an alarm that repeated at 5 second intervals.  Then a second daily alarm that triggered at 17:00 whose action function cancelled the 5 second repeat one.

Your test to see whether the stepper function should be called if flawed though.
Code:
 if(hour() > 15 && hour() < 17)
Consider what will happen when hour() is 15 ?  Will hour() be greater than 15 ?


I guess I can set up an alarm to in turn set up another timer alarm. But how do I do a second daily alarm to cancel the first repeat alarm????
« Last Edit: February 08, 2013, 12:22:11 pm by ArduiNoooooooB » Logged

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

Do you mean how do you set up a second daily alarm or how do you cancel an alarm ?

The TimeAlarms example has daily alarms so that should not cause you any problem.  What it does not have is an example of cancelling an alarm, but the readme briefly mentions the disable( ID) function.  You can get the ID when setting up the alarm
Code:
alarmID = Alarm.timerRepeat(5, stepnow);
then disable it like this
Code:
Alarm.disable(alarmID);
Logged

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

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

The point was that the test should be for hour() greater than or equal 15 and less than or equal 17. Otherwise, stuff will happen only when hour() is 16.
Logged

Anaheim CA.
Offline Offline
Faraday Member
**
Karma: 44
Posts: 2812
...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Finally the right answer. Like pulling Stumps..

Bob
Logged

--> WA7EMS <--
“The solution of every problem is another problem.” -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

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

Finally the right answer. Like pulling Stumps..

Bob
Are you suggesting that I should have given the answer rather than a very pointed clue to the answer ?
Logged

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

Pages: [1]   Go Up
Jump to: