Changing them to 50 slows the time it takes to dim but not ramp up. Also the second strip of leds still flickers like the first set before the fix.
steve43:
Changing them to 50 slows the time it takes to dim but not ramp up. Also the second strip of leds still flickers like the first set before the fix.
it may just appear that way, make it longer like 100 or 200 and see.
you haven't debugged the other Led yet!
steve43:
Changing them to 50 slows the time it takes to dim but not ramp up. Also the second strip of leds still flickers like the first set before the fix.
Ha!
Same error on line 41... should be:
if((millis() - flowTimer > TIMEOUT) && ledSet[0].setPoint() == 255)
so all together:
#include "Fade.h"
#define TIMEOUT 1000
const byte led2 = 10; // Shower Led
const byte led1 = 9; // Led connect to gate
const byte pir = A1; // PIR signal out
const byte flow = A0;
Fade ledSet[] = { // dimming pin, millis between steps
{led1, 10},
{led2, 10}}
;
unsigned long flowTimer = 0;
unsigned long pirTimer = 0;
void setup()
{
Serial.begin(9600);
for(byte i = 0; i < sizeof(ledSet)/sizeof(ledSet[0]); i++)
{
ledSet[i].begin();
ledSet[i].write(0);
}
pinMode(pir, INPUT);
pinMode(flow, INPUT);
}
void loop()
{
for(byte i = 0; i < sizeof(ledSet)/sizeof(ledSet[0]); i++)
{
ledSet[i].update();
}
if(checkFlow())
{
ledSet[0].write(255);
flowTimer = millis();
}
if((millis() - flowTimer > TIMEOUT) && ledSet[0].setPoint() == 255)
{
ledSet[0].write(0);
Serial.println(F("Flow Timer Expired..."));
}
if(checkMotion())
{
ledSet[1].write(255);
Serial.print(F("Set to max brite at "));
Serial.println(millis());
pirTimer = millis();
}
if((millis() - pirTimer > TIMEOUT) && ledSet[1].setPoint() == 255)
{
ledSet[1].write(0);
Serial.println(F("Motion Timer Expired..."));
Serial.print(F("Set to zero at "));
Serial.println(millis());
}
}
bool checkFlow()
{
static bool flowState = LOW;
bool state = digitalRead(flow);
if(flowState != state)
{
if(state == HIGH)
{
Serial.println(F("Flow Sensor Triggered..."));
return true;
}
flowState = state;
}
return false;
}
bool checkMotion()
{
static bool motionState = LOW;
bool state = digitalRead(pir);
if(motionState != state)
{
if(state == HIGH)
{
Serial.println(F("Motion Sensor Triggered..."));
return true;
}
motionState = state;
}
return false;
}
Second set of leds work once and displays this below but does not stay on for the set time and then dims down but wont start again and the led just flickers rapidly.
Flow Sensor Triggered...
Flow Sensor Triggered...
Flow Sensor Triggered...
Flow Sensor Triggered...
Flow Sensor Triggered...
Flow Sensor Triggered...
Flow Sensor Triggered...
Flow Timer Expired...
Flow Sensor Triggered...
Flow Timer Expired...
Flow Sensor Triggered...
Flow Timer Expired...
Flow Sensor Triggered...
Flow Timer Expired...
Flow Sensor Triggered...
Flow Timer Expired..
your flow sensor looks bouncy... you can see it is changing state frequently, not like the PIR at all
How do you want it to work, considering that it is changing states so often?
you can ignore the additional pulses after the detection of the first while the leds are on, for example.
I have tested both sets of leds and they both work as i want them to perfectly but the ramp up time is to slow i want them to brighten up quicker
Just play with the second values in the constructor until you get the fade speed you like. Smaller is faster.
Persistence of Vision affects your 'perceived' ramp up/ramp down time... on a linear fade like this one.
We cant find any other values that can be changed other than the 255 which is max brightness to 0 which is off which line is to be changed? Also i need a relay to come on when the flow switch is activated (same time as the led) can this be done easily
steve43:
We cant find any other values that can be changed other than the 255 which is max brightness to 0 which is off which line is to be changed? Also i need a relay to come on when the flow switch is activated (same time as the led) can this be done easily
As I pointed out above, change the 10 in the constructors to a different number, that affects the speed.
you can easily add a relay to the code, insert the function to turn it on right under the function to set the led target value to 255
if(checkMotion())
{
ledSet[1].write(255);
relayON();
Serial.print(F("Set to max brite at "));
Serial.println(millis());
pirTimer = millis();
}
Thanks very much for your help much appreciated me and my son will have a play around with adding the code for the relay tomorrow but again thanks very much.
Great.
Plus I can teach you to dynamically change the fade rate if you want the appearance of a faster/slower ramp up or ramp down.
![]()
Yes that would be great. Hopefully my son and i can get the relay to work.
Ok i got the relay to work were i wont it
really i need to adjust the ramp up speed, its too slow,but the ramp down is ok.
adjusting {led1, 10} sets the speed for ramp up and down. i need to be able to adjust ramp up independently from ramp down
is this possible.
also is it possible to add a line to keep the pir pin high when the flow switch is activated
this is to keep the bathroom leds on when your in the shower as the bathroom leds go out because the pir cant detect movement when in the shower. in other words the bathroom lights come on when walk in to the bathroom, the shower led comes on when the shower is started, the bathroom lights stay on, then shower off, shower led off, leave bathroom, bathroom lights off.but at the moment all this above works but bathroom lights go off when in shower until i come out of shower the bathroom lights come back on.
i hope you can understand what i am trying to do.
Thanks again
steve
steve43:
adjusting {led1, 10} sets the speed for ramp up and down. i need to be able to adjust ramp up independently from ramp down
you can call the writeSpeed() method before you set the target set point like this:
if(checkMotion())
{
ledSet[1].writeSpeed(50); // Sets the speed of fade to 10ms per increment <<<<<<<<<<<<
ledSet[1].write(255);
Serial.print(F("Set to max brite at "));
Serial.println(millis());
pirTimer = millis();
}
if((millis() - pirTimer > TIMEOUT) && ledSet[1].setPoint() == 255)
{
ledSet[1].writeSpeed(250); // Sets the speed of fade to 250ms per increment<<<<<<<<<<
ledSet[1].write(0);
Serial.println(F("Motion Timer Expired..."));
Serial.print(F("Set to zero at "));
Regarding the lights, yes you can do what you want... think in terms of states:
IDLE = No Motion, No Flow No Lights on
BATH = Motion, No Flow Bath Lights On
Shower = Flow, No Motion Shower and Bath Lights on
Then, develop your sensor to respond based on its three states...
steve43:
also is it possible to add a line to keep the pir pin high when the flow switch is activated
this is to keep the bathroom leds on when your in the shower as the bathroom leds go out because the pir cant detect movement when in the shower. in other words the bathroom lights come on when walk in to the bathroom, the shower led comes on when the shower is started, the bathroom lights stay on, then shower off, shower led off, leave bathroom, bathroom lights off.but at the moment all this above works but bathroom lights go off when in shower until i come out of shower the bathroom lights come back on.
Something like this:
#include "Fade.h"
#define TIMEOUT 10000
enum BathState{
EMPTY,
BATH_OCCUPIED,
SHOWER_OCCUPIED
};
const char* const stateText[] = {"Empty", "Bath Occupied", "Shower Occupied"};
const byte led2 = 10; // Shower Led
const byte led1 = 9; // Led connect to gate
const byte pir = A1; // PIR signal out
const byte flow = A0;
Fade ledSet[] = { // dimming pin, millis between steps
{led1, 10},
{led2, 10}}
;
BathState bathState = EMPTY;
BathState lastState = BATH_OCCUPIED;
unsigned long flowTimer = 0;
unsigned long pirTimer = 0;
void setup()
{
Serial.begin(9600);
for(byte i = 0; i < sizeof(ledSet)/sizeof(ledSet[0]); i++)
{
ledSet[i].begin();
ledSet[i].write(0);
}
pinMode(pir, INPUT);
pinMode(flow, INPUT_PULLUP);
}
void loop()
{
for(byte i = 0; i < sizeof(ledSet)/sizeof(ledSet[0]); i++)
{
ledSet[i].update();
}
switch (bathState)
{
case EMPTY:
if(checkMotion())
{
ledSet[1].write(255);
Serial.print(F("Set to max brite at "));
Serial.println(millis());
pirTimer = millis();
bathState = BATH_OCCUPIED;
}
else if(ledSet[1].read() == 255) // if it just came out of BATH_OCCUPIED state
{
ledSet[1].write(0);
}
break;
case BATH_OCCUPIED:
if(checkFlow())
{
ledSet[0].write(255);
flowTimer = millis();
bathState = SHOWER_OCCUPIED;
}
else if(ledSet[0].read() == 255) // if it just came out of SHOWER_OCCUPIED state
{
ledSet[0].write(0);
}
if((millis() - pirTimer > TIMEOUT))
{
ledSet[1].write(0);
Serial.println(F("Motion Timer Expired..."));
Serial.print(F("Set to zero at "));
Serial.println(millis());
bathState = EMPTY;
}
if(checkMotion())
{
pirTimer = millis(); // as long as motion is detected, prevent the timeout by updating the timer
}
break;
case SHOWER_OCCUPIED:
if((millis() - flowTimer > TIMEOUT))
{
ledSet[0].write(0);
Serial.println(F("Flow Timer Expired..."));
bathState = BATH_OCCUPIED;
}
if(checkFlow()) // as long as the shower is running, prevent the timeout by updating the timer
{
flowTimer = millis();
}
break;
}
if(bathState != lastState)
{
Serial.print(F("New Bath State is "));
Serial.println(stateText[bathState]);
lastState = bathState;
}
}
bool checkFlow()
{
static bool flowState = LOW;
bool state = digitalRead(flow);
if(flowState != state)
{
if(state == HIGH)
{
Serial.println(F("Flow Sensor Triggered..."));
return true;
}
flowState = state;
}
return false;
}
bool checkMotion()
{
static bool motionState = LOW;
bool state = digitalRead(pir);
if(motionState != state)
{
if(state == HIGH)
{
Serial.println(F("Motion Sensor Triggered..."));
return true;
}
motionState = state;
}
return false;
}
OK
Thanks again
i have added the line of code but the led still on fade up slow if i put in a lower number or high it still only fade up slow. what would like is both leds to have a fast ramp up and slow ramp down.
But other than that it works great
maybe post your final code so I can see what you mean by ramp up different than ramp down...
they are the same function!
Hi
ok here is my sketch
#include "Fade.h"
#define TIMEOUT 100
enum BathState{
EMPTY,
BATH_OCCUPIED,
SHOWER_OCCUPIED
};
const char* const stateText[] = {"Empty", "Bath Occupied", "Shower Occupied"};
const byte led2 = 10; // Shower Led
const byte led1 = 9; // Led connect to gate
const byte pir = A1; // PIR signal out
const byte flow = A0;
const byte relay = 11;
Fade ledSet[] = { // dimming pin, millis between steps
{led1, 10},
{led2, 10}}
;
BathState bathState = EMPTY;
BathState lastState = BATH_OCCUPIED;
unsigned long flowTimer = 0;
unsigned long pirTimer = 0;
void setup()
{
digitalWrite(relay,HIGH); // set pin HIGH first
Serial.begin(9600);
for(byte i = 0; i < sizeof(ledSet)/sizeof(ledSet[0]); i++)
{
ledSet*.begin();*
_ ledSet*.write(0);_
_ }_
_ pinMode(pir, INPUT);_
pinMode(flow, INPUT_PULLUP);
_ pinMode(relay, OUTPUT);_
_}_
void loop()
_{_
_ for(byte i = 0; i < sizeof(ledSet)/sizeof(ledSet[0]); i++)_
_ {_
_ ledSet.update();
}*_
* switch (bathState)*
* {*
* case EMPTY:*
* if(checkMotion())*
* {*
* ledSet[1].writeSpeed(25); // Sets the speed of fade to 10ms per increment <<<<<<<<<<<<*
* ledSet[1].write(255);*
* digitalWrite (relay, HIGH);*
* Serial.print(F("Set to max brite at "));*
* Serial.println(millis());*
* pirTimer = millis();*
* bathState = BATH_OCCUPIED;
_ }_
else if(ledSet[1].read() == 255) // if it just came out of BATH_OCCUPIED state*
* {*
* ledSet[1].write(0);*
* }*
* break;*
* case BATH_OCCUPIED:
_ if(checkFlow())
{
ledSet[1].writeSpeed(25); // Sets the speed of fade to 10ms per increment <<<<<<<<<<<<
ledSet[0].write(255);
digitalWrite (relay, LOW);
flowTimer = millis();_
bathState = SHOWER_OCCUPIED;
_ }_
else if(ledSet[0].read() == 255) // if it just came out of SHOWER_OCCUPIED state*
* {*
* ledSet[0].write(0);*
* }*
* if((millis() - pirTimer > TIMEOUT))*
* {*
* ledSet[1].write(0);*
* Serial.println(F("Motion Timer Expired..."));*
* Serial.print(F("Set to zero at "));*
* Serial.println(millis());*
* bathState = EMPTY;*
* }*
* if(checkMotion())*
* {*
* pirTimer = millis(); // as long as motion is detected, prevent the timeout by updating the timer*
* }*
* break;*
* case SHOWER_OCCUPIED:
_ if((millis() - flowTimer > TIMEOUT))
{
ledSet[1].writeSpeed(50); // Sets the speed of fade to 10ms per increment <<<<<<<<<<<<
ledSet[0].write(0);*_
* Serial.println(F("Flow Timer Expired..."));*
* bathState = BATH_OCCUPIED;
_ }
if(checkFlow()) // as long as the shower is running, prevent the timeout by updating the timer*
* {
flowTimer = millis();
}
break;
}*_
* if(bathState != lastState)*
* {*
* Serial.print(F("New Bath State is "));*
* Serial.println(stateText[bathState]);*
* lastState = bathState;*
* }*
}
bool checkFlow()
{
* static bool flowState = LOW;*
* bool state = digitalRead(flow);*
* if(flowState != state)*
* {*
* if(state == HIGH)*
* {*
* Serial.println(F("Flow Sensor Triggered..."));*
* return true;*
* }*
* flowState = state;*
* }*
* return false;*
}
bool checkMotion()
{
* static bool motionState = LOW;*
* bool state = digitalRead(pir);*
* if(motionState != state)*
* {*
* if(state == HIGH)*
* {*
* Serial.println(F("Motion Sensor Triggered..."));*
* return true;*
* }*
* motionState = state;*
* }*
* return false;*
}
how do you add a sketch in to a post corretly
sketch_may08a.ino (3.46 KB)
ledSet[1].writeSpeed(50); // Sets the speed of fade to 10ms per increment <<<<<<<<<<<<
ledSet[0].write(0);
change the speed on object one and then dim object zero...?
try:
ledSet[0].writeSpeed(50); // Sets the speed of fade to 10ms per increment <<<<<<<<<<<<
ledSet[0].write(0);
there are two fade objects to manipulate 0 and 1.
Got it?
Hi again
ok i think i got working now, ramp up speed is a bit slow but it does look great
#include "Fade.h"
#define TIMEOUT 100
enum BathState{
EMPTY,
BATH_OCCUPIED,
SHOWER_OCCUPIED
};
const char* const stateText[] = {"Empty", "Bath Occupied", "Shower Occupied"};
const byte led2 = 10; // Shower Led
const byte led1 = 9; // Bathroom Led
const byte pir = A1; // PIR signal in
const byte flow = A0; // Flow Switch
const byte relay = 11; // Fan Relay
Fade ledSet[] = { // dimming pin, millis between steps
{led1, 20},
{led2, 10}}
;
BathState bathState = EMPTY;
BathState lastState = BATH_OCCUPIED;
unsigned long flowTimer = 0;
unsigned long pirTimer = 0;
void setup()
{
digitalWrite(relay,HIGH); // set pin HIGH first
Serial.begin(9600);
for(byte i = 0; i < sizeof(ledSet)/sizeof(ledSet[0]); i++)
{
ledSet[i].begin();
ledSet[i].write(0);
}
pinMode(pir, INPUT);
pinMode(flow, INPUT_PULLUP);
pinMode(relay, OUTPUT);
}
void loop()
{
for(byte i = 0; i < sizeof(ledSet)/sizeof(ledSet[0]); i++)
{
ledSet[i].update();
}
switch (bathState)
{
case EMPTY:
if(checkMotion())
{
ledSet[1].writeSpeed(10); // Sets the speed of fade to 10ms per increment <<<<<<<<<<<<
ledSet[1].write(255);
digitalWrite (relay, HIGH);
Serial.print(F("Set to max brite at "));
Serial.println(millis());
pirTimer = millis();
bathState = BATH_OCCUPIED;
}
else if(ledSet[1].read() == 255) // if it just came out of BATH_OCCUPIED state
{
ledSet[1].write(0);
}
break;
case BATH_OCCUPIED:
if(checkFlow())
{
ledSet[0].write(255);
ledSet[0].writeSpeed(15); // Sets the speed of fade to 10ms per increment <<<<<<<<<<<<
digitalWrite (relay, LOW);
flowTimer = millis();
bathState = SHOWER_OCCUPIED;
}
else if(ledSet[0].read() == 255) // if it just came out of SHOWER_OCCUPIED state
{
ledSet[0].write(0);
}
if((millis() - pirTimer > TIMEOUT))
{
ledSet[1].write(0);
Serial.println(F("Motion Timer Expired..."));
Serial.print(F("Set to zero at "));
Serial.println(millis());
bathState = EMPTY;
}
if(checkMotion())
{
pirTimer = millis(); // as long as motion is detected, prevent the timeout by updating the timer
}
break;
case SHOWER_OCCUPIED:
if((millis() - flowTimer > TIMEOUT))
{
ledSet[0].write(0);
Serial.println(F("Flow Timer Expired..."));
bathState = BATH_OCCUPIED;
}
if(checkFlow()) // as long as the shower is running, prevent the timeout by updating the timer
{
flowTimer = millis();
}
break;
}
if(bathState != lastState)
{
Serial.print(F("New Bath State is "));
Serial.println(stateText[bathState]);
lastState = bathState;
}
}
bool checkFlow()
{
static bool flowState = LOW;
bool state = digitalRead(flow);
if(flowState != state)
{
if(state == HIGH)
{
Serial.println(F("Flow Sensor Triggered..."));
return true;
}
flowState = state;
}
return false;
}
bool checkMotion()
{
static bool motionState = LOW;
bool state = digitalRead(pir);
if(motionState != state)
{
if(state == HIGH)
{
Serial.println(F("Motion Sensor Triggered..."));
return true;
}
motionState = state;
}
return false;
}
Thanks again BULLDOGLOWELL for all help, this is just what i have wanted for some time.
i put together a control system that just turns on the leds and fan using a pir, relays and 2 din rail times but this just doesn't look as good as fading in and out also it will be a lot quieter now with out all of the relays operating all the time.
Thanks again. i have learnt a lot from this but i am not any ware near you level.
Thanks
Regards
Steve