I am currently trying to add a clock functionality to my current sketch for a peristaltic pump [run by NEMA 23].

The current sketch waits for a button on my breadboard to be pressed, it then runs the motor for a specified amount of steps and then stops and waits for the next press of the button. It also displays the amount of steps to the Serial Monitor. I have a working sketch for a clock using an RTC DS3231 module and have unsuccessfuly attempted to fit it into my motor sketch. The problem is, the clock seems to be taking precedent over the rest of my sketch, so pressing the button no longer works and the time is simply consistently output onto the Serial Monitor.

The ideal functionality would be the time and date displayed on LCD constantly (or at least blinking so rapidly that one could not tell the difference) with the number of button presses displayed under it, changing in value only when a button is pressed.

Attatched find my code, it is really quite simple, and thank you so much for the time and help.

#include <Bounce2.h>
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <AccelStepper.h>
#include <MultiStepper.h>
#include <Wire.h>       //I2C Library
#include <OneWire.h>
#define DS3231_I2C_ADDRESS 0x68

AccelStepper stepper(1, 9, 8); // initiate stepper motor

LiquidCrystal lcd(6, 7, 5, 4, 3, 2);

int pos = 0; // variable to store position
int buttonPresses = 0;

const byte ButtonPin = 10;
const byte LedPin = 13; // for debugging and visualization

unsigned long ledOnTimer = 0;

const unsigned int ledPeriod = 3000;

Bounce button;

void setup() {
  lcd.begin(16, 2);
  pinMode(LedPin, OUTPUT);

byte bcdToDec(byte val)  {
  // Convert binary coded decimal to normal decimal numbers
  return ( (val / 16 * 10) + (val % 16) );

void loop() {

void motor() { // functioning motor sketch by itself

  //if led is on
  if (digitalRead(LedPin)) {
    stepper.runToNewPosition(21000); //about 420 steps per 10 mL
    digitalWrite(LedPin, LOW);
    // }

  //if led is off
  else {
    if (button.fell()) { // if the button "fell" from a value from HIGH to LOW (so now it is off)
      digitalWrite(LedPin, HIGH);
      buttonPresses++; // count presses of button and print to monitor
      Serial.print("Number of Button ");
      lcd.setCursor(0, 1);
      Serial.print("Presses = ");

void printDate() { //functioning time sketch by itself

  // Reset the register pointer

  byte zero = 0x00;

  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

  //  print the date EG   3/1/11 23:59:59
  Serial.print(" ");

Last note: Everything compiles, and I will change "Serial" to "lcd" after I finish debugging the sketch, so that is why everything is currently beign sent to the Serial Monitor.

3 Answers 3


Even if you want to display time with the seconds showing, you don't need to check the time on the RTC continiously. Once a second should be enough. You can use a timer interrupt that triggers an ISR(Interrupt Service Routine) every second to do that check for you. This will free a lot of time which your arduino can use to do other stuff.

Same goes for your button(s): you can use an External Interrupt (INT) or a Pin Change Interrupt (PCINT) for them. This way the arduino will only look at them when they are pushed (and/or released).

If you combine those two interrupts (for the clock and the button), your arduino will have a lot of spare time. This is especially handy if you later want to expand your project with more buttons and motors.


I think you need to look at using interrupts.

The printDate function is quite processor intensive with all the Wire and Serial.print calls, it will be taking the majority of the time in each loop(). Whilst in printDate it won't be looking at the state of the button, so you have to press at just the right time.

If you place an interrupt on the button's pin I think you will get a more responsive system. Another thing that you could look at it only reading the clock once a second, this could be done with a timer interrupt.


Attaching interrupt (PIN change or LEVEL change) to the push button would work fine. You can trigger a flag and the flag should access the controller to run motor() function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.