Go Down

Topic: Stepper Quesetion....Help Please (Read 629 times) previous topic - next topic

Feb 01, 2013, 08:28 am Last Edit: Feb 01, 2013, 08:48 am by AWOL Reason: 1
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.


Code: [Select]

/* 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


UKHeliBob

I will ask before anyone else does.  Please edit your post and put your program(s) in code tags = # above the smileys.  It makes it so much easier to read as does taking out excess blank lines.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

UKHeliBob

When RTCsecond goes to 0 as the minute changes will it be bigger than stepperSec (55 at the time) + 5 ?
You need to reset stepperSec to zero when it rolls over 60.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

How do I reset it back to 0 as it rollsover to 60? Sorry I'm really dumb at arduino...........

UKHeliBob

Code: [Select]
If (stepperSec >= 60)
{
  stepperSec = 0;
}

You already have one if in your code.  This works the same way.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

If tried the code u suggest but it still doesn't work............. Why?

AWOL

Quote
If tried the code u suggest but it still doesn't work............. Why?

Because it is incorrect?
We can't see your code.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UKHeliBob

Please post your whole code, including the resetting of stepperSec
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PaulS

And tell us which version of the IDE you are using. That comment after Serial.flush() is crap.

Go Up