Hi, I'm trying to get the motor to stay on for 5 minutes, turn off and stay off for 1 hour, turn on again and last for 5 minutes and turn off again, so in cycle. but after a certain time it stops working or goes out and does not turn on again or lasts always, help!
below the code:
int pwm = 2;
//unsigned long t = 0;
int interprend = 5000;
int interejec = 8000;
// Tracks the time since last event fired
unsigned long conteo=0;
void setup() {
pinMode(2, OUTPUT);
Serial.begin(9600);
}
void loop() {
unsigned long tiempo = millis();
if ((unsigned long)(tiempo - conteo) <= interprend) {
digitalWrite(pwm,HIGH);
}else{
digitalWrite(pwm,LOW);
}
if ((unsigned long)(tiempo - conteo) >= interejec){
conteo = tiempo;
}
Serial.print("Tiempo en segundos ");
Serial.println(tiempo);
}
hpedraza724:
Hi, I'm trying to get the motor to stay on for 5 minutes, turn off and stay off for 1 hour, turn on again and last for 5 minutes and turn off again, so in cycle. but after a certain time it stops working or goes out and does not turn on again or lasts always, help!
below the code:
int pwm = 2;
//unsigned long t = 0;
int interprend = 5000;
int interejec = 8000;
// Tracks the time since last event fired
unsigned long conteo=0;
void setup() {
pinMode(2, OUTPUT);
Serial.begin(9600);
}
void loop() {
unsigned long tiempo = millis();
if ((unsigned long)(tiempo - conteo) <= interprend) {
digitalWrite(pwm,HIGH);
}else{
digitalWrite(pwm,LOW);
}
if ((unsigned long)(tiempo - conteo) >= interejec){
conteo = tiempo;
}
Serial.print("Tiempo en segundos ");
Serial.println(tiempo);
}
you code does not match what you say you want to do! anyhow try code below... should work fine:
int pwm = 2;
//unsigned long t = 0;
unsigned long ONTIME = 300000; //5min ON
unsigned long OFFTIME = 3600000; //1hr OFF
unsigned long oldtime = 0
// Tracks the time since last event fired
unsigned long conteo=0;
void setup() {
pinMode(2, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (millis() - oldtime > OFFTIME) {
digitalWrite(pwm,HIGH);
oldtime=millis();
}
else if (millis() - oldtime > ONTIME && digitalRead(pwm)==HIGH) {
digitalWrite(pwm,LOW);
oldtime=millis();
}
}