Go Down

Topic: TimeAlarms and conditons..............Help!?!? (Read 317 times) previous topic - next topic

Feb 08, 2013, 05:36 pm Last Edit: Feb 08, 2013, 06:12 pm by ArduiNoooooooB Reason: 1
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: [Select]
/*
* 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);
}



UKHeliBob

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: [Select]
  if(hour() > 15 && hour() < 17)
Consider what will happen when hour() is 15 ?  Will hour() be greater than 15 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

#2
Feb 08, 2013, 06:20 pm Last Edit: Feb 08, 2013, 06:22 pm by ArduiNoooooooB Reason: 1

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: [Select]
 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????

UKHeliBob

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: [Select]
alarmID = Alarm.timerRepeat(5, stepnow);
then disable it like this
Code: [Select]
Alarm.disable(alarmID);
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PaulS

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.

Docedison

Finally the right answer. Like pulling Stumps..

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

UKHeliBob


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 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up