I'm having trouble figuring out an approach to this. I need a relay to turn on for x seconds every y mins. For some reason this is proving to very difficult. Maybe I'm just having an off day. Any help or examples would be greatly appreciated.
(deleted)
This is what i have so far. I've tried messing with some of the timer libraries but didn't have any luck figuring those out. Im still not sure what the exact times need to be yet so the times here are just for testing. basically i need a pump to fill a bucket every 'y' mins, it takes the pump 'flowtime' seconds to fill the bucket.
#include <SimpleTimer.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int relay = 13;
int flowtime = 0;
int waittime = 10;
void flow() {
flowtime = 0;
while(flowtime < 5) {
flowtime = millis() / 1000;
digitalWrite(relay, HIGH);
}
}
void setup() {
lcd.begin(16,2);
Serial.begin(9600);
pinMode(13, OUTPUT);
lcd.print("Running Time: ");
}
void loop() {
lcd.setCursor(14,0);
int timepass = millis() / 1000;
lcd.print(timepass);
delay(15);
if(timepass % waittime == 0) {
flow();
}else {
digitalWrite(relay, LOW);
}
}
I'm having trouble figuring out an approach to this. I need a relay to turn on for x seconds every y mins. For some reason this is proving to very difficult. Maybe I'm just having an off day. Any help or examples would be greatly appreciated.
Do you have a real time clock module ? or you just want to do it while you turn your components power ON?
No i dont have a rtc module. But i dont mind keeping the arduino powered on all the time so i was hoping i could just have it constantly counting.
millis() is a timer that runs continuously. The Blink without Delay example in the IDE does exactly what you ask, except blinks an LED instead of emptying a bucket. No timer libraries required.
I need a relay to turn on for x seconds every y mins.
Well I can give you an example, because you don't have clock module you can build your own one in code and letting up arduino / chip but while you close it you need to define again the time. Like a clock that you get out the battery put it back and the time is reseting.
So this is the code I wrote right now, theotericaly should work, i didn't tested it.
// Domino60
// set your rime here
int hours = 0; // set hours
int minits = 0; // set minit
int seconds = 0; // set seconds
// not really need but it add them as example
// add your necesary seconds x_seconds
int x_seconds = 10000; // turn relay for x seconds , 10000ms = 10seconds
int y_minits; // every y minits
void setup(){
}
unsigned long now = 0;
void time_(){ // real time clock function
if(millis() - now >= 1000){ //runs this funtion every second
seconds++;
if(seconds == 60){
minits++;
seconds = 0;
}
if(minits == 60){
hours++;
minits = 0;
seconds = 0;
}
now = millis();
}
}
unsigned long now_2;
void relay(){ // relay function turn ON for x seconds every y minits
if(y_minits == 0 || y_minits == 30){ // Turn on the relay every 30minits
if(millis() - now_2 == x_seconds){
// RUN YOUR RELAY ON
}
else{
// RELAY OFF
}
now_2 = millis();
}
}
void loop(){
time_();
relay();
}
// Domino60
So I tested the code it's working here is full example of real time clock, just remember to keep your arduino on if you reset it or turn the power off, you need to set the time again.
Added some serial to see it in your serial monitor.
It's a 24 hours real time clock by Domino60 ![]()
// 24 hours Real time clock by Domino60
// set your rime here
int hours = 24; // set hours
int minits = 59; // set minit
int seconds = 50; // set seconds
// not really need but it add them as example
// add your necesary seconds x_seconds
int x_seconds = 10000; // turn relay for x seconds , 10000ms = 10seconds
int y_minits; // every y minits
void setup(){
Serial.begin(9600);
}
unsigned long now = 0;
void time_(){ // real time clock function
if(millis() - now >= 1000){ //runs this funtion every second
seconds++;
if(seconds == 60){
minits++;
seconds = 0;
}
if(minits == 60){
hours++;
minits = 0;
seconds = 0;
}
if(hours >24){
hours = 0;
}
now = millis();
Serial.print(hours);
Serial.print(":");
Serial.print(minits);
Serial.print(":");
Serial.println(seconds);
}
}
unsigned long now_2;
void relay(){ // relay function turn ON for x seconds every y minits
if(y_minits == 0 || y_minits == 30){ //
if(millis() - now_2 == x_seconds){
// RUN YOUR RELAY ON
}
else{
// RELAY OFF
}
now_2 = millis();
}
}
void loop(){
time_();
relay();
}
// Real time clock by Domino60
D.60