On a Arduino Mega 1560. I'm attempting to make a clock using two stepper motors as the hands. Its a count-down 24hour clock, one hand(hours) does 24 ticks the other is a minute hand that runs at 5 minute intervals therefore ticks 12 times. Once it has ended it turns a servo that drops a ball.
What I'm Using
(2) 42BY48H08 stepping motors (7.5 degree steps)
(2) Spark Fun easy driver boards
and a Parallax Standard Servo
For some reason the code does not work. It will either rotate one stepper or the other stepper not both. The servo acts out of place, it doesn't wait for the code to finish before changing its position.
It is suppose to make 1 full rotation on the minute hand(clockwise), then move the hours hand down(anti-clockwise) 1 tick, make another full rotation of the minute hand then move the hour hand down a tick until it reaches zero, where it then will operate the servo. I apologize if my code isn't very neat, and I left all my comments in so hopefully that may help. I'll be troubleshooting myself, but a bit of outsider help may certainly help. Any information would be greatly appreciated.
I've also attached how I have everything wired via a Fritzing Diagram.
Thanks
#include <Servo.h>
int lastIncrement = 0;
int rotationCount = 0;
int rotationCount2 = 0;
int totalRotation = 48;
int servoDrop = false;
// Servo information
int STEPPER1_DIR_PIN = 2; // the pin which the driver's DIR PIN is connected to on the arduino board
int STEPPER1_STEP_PIN = 3; // the pin which the driver's STEP PIN is connected to on the arduino board
int STEPPER2_DIR_PIN = 4; // the pin which the driver's DIR PIN is connected to on the arduino board
int STEPPER2_STEP_PIN = 5; // the pin which the driver's STEP PIN is connected to on the arduino board
int const MOVE_SPEED = 2000; // The stepper will work better when there is more of a delay. Decreasing this will reduce its power.
int const TOTAL_STEPS = 48; // The number of steps that the stepper motor will take
int const MICROSTEPS = 8; // Each step is broken down into 8 microsteps by the driver.
// servo stuff
int SERVO_PIN = A0;
Servo servo ;
void setup()
{
Serial.begin( 9600 );
// Set up each of the pins for output
pinMode(STEPPER1_DIR_PIN, OUTPUT);
pinMode(STEPPER1_STEP_PIN, OUTPUT);
// Set up each of the pins for output
pinMode(STEPPER2_DIR_PIN, OUTPUT);
pinMode(STEPPER2_STEP_PIN, OUTPUT);
// add a servo
servo.attach( SERVO_PIN );
}
void loop()
{
// if the servo has dropped the ball
// lets stop running...
if ( servoDrop == true ) return;
// break the current time into five minute blocks
// 1000 ms * 60 seconds in a minute * 5 for minutes
//int curentIncrementInFiveMinutes = millis() / (1000 * 60 * 5);
int curentIncrementInFiveMinutes = millis() / (1000 );
// check if the last increment is different from the one we're at now
// i.e. have we moved into a new block
if (lastIncrement != curentIncrementInFiveMinutes )
{
//do stepper stuff
int steps = 4;
// Move stepper 2 steps forward
stepStepper1( false, steps ); // false will be anticlockwise, true will be clockwise
// update the lastIncrement to the one that we have now
// for the next looop
lastIncrement = curentIncrementInFiveMinutes;
// update the counter for rotations
rotationCount += steps;
}
// Check if the rotation count is the same as the total
// if it is then step the hour counter
{
int steps = 2;
// do other stepper
stepStepper2( true, steps ); // false will be anticlockwise, true will be clockwise
rotationCount2 += steps;
}
// Check if the second rotation count is the same as the total
// then move the servo
if (rotationCount2 % totalRotation == 0)
{
// do the servo stuff...
servo.write( 180 );
delay( 3000 );
servo.write( 0 );
delay( 1000 );
// play music here
servoDrop = true;
}
delay( 50 );
}
void stepStepper1(boolean dir,int steps)
{
// If the direction is true - forwards
if( dir == true )
digitalWrite(STEPPER1_DIR_PIN, HIGH); // Set the power to HIGH and this sets the direction to clockwise
else
digitalWrite(STEPPER1_STEP_PIN, LOW); // otherwise set it to anticlockwise
// Wait for 50 milliseconds
delay(50);
// The driver makes a step as 8 microsteps
// Multiply the number of steps by 8 to get
// clean movement
steps = steps * MICROSTEPS;
// loop for each of the steps
for(int i=0;i<steps;i++){
// Set the power to High to begin movement
digitalWrite(STEPPER1_STEP_PIN, HIGH);
// Wait for a faction of a second
delayMicroseconds(1500);
// turn it off
digitalWrite(STEPPER1_STEP_PIN, LOW);
// Wait for a faction of a second
delayMicroseconds(1500);
// We have now moved one microstep.
// loop until they've all been made
}
}
void stepStepper2(boolean dir,int steps)
{
// If the direction is true - forwards
if( dir == true )
digitalWrite(STEPPER2_DIR_PIN, HIGH); // Set the power to HIGH and this sets the direction to clockwise
else
digitalWrite(STEPPER2_STEP_PIN, LOW); // otherwise set it to anticlockwise
// Wait for 50 milliseconds
delay(50);
// The driver makes a step as 8 microsteps
// Multiply the number of steps by 8 to get
// clean movement
steps = steps * MICROSTEPS;
// loop for each of the steps
for(int i=0;i<steps;i++){
// Set the power to High to begin movement
digitalWrite(STEPPER2_STEP_PIN, HIGH);
// Wait for a faction of a second
delayMicroseconds(1500);
// turn it off
digitalWrite(STEPPER2_STEP_PIN, LOW);
// Wait for a faction of a second
delayMicroseconds(1500);
// We have now moved one microstep.
// loop until they've all been made
}
}
Clock Fritzing Diagram.fzz (9.21 KB)