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() {
Wire.begin();
Serial.begin(9600);
lcd.begin(16, 2);
button.attach(ButtonPin);
pinMode(LedPin, OUTPUT);
stepper.setMaxSpeed(4000);
stepper.setSpeed(2000);
stepper.setCurrentPosition(0);
}
byte bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal numbers
return ( (val / 16 * 10) + (val % 16) );
}
void loop() {
motor();
printDate();
}
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 {
button.update();
if (button.fell()) { // if the button "fell" from a value from HIGH to LOW (so now it is off)
stepper.setCurrentPosition(0);
digitalWrite(LedPin, HIGH);
buttonPresses++; // count presses of button and print to monitor
lcd.clear();
Serial.print("Number of Button ");
lcd.setCursor(0, 1);
Serial.print("Presses = ");
Serial.println(buttonPresses);
}
}
}
void printDate() { //functioning time sketch by itself
// Reset the register pointer
Wire.beginTransmission(DS3231_I2C_ADDRESS);
byte zero = 0x00;
Wire.write(zero);
Wire.endTransmission();
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(month);
Serial.print("/");
Serial.print(monthDay);
Serial.print("/");
Serial.print(year);
Serial.print(" ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);
}
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.