Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Analog Clock with Stepper
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Analog Clock with Stepper

by mitchbarbon on Fri Oct 16, 2015 8:45 am

HELLO!

I would like to make an analog clock with a twist. I am using a stepper since I would like to have a "random" progression of movements before the system tells the accurate time. And then a servo moves the clock face. I have the code working except for the time.

I all of my products are adadfruit including the ds1307 RTC. I have set up the time module so it is reading the time. I do not know how to take the time from the RTC, and turning that into a position on the stepper. I will only have on hand on the clock to make things easier, so it will be an hour hand.

Also, I purchased a motor shield v2, I have gotten examples to run on it, but have been unable to get a code like this work with the shield. Any Suggestions?

Here is the current code:

Code: Select all | TOGGLE FULL SIZE
#include <Stepper.h>
#include <Servo.h>

const int stepsPerRevolution = 200;

int pos = 135;    // variable to store the servo position

void setup() {
  myStepper.setSpeed(80);
  Serial.begin(9600);
  myservo.attach(6);
}

void loop() {
  Serial.println("counterclockwise");
  myStepper.step(-60);
  delay(500);
 
  Serial.println("clockwise");
  myStepper.step(250);
  delay(1);

  Serial.println("counterclockwise");
  myStepper.step(-300);
  delay(500);

  Serial.println("counterclockwise");
  myStepper.step(-170);
  delay(50);

  Serial.println("clockwise");
  myStepper.step(50);
  delay(1);

  Serial.println("counterclockwise");
  myStepper.step(-80);
  delay(20);

  Serial.println("clockwise");
  myStepper.step(210);
  delay(1);

  { 
  for(pos = 135; pos>=10; pos-=1)   
  {                               
    myservo.write(pos);             
    delay(1);                       
  }
    for(pos = 10; pos <= 135; pos += 1)
  {                             
    myservo.write(pos);             
    delay(50);                       
  }
  }
}



THANK YOU!
Last edited by adafruit_support_bill on Fri Oct 16, 2015 8:47 am, edited 1 time in total.
Reason: please use the </> button when submitting code. press </>, then paste your code between the [code] [/code] tags.

mitchbarbon
 
Posts: 4
Joined: Fri Oct 09, 2015 2:38 pm

Re: Analog Clock with Stepper

by adafruit_support_bill on Fri Oct 16, 2015 8:50 am

The Arduino 'Stepper' library does not support the Adafruit motor shield. You need to use the Adafruit motor shield library:
https://learn.adafruit.com/adafruit-mot ... ry-install
There are stepper example programs packaged with the library.

adafruit_support_bill
 
Posts: 77951
Joined: Sat Feb 07, 2009 10:11 am

Re: Analog Clock with Stepper

by mitchbarbon on Fri Oct 16, 2015 9:13 am

I was using the Motor shield libraries. I did get the stepper to work with it, I just could not modify the code successfully.

Can you help with the main issue of pulling the RTC time to get a position on a stepper motor?

Thank you

mitchbarbon
 
Posts: 4
Joined: Fri Oct 09, 2015 2:38 pm

Re: Analog Clock with Stepper

by adafruit_support_bill on Fri Oct 16, 2015 9:21 am

I don't understand the "current code" you posted then. It has no motor shield code or RTC code. The first thing you need to do is include both of those libraries.

adafruit_support_bill
 
Posts: 77951
Joined: Sat Feb 07, 2009 10:11 am

Re: Analog Clock with Stepper

by mitchbarbon on Fri Oct 16, 2015 9:34 am

I have the surest code not running off the motor shield. I am running it directly off of my arduino uno. I am sorry if my secondary question was confusing, but we can forget about the shield. I have the RTClib, but I am unable to successfully incorporate the library, or use it.
Attachments
Screen Shot 2015-10-16 at 9.32.58 AM.png
Screen Shot 2015-10-16 at 9.32.58 AM.png (282.16 KiB) Viewed 734 times

mitchbarbon
 
Posts: 4
Joined: Fri Oct 09, 2015 2:38 pm

Re: Analog Clock with Stepper

by adafruit_support_bill on Fri Oct 16, 2015 10:12 am

According to the error message we can see on the screen, the compiler is unable to locate wire.h. Wire.h is a standard part of the Arduino IDE install and is required by the motor shield as well. It is possible you have a corrupted IDE install. You can try re-installing the IDE.

adafruit_support_bill
 
Posts: 77951
Joined: Sat Feb 07, 2009 10:11 am

Re: Analog Clock with Stepper

by mitchbarbon on Fri Oct 16, 2015 10:30 am

I have made this the top of the code and it has at least verified.

If this is ok to use, how would I get the time, and translate that into stepper position?
Attachments
Screen Shot 2015-10-16 at 10.29.49 AM.png
Screen Shot 2015-10-16 at 10.29.49 AM.png (23.83 KiB) Viewed 724 times

mitchbarbon
 
Posts: 4
Joined: Fri Oct 09, 2015 2:38 pm

Re: Analog Clock with Stepper

by adafruit_support_bill on Fri Oct 16, 2015 11:04 am

The RTCLib has example code that shows you how to read hours, minutes, seconds etc. You are only interested in the hours.

Since your motor is a 200 step motor, each hour will be 200/12 steps for a 12 hour clock. That does not work out to an even step so you will need to round to the nearest full step. Assuming that you start counting at the top of the dial:

12:00 would be (200 * 0)/12 = 0 steps
1:00 would be (200 * 1)/12 = 16.6667 or 17 steps
2:00 would be (200 * 2)/12 = 33.333 or 33 steps
3:00 would be (200 * 3)/12 = 50 steps

etc...

adafruit_support_bill
 
Posts: 77951
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.