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);
}
}
/*
* 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);
}