Hi there
I am new arduino code so please for give me.
I am tying to make a project that will fade up some leds when a pir is activated then run for a set time(millis) if no moment detected then fade back down.
i do have a sketch that does this just fine but i also need to turn on a relay when a water flow switch is made and stay on till the switch turns off but run on for a pre-set time (millis)
i do have a separate sketch that also does this on its own but i can not get them to run together
can some help me please.
here is my sketch of the led fade up and down. please amend were appropriate.
const int flowswitch = A0;
const byte led = 9; // connect to gate
const byte pir = 7; // PIR signal in
const byte turnOnTime = 2; // turn on/off time in seconds !(will be forced: 50 > time > 0)
const byte minutesToOff = 2; // minutes of 'silence' before "lights out" !! ALTER THIS VALUE
const boolean debug=false;
int stateRelay = LOW;
int stateButton =LOW;
boolean lightIsOn = false; // remember LED-status 'now'
unsigned long timeToTurnOff; // time when led will turn off
unsigned long timeNow; // current time
int Relay = 5;
void setup()
{
pinMode(led,OUTPUT);
pinMode(pir,INPUT);
}
boolean heWantsLightsOn()
{
return (digitalRead(pir)==HIGH);
}
void turnLightsOn(byte period)
{
long delayTime = period * 2 ;
for (byte i=0; i<255; i++)
{
analogWrite(led,i);
delay(delayTime);
}
digitalWrite(led,HIGH); // no PWM needed - turn on 100%
lightIsOn=true; // remember status
}
void turnLightsOff(byte period)
{
long delayTime = period*60 ;
for (byte i=254; i>0; i--)
{
analogWrite(led,i);
delay(delayTime);
}
digitalWrite(led,LOW); // no PWM needed - turn off
lightIsOn=false; // remember status
}
void loop()
{
timeNow=millis(); // note time now
if (heWantsLightsOn()) // if movement -> be light!
{
if (!lightIsOn) turnLightsOn(turnOnTime);
}
if (timeNow > timeToTurnOff) // is it time to turn off ??
{
if (lightIsOn) turnLightsOff(turnOnTime); // if light still are on: turn them off
}
}
//END********
here is the sketch of the relay
const int flowswitch = A0;
int Relay = 5;
int stateRelay = LOW;
int stateButton =HIGH;
int previous = LOW;
long time = 0;
long debounce = 0;
int stayON = 1000; //stay on for 180000 ms = 3mins
void setup() {
pinMode(flowswitch, INPUT);
pinMode(Relay, OUTPUT);
}
void loop() {
stateButton = digitalRead(flowswitch);
if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
if(stateRelay == HIGH){
digitalWrite(Relay, LOW);
} else {
digitalWrite(Relay, LOW);
delay(stayON);
digitalWrite(Relay, HIGH);
}
time = millis();
}
}
thanks
steve