I have done my due diligence and searched to my utmost, but can't find anything close to my problem.
I have attached a "Fritzing" layout as well as a text file with the sketch. This runs a marble clock by waiting for the minute change, then starts the motor to turn an auger. The "beam break sensor" detects the delivered ball and stops the motor. I have tested individual components as "stand-alone" projects as well as combining all components for a final "product". Everything works just as it's supposed to...for a time. One time it may run 31 minutes and then just stop, next time 52 min., then 40 min...etc. There's no consistency to the length of time it may run. When I disconnect power (12v) and re-connect it, it starts running again just as it should until... I can't figure out what could be stopping the process. I am NOT electronics educated, and am just a hobbyist. Thanks for any help. (Can't get the fritzing diagram to upload)
The most likely problem is the motor starting/stopping is causing noise on the I2C bus which hangs the wire library. As pointed out, you are doing a TON of reading of the rtc and once the bus hangs, you are toast.
I'd suggest not reading so often (every second would be fast enough) and maybe not at all while the motors are running/stopping.
Add a variable in loop() to indicate if you are running or not and don't read during that time.
#include <DS3231_Simple.h>
#define SensorPin 4
DS3231_Simple Clock;
int sensorState = 0;
int lastState;
int OnMin;
//L293D
//Motor A
const int motorPin1 = 5; // Pin 14 of L293
const int motorPin2 = 6; // Pin 10 of L293
//Motor B
const int motorPin3 = 10; // Pin 7 of L293
const int motorPin4 = 9; // Pin 2 of L293
DateTime DT;
void setup() {
Serial.begin(9600);
Clock.begin();
pinMode (SensorPin, INPUT);
digitalWrite(SensorPin, HIGH);
DT = Clock.read();
OnMin = DT.Minute;
//Set pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
digitalWrite(motorPin3, HIGH);
/* DON'T forget to comment the clock setup out and then re-load the code. */
//// Set the clock.
//DT.Day = 07;
//DT.Month = 03;
//DT.Year = 20;
//DT.Hour = 9;
//DT.Minute = 23;
//DT.Second = 00;
//// Then write it to the clock
//Clock.write(DT);
/* DON'T forget to comment the clock setup out and then re-load the code. */
}
bool isRunning = false;
unsigned long lastReadTime;
const unsigned long readInterval = 1000;
void loop()
{
if ( isRunning == false && millis() - lastReadTime >= readInterval ) {
// we are not running and it has been long enough so
// Ask the clock for the data.
DT = Clock.read();
}
sensorState = digitalRead(SensorPin);
if (sensorState == LOW && lastState == HIGH) { // it's been broken, stop the motor
digitalWrite(motorPin1, LOW);
isRunning = false;
delay(1000); // let things settle down
}
lastState = sensorState;
if (DT.Minute != OnMin) {
Serial.println("New minute, Start Motor.");
OnMin = DT.Minute;
digitalWrite(motorPin1, HIGH);
isRunning = true;
}
}
Edit: also added in sensor state change since you only want to delay when it happens, not every time
Just being a "tinkerer" these are some great suggestions that I will incorporate in the sketch. I only started learning the Arduino and the language a couple weeks ago. I just retired and am getting a hobby that I can do at home, this fit the bill. Again, thanks for all the suggestions and the fix for the minute worked great.