Relay control at specific timing

Hi,
I am trying to switch On a relayfor 2 minutes from the program start and after 1 minute switch Off and again On for 2 minutes so on.. I am doing this with MCP3551 library "https://github.com/Seimen/CtrlHV/tree/master/MCP3551". But when I try to add any timer libray, the ralay is not working(I think there is a clash between these two library timings). But metro library working fine if I am not adding the MCP3551 library. So i planned to write my own code based on BlinkWithoutDelay example. But it didn't work. Can anybody point out what I am doing wrong or any alternate solution. Please find my code below..

const int ledPin =  13;      // the number of the LED pin
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;       
unsigned long A = 2000;           // interval at which to blink (milliseconds)
unsigned long B = 1000;
unsigned long C;
unsigned long Time2;
unsigned long Time3;
void setup() {
  pinMode(ledPin, OUTPUT);      
  Serial.begin(9600);
}

void loop()
{

  C=A+B;
  unsigned long currentMillis = millis();

  if(currentMillis <= A) {
    digitalWrite(13, HIGH);
    Serial.print("A)");
    Serial.print(A);
    Serial.print("----");
  }
  else if (currentMillis <=C)
  {
    digitalWrite(13, LOW);
  }
  else if (currentMillis > C)
  {  
    A=C+A;
    Serial.print("A2)");
    Serial.print(A);
    Serial.print("----");
  }
  //delay(1000);
}
Can anybody point out what I am doing wrong

Possibly several things, but we'll start with these:

unsigned long A = 2000;           // interval at which to blink (milliseconds)
unsigned long B = 1000;
unsigned long C;

These names don't mean squat. All upper case names are, by convention, reserved for constants. These are not constants.

Give these variables meaningful names, so that:

  C=A+B;

means something.

Dear Paul,
Thank you very much for the quick response. Please find below the changed code. That's the best name I can come up with(English is not my mother language, sorry). Now it blinked two threetimes and styed high for long time and blinked..

const int relayPin =  13;      // the number of the LED pin
long previousMillis = 0;       
unsigned long on_time = 2000;          
unsigned long off_time = 1000;
unsigned long sum_onoff_time;
unsigned long Time2;
unsigned long Time3;
void setup() {
  pinMode(relayPin, OUTPUT);      
  Serial.begin(9600);
}

void loop()
{

  sum_onoff_time=on_time + off_time;

  unsigned long currentMillis = millis();

  if(currentMillis <= on_time) {
    digitalWrite(relayPin, HIGH);
    Serial.print("Initial on Time-");
    Serial.print(on_time);
    Serial.print("--");
  }
  else if (currentMillis <=sum_onoff_time)
  {
    digitalWrite(relayPin, LOW);
  }
  else if (currentMillis > sum_onoff_time)
  {  
    on_time=sum_onoff_time+on_time;
    Serial.print("Second On Time)");
    Serial.print(on_time);
    Serial.print("----");
  }
  //delay(1000);
}
  if(currentMillis <= on_time) {

mills returns (ignoring rollover) the number of mills since your program started. So the above code will be true only once. Take another look at blink without delay.

Mark

holmes4:
mills returns (ignoring rollover) the number of mills since your program started. So the above code will be true only once. Take another look at blink without delay.

Mark

That was my first thought, too. But, on_time is modified later in the sketch.

  sum_onoff_time=on_time + off_time;

Why does this need to happen every time through loop()? Do it once when on_time or off_time is modified.

Put every { on a new line, so your code is consistent.

This is because I wanted to save that values in EEPROM and increment and decrement that values using buttons(I wanted to adjust the relay ON/OFF time using UP/DOWN buttons). So this has to be in the loop. I am away from my UNO now. After 30 minutes, I will update serial output in next post, so you great people will get more clear picture as well as my complete code

Attached file is my serial output for about a minute. Also you may find my updated code below ..

Updated Code..

#include <EEPROM.h>
const int relayPin =  13;  
const int upPin =  3;  
const int downPin =  4;  
long previousMillis = 0;       
unsigned long on_time = 2000;          
unsigned long off_time = 1000;
unsigned long sum_onoff_time;
void setup() {
  pinMode(relayPin, OUTPUT);      
  Serial.begin(9600);
  on_time = EEPROM.read(0);
  off_time = EEPROM.read(1);
}

void loop()
{

  sum_onoff_time=on_time + off_time;

  unsigned long currentMillis = millis();

  if(currentMillis <= on_time) {
    digitalWrite(relayPin, HIGH);
    Serial.print("Initial on Time-");
    Serial.print(on_time);
    Serial.print("--");
  }
  else if (currentMillis <=sum_onoff_time) 
  {
    digitalWrite(relayPin, LOW);
  }
  else if (currentMillis > sum_onoff_time)
  {  
    on_time=sum_onoff_time+on_time;
    Serial.print("Second On Time)");
    Serial.print(on_time);
    Serial.print("----");
  }
  {   
    if(digitalRead(upPin)==HIGH)                                            
    { 
      on_time+=1; 

    }                                                                         
    EEPROM.write(0, on_time);    
    if(digitalRead(downPin)==HIGH)                                             
    {                                                                         
      off_time+=1;  
    }   
    EEPROM.write(1,off_time);  
    //delay(1000);
  }
}

Serial Output.txt (46 KB)