having a problem with my code where the motor will turn on at the specified time, but it wont turn off at all. i want my motor to spin for 10 seconds at two predetermined times throughout the day (930am and 530pm) currently the motor will turn on at those times but it will not turn off after 10 seconds has passed. The motor will continue to run until i reset the arduino. I am using a sainsmart arduino and a seeedstudio motorshield and a ds3231 RTC.
here is my code (i had an lcd i was using but had to take that off since it uses some of the pins that they motorshield is using.)
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
#include <TimeAlarms.h>
#include <Time.h>
RTC_DS1307 RTC;
//LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);
//int motorPin = 2;
int lastHour = 23;
int lastMinute = 59;
bool feedPet = false;
unsigned long feedTime = 0;
unsigned long lastPrintTime = 0;
struct FeedTime {
int hour, minute;
};
FeedTime amFeed = {9, 30}; // i.e. 9:30am
FeedTime pmFeed = {17, 30}; // i.e. 5:30pm
//sets up motor 1
int pinI1 = 8;
int pinI2 = 11;
int speedpin1 = 9;
//sets speed of motors
int speed = 255;
void setup ()
{
//pinMode(motorPin, OUTPUT);
Serial.begin(9600);
//lcd.begin(16, 2);
Wire.begin();
RTC.begin();
pinMode (pinI1, OUTPUT);
pinMode (pinI2, OUTPUT);
pinMode (speedpin1, OUTPUT);
/*{
lcd.println("RTC NOT Running!");
RTC.adjust(DateTime((__DATE__), (__TIME__)));
}*/
}
void loop ()
{
DateTime now = RTC.now();
FeedTime currentTime;
currentTime.hour = now.hour();
currentTime.minute = now.minute();
if ((currentTime.minute != lastMinute) && (((currentTime.hour == amFeed.hour) && (currentTime.minute == amFeed.minute)) || ((currentTime.hour == pmFeed.hour) && (currentTime.minute == pmFeed.minute))))
{
feedTime = millis();
feedPet = true;
}
lastMinute = currentTime.minute;
if (feedPet)
{
turnFeeder();
}
if (millis() - lastPrintTime > 1000UL)
{
/*lcd.setCursor(0, 0);
char nowDate[24] = "";
sprintf(nowDate, "DATE: %02d/%02d/%d", now.month(), now.day(), now.year());
lcd.print(nowDate);
// display the time
lcd.setCursor(0, 1);
char nowTime[24] = "";
sprintf(nowTime, "Time: %02d:%02d:%02d", now.hour(), now.minute(), now.second());
lcd.print(nowTime);
lastPrintTime = millis();*/
}
analogWrite(speedpin1, speed);
}
void turnFeeder(void)
{
static bool pinState = true;
if (millis() - feedTime < 1000UL) // 10 second(s)
{
if (pinState)
{
//digitalWrite(motorPin, HIGH); <<<<old way without motorshield
digitalWrite(pinI2, LOW);
pinState = false;
}
}
else
{
//digitalWrite(motorPin, LOW); <<<<old way without motorshield
digitalWrite(pinI1, HIGH);
pinState = true;
feedPet = false;
}
}