I regret having to ask for help for this 'cause I know it’s gonna be something stupid but I gotta get this resolved to move on. This sketch if for testing the electrical and mechanics for a solar tracker I’m building. The purpose is to have the motor driven device alternately rotate clockwise then counter-clockwise. The function (Drive_East) to energize a relay and illuminate an led indicator initiates but it doesn’t time out via "if (millis() - east_mark_time > 2000). The debugging lines in the serial monitor monitor show east_mark_time less than millis() by more than 2000 but it just stays in that function. I can’t seem to so will someone please spot the problem for me . Thanks - Scotty
byte east_limit_sense = 4;
byte west_limit_sense = 3;
byte relay_east = 9;
byte relay_west = 8;
byte east_limit_led = 7;
byte west_limit_led = 5;
byte east_drive_led = 10;
byte west_drive_led = 6;
byte not_dark_led = 11;
byte overcurrent_led = 12;
boolean east_limit;
boolean west_limit;
boolean east_state = false;
boolean west_state = false;
boolean in_pause = false;
boolean in_drive = false;
int Led_Pins[] = {
13,12,11,10,7,6,5};
unsigned long east_mark_time = 0;
unsigned long west_mark_time = 0;
unsigned long start_pause = 0;
void setup(){
digitalWrite (relay_east,LOW);
digitalWrite (relay_west,LOW);
Serial.begin(9600);
// Following 3 lines set pins 5 thru 13 to outputs
Serial.println ("In Setup");
for (byte pin=5;pin<14;pin++){
pinMode (pin, OUTPUT);
}
// Following 6 lines sequentially turn on leds for led check
for (byte led=0;led<7;led++){
digitalWrite (Led_Pins[led], HIGH);
Serial.print ("Led ");
Serial.print (Led_Pins[led]);
Serial.println (" is on");
delay(500);
}
delay(1000);//wait one second
// Following 2 lines turn the leds off after led check
for (byte led=0;led<8;led++){
digitalWrite (Led_Pins[led], LOW);
}
Serial.println ("Setup complete");
}
void Read_Sensors(){
Serial.println("In Read Sensors");
east_limit = digitalRead (east_limit_sense);
west_limit = digitalRead (west_limit_sense);
}
void Update_Indicators(){
digitalWrite (east_limit_led, east_limit);
digitalWrite (west_limit_led, west_limit);
Serial.println ("In Update_Indicators");
}
void Drive_East(){
in_drive = true;
Serial.println(" In Drive_East");
Serial.print(" in_drive = ");
Serial.println (in_drive);
digitalWrite (relay_east, HIGH);
digitalWrite (east_drive_led, HIGH);
// Next 2 lines: "if (! east_mark_time)" i.e.if east_mark_time is 0,
// then this timer has been reset and ready to start a new count
// so go ahead and set east_mark _time to the current time, (millis).
// If it's not 0, then the timer is already running
if (! east_mark_time){
east_mark_time = millis();
}
// Next line checks to see if the current time (millis) is greater
// than 1/2 second than when the counter (east_mark_time) was started
if (millis() - east_mark_time > 2000){
digitalWrite (relay_east, LOW);
digitalWrite (east_drive_led, LOW);
in_drive = false; // This flag is used by the function Drive_West
// to let that function know that this function is finished. This is to
// prevent either East_Drive or West_Drive functions from being on at
// the same time.
east_mark_time = 0;//resets counter for the next time this function is run.
Serial.println ("Out of In_Drive_East");
}
}
void Drive_West(){
in_drive = true;
Serial.println (" In Drive_West");
digitalWrite (relay_west,HIGH);
digitalWrite (west_drive_led, HIGH);
if (! west_mark_time){
west_mark_time = millis();
}
if (millis() - west_mark_time > 2000){
digitalWrite (relay_west, LOW);
digitalWrite (west_drive_led, LOW);
in_drive = false;
west_mark_time = 0;
Serial.println ("Out of In_Drive_West");
}
}
// The folloing function is a delay used before Drive_East or Drive_West
// functions to either East or West relays' contacts are open before
// closing the contacts of the counterpart's relay.
void Pause(){
if(!start_pause){
start_pause = millis();
in_pause = true;
Serial.println(" In Pause");
Serial.print(" millis = ");
Serial.println (millis());
Serial.print(" west_mark_time = ");
Serial.println (west_mark_time);
Serial.println(" In Pause");
Serial.print(" millis = ");
Serial.println (millis());
Serial.print(" east_mark_time = ");
Serial.println (east_mark_time);
Serial.print(" in_drive = ");
Serial.println (in_drive);
Serial.println(" Out of Pause");
}
if (millis() - start_pause > 1000){
in_pause = false;
start_pause = 0;
}
}
void loop(){
Read_Sensors();
Update_Indicators();
Serial.print(" millis = ");
Serial.println (millis());
Serial.print(" west_mark_time = ");
Serial.println(west_mark_time);
Serial.print(" east_mark_time = ");
Serial.println(east_mark_time);
if(!in_drive && !in_pause && !east_limit){
Drive_East();
}
if (!in_drive){
Pause();
}
if(!in_drive && !in_pause && !west_limit){
Drive_West();
}
if (!in_drive){
Pause();
}
//delay(2000);
Serial.println ("In loop");
}
Serial monitor:
In UpdýIn Setup
Led 13 is on
Led 12 is on
Led 11 is on
Led 10 is on
Led 7 is on
Led 6 is on
Led 5 is on
Setup complete
In Read Sensors
In Update_Indicators
millis = 4503
west_mark_time = 0
east_mark_time = 0
In Drive_East
in_drive = 1
In loop
In Read Sensors
In Update_Indicators
millis = 4736
west_mark_time = 0
east_mark_time = 4673
In loop
In Read Sensors
In Update_Indicators
millis = 4878
west_mark_time = 0
east_mark_time = 4673
In loop
In Read Sensors
In Update_Indicators
millis = 5020
west_mark_time = 0
east_mark_time = 4673
In loop
In Read Sensors
In Update_Indicators
millis = 5163
west_mark_time = 0
east_mark_time = 4673
In loop
In Read Sensors
In Update_Indicators
millis = 5305
west_mark_time = 0
east_mark_time = 4673
In loop
In Read Sensors
In Update_Indicators
millis = 5447
west_mark_time = 0
east_mark_time = 4673
In loop
In Read Sensors
In Update_Indicators
millis = 5590
west_mark_time = 0
east_mark_time = 4673
In loop
In Read Sensors
In Update_Indicators
millis = 5733
west_mark_time = 0
east_mark_time = 4673
In loop
In Read Sensors
In Update_Indicators
millis = 5875
west_mark_time = 0
east_mark_time = 4673
In loop
In Read Sensors
In Update_Indicators
millis = 6018
west_mark_time = 0
east_mark_time = 4673
In loop
In Read Sensors
In Update_Indicators
millis = 6160
west_mark_time = 0
east_mark_time = 4673
In loop
In Read Sensors
In Update_Indicators
millis = 6302
west_mark_time = 0
east_mark_time = 4673
In loop
In Read Sensors
In Update_Indicators
millis = 6445
west_mark_time = 0
east_mark_time = 4673
In loop
In Read Sensors
In Update_Indicators
millis = 6587
west_mark_time = 0
east_mark_time = 4673
In loop
In Read Sensors
In Update_Indicators
millis = 6730
west_mark_time = 0
east_mark_time = 4673
In loop
In Read Sensors
In Update_Indicators
millis = 6873
west_mark_time = 0
east_mark_time = 4673
In loop
In Read Sensors
In Update_Indicators
millis = 7015
west_mark_time = 0
east_mark_time = 4673
In loop