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);
}
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);
}
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.
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.
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