Assignments

Response

I interfaced an RTC module with stepper motor with the goal of the motor moving one step every minute and one revolution every 24 hours. This means it has to move at rate 200/1440 ( 0.13888888888) steps per minute. As the stepper motor does not seem to take a fraction of a step, I tried to figure out the speed settings for the motor and got stuck. My next attempt will be to use millis( ) or an easing function.

Questions

  1. Should delay( ) be used when trying to keep time accurately? I’m afraid it may throw off portions of the code if I don’t tie every part to the readings from the RTC.
  2. I am able to connect to RTC sometimes and unable at other times. I’ve tried different sketches but have consistently run into this problem.
  3. General, non-Time related question: I couldn’t connect to Arduino at all from my Mac. I tried three different boards (each a different Arduino version), 2 different cables, and 2 different laptops! Finally, I gave up and used my partner’s PC. This has happened before; I believe it’s a port issue as I am receiving an avrdude error. The connection issue comes and goes but has been happening a lot more frequently - any help is appreciated.

The stepper motor and RTC module working individually.

The stepper motor and RTC module working individually.

Wiring of stepper motor and RTC module with Arduino Nano

Wiring of stepper motor and RTC module with Arduino Nano

Motor’s default stepping speed completing 200 steps per revolution.

Motor’s default stepping speed completing 200 steps per revolution.

#include <Stepper.h>
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include "RTClib.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);
int stepCount = 0;         // number of steps the motor has taken

RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () {
  Serial.begin(57600);

#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    //Serial.flush();
    //abort();
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  } else {
    Serial.println("RTC has maintained time since last boot!");
  }

  // When time needs to be re-set on a previously configured device, the
  // following line sets the RTC to the date & time this sketch was compiled
  // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  // This line sets the RTC with an explicit date & time, for example to set
  // January 21, 2014 at 3am you would call:
  // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}

void loop () {
  Serial.println("in loop");

  //COMBINE STEPPER AND CLOCK
  int myStep;
  //200 steps per rev. 1440 minutes in a 24 hours
  int minutesInADay = 1440;
  //stepsPerRevolution = 200 is already above as global
  //map values?
  // for (i=0, i++, 1<=24){
  //   for (j=0, j++, j<=60){
  //     myStepper.goto(at the right place)
  //     myStepper.step()
  //   }
  // unsigned long currentMillis = millis();

  // if (currentMillis - previousMillis >= interval) {
  //   // save the last time you blinked the LED
  //   previousMillis = currentMillis;

  //   // if the LED is off turn it on and vice-versa:
  //   if (ledState == LOW) {
  //     ledState = HIGH;
  //   } else {
  //     ledState = LOW;
  //   }

  //STEPPER MOTOR ONLY, step one step
    //myStepper.setSpeed(15);
    myStepper.step(0.5);
    Serial.print("steps:");
    Serial.println(stepCount);
    stepCount++;
    //delay(500);

  //CLOCK ONLY
    DateTime now = rtc.now();

    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);

    Serial.println();
    delay(3000);
}