I'm a complete noob at arduino and I'm having problems stepping a motors. My program basically checks the arduino time and then steps the motor after every 5 seconds have past since the arduino time. I'm using a DS1307 rtc to sync the arduino time. Problem is the motor steps every 5 seconds and displays stepping. However when the serial port time passes a minute it does not step anymore of serial print my message eg when the seconds goes from 55 to 1 second it stops stepping. Opening and closing the serial port again makes it step again. Sorry if my code is messy. Any help is greatly appreciated.
/* THIS IS MY PROGRAM. SIGH..........................
*/
#include <Stepper.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);
#include <Time.h>
#include <Wire.h>
#define DS1307_I2C_ADDRESS 0x68
int ledPin = 13;
// for RTC work
byte RTCsecond, RTCminute, RTChour, RTCdayOfWeek, RTCdayOfMonth, RTCmonth, RTCyear;
//my global variables
byte stepperMin, stepperSec;
void setup()
{
pinMode(ledPin, OUTPUT); // show the status
myStepper.setSpeed(2); //Motor RPM
Serial.begin(9600); // for debugging
Serial.flush(); // need to flush serial buffer, otherwise first read from reset/power on may not be correct
Wire.begin(); // initialise i2c bus for DS1307
clkSync(); //sync rtc values to arduino
}
void loop(){
digitalClockDisplay();
delay(1000);
getDateDs1307(&RTCsecond, &RTCminute, &RTChour, &RTCdayOfWeek, &RTCdayOfMonth, &RTCmonth, &RTCyear);
if(RTCsecond>stepperSec+5){
Serial.println("Stepping");
myStepper.step(4); //activate stepper motor
stepperSec=RTCsecond; //reinit variable to prepare for next loop
}
}
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)); // 0 to bit 7 starts the clock
Wire.write(decToBcd(RTCminute));
Wire.write(decToBcd(RTChour)); // If you want 12 hour am/pm you need to set
// bit 6 (also need to change readDateDs1307)
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); // Need to change this if 12 hour am/pm
*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); // Set the Arduino System To Value retrieved from DS1307
}
Moderator edit: SIGH. and there are some CODE TAGS