Hi guys,
So long story short I'm controlling this apparatus with a motor so that it will turn on a heater at specific times a day. It needs to turn on for a minute at a time, every half hour, from 9 am to 5 pm. I am using an RTC module to keep time. The temperature needs to get up to 40 degrees C and no higher.
However, when I tested it to see if it would run the motor at the correct time it did not work. There are no actual errors in the code (it uploads fine) but something is wrong with it that is making it not turn on. I have attached the relevant parts of my code (but let me know if you need to see any additional lines).
Any help would be greatly appreciated! thanks!
/TMP36 Pin Variables
int sensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
//the resolution is 10 mV / degree centigrade with a
//500 mV offset to allow for negative temperatures
tmElements_t
tm;
#define ON true
#define OFF false
const byte pinLED = LED_BUILTIN;
const unsigned long culOdorDispenseTime = 120000ul; //2 minutes
byte
last_minute,
last_hour;
bool
bOdorState,
bApparatusState;
unsigned long
timeOdor;
void setMotorSpeed(int speed)
{
if (speed < 0)
{
smcSerial.write(0x86); // motor reverse command
speed = -speed; // make speed positive
}
else
{
smcSerial.write(0x85); // motor forward command
}
smcSerial.write(speed & 0x1F);
smcSerial.write(speed >> 5 & 0x7F);
}
Wire.begin();
Serial.begin(9600);
//pin setups as needed
Apparatus( OFF );
Odor( OFF );
//check if the RTC can be read; if not, send message and stop
if( RTC.read(tm) )
{
last_minute = tm.Minute;
last_hour = tm.Hour;
}//if
else
HaltandCatchFire();
pinMode(8, OUTPUT);
//motor controller
smcSerial.begin(19200);
delay(5);
smcSerial.write(0xAA);
exitSafeStart();
}
void HaltandCatchFire( void )
{
Apparatus( OFF );
Odor( OFF );
while( true )
{
//halt and catch fire; no need for fancy timing here; just blink LED rapidly to indicate fault
digitalWrite( pinLED, HIGH );
delay( 300 );
digitalWrite( pinLED, LOW );
delay( 300 );
Serial.println( "Failed reading RTC." );
}
}
void loop() {
//motor controller
setMotorSpeed(3200); // full-speed forward
delay(1000);
setMotorSpeed(-3200); // full-speed reverse
delay(1000);
static unsigned long
timeReadRTC = 0;
//read the RTC once every 500mS
if( (millis() - timeReadRTC) > 500 )
{
//set up for read 500mS from now
timeReadRTC = millis();
//get the time
if( RTC.read(tm) )
{
//if the apparatus is off now
if( bApparatusState == OFF )
{
//check if time to turn on
if( tm.Hour >= 9 && tm.Hour <= 18 )
{
Apparatus( ON );
}//turn on if in right times
if(temperatureC < 40 )
{
Apparatus( ON );
}//turn on if temp is too low
else
{
if( temperatureC > 40 )
{
Apparatus( OFF );
}
} //turn off if temp is too high
}//if
else
{
//is on now; time to turn off?
if( tm.Hour < 9 || tm.Hour > 18 )
{
//turn off the apparatus
Apparatus( OFF );
//turn off odor release
Odor( OFF );
} //everything off in these times
}
//check if time for odor release (!)
// - apparatus turned on
// - minute just turned 0 or 30
if( (bApparatusState == ON) && (tm.Minute != last_minute) && (tm.Minute == 0 || tm.Minute == 30) )
{
//save the minute so we don't turn on the odor for the entire duration of 0 or 30
last_minute = tm.Minute;
//turn on
Odor( ON );
}
}
else
//if we can't read the time, shut everything down (?)
HaltandCatchFire();
}
//check if odor on; if so, turn it off when time appropriate
if( bOdorState == ON )
{
if( (millis() - timeOdor) >= culOdorDispenseTime )
Odor( OFF );
}
}
void Apparatus( bool State )
{
//asking for on?
if( ON )
{
setMotorSpeed(3200); // full-speed forward
delay(1000);
bApparatusState = ON;
}//if
else
{
setMotorSpeed(-3200); // full-speed reverse
delay(1000);
bApparatusState = OFF;
}//else
}//Apparatus
void Odor( bool State )
{
//asing for on?
if( State )
{
digitalWrite(8, HIGH);
delay(1000);
timeOdor = millis();
bOdorState = ON;
}//if
else
{
digitalWrite(8, LOW);
delay(1000);
bOdorState = OFF;
}
} //odor