I am creating a sketch for an Attiny85 to run a fan for 60 seconds every 30 minutes. While the fan is running (relay connected to Pin 0) I want an LED (connected to Pin 1) to flash.
Although my current sketch isn't set to 60 seconds every 30 minutes, the fan operation is working as expected. (timing set shorter for development / testing purposes)
I cannot get the millis() loop and logic working correctly to flash the LED only while the relay is on.
Can I please have some assistance getting this code right. Currently the LED isn't flashing & it's in reverse. ie: on solid while the fan is off.
const long LED= 1;
const long RELAY= 0;
const long FANINTERVAL= 5000;
const long FANONTIME= 2000;
const long LedonDuration= 250;
const long LedoffDuration= 750;
int ledon = 0;
int fanon = 0;
long ledtime= 0;
void setup(void) {
pinMode(LED,OUTPUT);
pinMode(RELAY,OUTPUT);
digitalWrite(LED,LOW);
digitalWrite(RELAY,LOW);
}
void loop(){
static uint32_t fanintervaltime= millis();
static uint32_t fanstarttime= millis();
// FAN START
if ( (millis()- fanintervaltime) > FANINTERVAL) {
fanintervaltime= millis();
fanstarttime= millis();
ledtime=millis();
fanon= 1;
digitalWrite(RELAY,HIGH);
//digitalWrite(LED,HIGH);
}
// FAN STOP
if ( (millis()- fanstarttime) > FANONTIME) {
digitalWrite(RELAY,LOW);
//digitalWrite(LED,LOW);
fanon= 0;
}
// LED FLASH WHILE FAN ON
if( fanon == 1 ) {
if( (millis()- ledtime) >= LedonDuration){
digitalWrite(LED,LOW);// change the state of LED
ledtime=millis();// remember Current millis() time
}
}
else {
if( (millis()- ledtime) >= LedoffDuration){
digitalWrite(LED,HIGH);// change the state of LED
ledtime=millis();// remember Current millis() time
}
}
}
...so the off part is only in the if( fanon == 1 ) part and the on part is only in the else part where fanon == 1 is not true. Maybe they both belong in the same part.
Thanks for the pointer. You were correct. I have tweaked the code and got it working now. But oddly, it will only work correctly if LedonDuration & LedoffDuration are the same. If I set on to 250 and off to 500 it only flashes once.
const long LED= 1;
const long RELAY= 0;
const long FANINTERVAL= 20000;
const long FANONTIME= 5000;
const long LedonDuration= 250;
const long LedoffDuration= 250;
int ledon = 0;
int fanon = 0;
long ledtime= 0;
void setup(void) {
pinMode(LED,OUTPUT);
pinMode(RELAY,OUTPUT);
digitalWrite(LED,LOW);
digitalWrite(RELAY,LOW);
}
void loop(){
static uint32_t fanintervaltime= millis();
static uint32_t fanstarttime= millis();
// FAN START
if ( (millis()- fanintervaltime) > FANINTERVAL) {
fanintervaltime= millis();
fanstarttime= millis();
digitalWrite(RELAY,HIGH);
fanon= 1;
digitalWrite(LED,HIGH);// change the state of LED
ledtime=millis();
}
// FAN STOP
if ( (millis()- fanstarttime) > FANONTIME) {
digitalWrite(RELAY,LOW);
digitalWrite(LED,LOW);
fanon= 0;
}
// LED FLASH WHILE FAN ON
if( fanon == 1 ) {
if( (millis()- ledtime) >= LedonDuration){
digitalWrite(LED,LOW);// change the state of LED
ledtime=millis();// remember Current millis() time
}
if( (millis()- ledtime) >= LedoffDuration){
digitalWrite(LED,HIGH);// change the state of LED
ledtime=millis();// remember Current millis() time
}
}
}
You are programmer enough to get 80% there, and that's more than 80% of people. It's always the last 20% where it gets tricky. And where you learn stuff.
One trick that helps in figuring out what is going on is adding print statements. Like:
const long LED = 1;
const long RELAY = 0;
const long FANINTERVAL = 20000;
const long FANONTIME = 5000;
const long LedonDuration = 250;
const long LedoffDuration = 250;
int ledon = 0;
int fanon = 0;
long ledtime = 0;
void setup(void) {
pinMode(LED, OUTPUT);
pinMode(RELAY, OUTPUT);
digitalWrite(LED, LOW);
digitalWrite(RELAY, LOW);
Serial.begin(115200);
}
void loop() {
static uint32_t fanintervaltime = millis();
static uint32_t fanstarttime = millis();
// FAN START
if ( (millis() - fanintervaltime) > FANINTERVAL) {
fanintervaltime = millis();
fanstarttime = millis();
Serial.print("F");
digitalWrite(RELAY, HIGH);
fanon = 1;
Serial.print("B");
digitalWrite(LED, HIGH); // change the state of LED
ledtime = millis();
}
// FAN STOP
if ((millis() - fanstarttime) > FANONTIME) {
Serial.print("f");
digitalWrite(RELAY, LOW);
digitalWrite(LED, LOW);
fanon = 0;
}
// LED FLASH WHILE FAN ON
if ( fanon == 1 ) {
if ( (millis() - ledtime) >= LedonDuration) {
Serial.print("l");
digitalWrite(LED, LOW); // change the state of LED
ledtime = millis(); // remember Current millis() time
}
if ( (millis() - ledtime) >= LedoffDuration) {
Serial.print("L");
digitalWrite(LED, HIGH); // change the state of LED
ledtime = millis(); // remember Current millis() time
}
}
}
Where you could make the output less frantic with: