For almost 3 years i have been working on a simple PIR led dimmer project. I have so many blue prints and electronic components bought with enthusiasm in the wardrobe...
Now I believe the dream is coming to reality!
If somebody could share his experience by writing a simple code for my need and asks for lets say 1 or 10 bucks( depending on the time consumption), i will be more than happy.
If we look from this professional person's view;
there are millions of Arduino users. If 5 of them asks for this service it makes 25 bucks in a few hours.
in short it is ''win win'' situation.
is there such service in this world? If not can somebody do this?
I'm sure there are people using this forum that is willing to 'get you started'
State clearly your problem. make diagrams/drawings/sketches and tell what you wish for.
when PIR is high ==> led fade in via mosfet
when PIR is low ==> led fade out via mosfet
5 minutes will be operation time (if not triggered in 5 minutes)
/************************************************************
Led dimmer using a logic lever MOSFET transitor
Supply voltage is 12V / LED build for 12V, so no resistor is needed
PIR-sensor power is fed from Arduino 5V / GND-pins.
PIR output to digital pin (D2) - polled
PIR can be retriggable or non retriggable
LED switches on when PIR detects movement
LED output will stay 'ON' until 5 minutes of 'PIR silence'
LED (Mosfet gate) MUST be connected to PWM pin. (D11)
this code can be drastically redused on volume - made for readability
****************************************************************/
// declare global constants
const byte led = 11; // connect to gate
const byte pir = 3; // PIR signal out
const byte turnOnTime = 5; // turn on/off time in seconds !(will be forced: 50 > time > 0)
const byte minutesToOff = 5; // minutes of 'silence' before "lights out"
// declare global variables
boolean lightIsOn = false; // remember LED-status 'now'
unsigned long timeToTurnOff; // time when led will turn off
unsigned long timeNow; // current time
// one time setup
void setup()
{
// set status og used I/O-pins
pinMode(led,OUTPUT);
pinMode(pir,INPUT);
}
//********************************
// small functions and procedures - to make reading of main prog easier
boolean heWantsLightsOn()
{
return (digitalRead(pir)==HIGH);
}
//********************************
void turnLightsOn(byte period)
{
// now the LED shall increase light intensity over "turnOnTime" seconds
long delayTime = period * 5 ;
for (byte i=40; i<255; i+=5)
{
analogWrite(led,i);
delay(delayTime);
}
digitalWrite(led,HIGH); // no PWM needed - turn on 100%
lightIsOn=true; // remember status
}
//**********************************
void turnLightsOff(byte period)
{
// now the LED shall decrease light intensity - slowly to zero
long delayTime = period*5 ;
for (byte i=250; i>40; i-=5)
{
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 still dark - be light! (else light were on already)
// set 'new' time for off -since you just told 'stay on for some mor minutes..)
timeToTurnOff=timeNow + minutesToOff * 60000; // set 'off-time' some minutes into the future (milliseconds used)
}
// now test for "Elvis has left the building"
if (timeNow > timeToTurnOff) // is it time to turn off ??
{
if (lightIsOn) turnLightsOff(turnOnTime); // if light still are on: turn them off
}
}
//***************END***********************
I tested with the onboard led (pin13), and it seems to work correct
I have altered to run incr/decr at a slower pace.
If you got a digital level MOSFET this whould work just fine.
Copy code - test run !
/************************************************************
Led dimmer using a logic lever MOSFET transitor
Supply voltage is 12V / LED build for 12V, so no resistor is needed
PIR-sensor power is fed from Arduino 5V / GND-pins.
PIR output to digital pin (D2) - polled
PIR can be retriggable or non retriggable
LED switches on when PIR detects movement
LED output will stay 'ON' until 5 minutes of 'PIR silence'
LED (Mosfet gate) MUST be connected to PWM pin. (D11)
this code can be drastically redused on volume - made for readability
****************************************************************/
// declare global constants
const byte led = 11; // connect to gate
const byte pir = 3; // PIR signal out
const byte turnOnTime = 5; // turn on/off time in seconds !(will be forced: 50 > time > 0)
const byte minutesToOff = 1; // minutes of 'silence' before "lights out" !! ALTER THIS VALUE
const boolean debug=false;
// declare global variables
boolean lightIsOn = false; // remember LED-status 'now'
unsigned long timeToTurnOff; // time when led will turn off
unsigned long timeNow; // current time
// one time setup
void setup()
{
// set status og used I/O-pins
pinMode(led,OUTPUT);
pinMode(pir,INPUT);
}
//********************************
// small functions and procedures - to make reading of main prog easier
boolean heWantsLightsOn()
{
return (digitalRead(pir)==HIGH);
}
//********************************
void turnLightsOn(byte period)
{
// now the LED shall increase light intensity over "turnOnTime" seconds
long delayTime = period * 5 ;
for (byte i=40; 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)
{
// now the LED shall decrease light intensity - slowly to zero
long delayTime = period*5 ;
for (byte i=254; i>40; 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 still dark - be light! (else light were on already)
// set 'new' time for off -since you just told 'stay on for some mor minutes..)
timeToTurnOff=timeNow + minutesToOff * 60000; // set 'off-time' some minutes into the future (milliseconds used)
if (debug) Serial.println("renew for one minute");
}
// now test for "Elvis has left the building"
if (timeNow > timeToTurnOff) // is it time to turn off ??
{
if (lightIsOn) turnLightsOff(turnOnTime); // if light still are on: turn them off
}
}
//***************END***********************
There is still one thing to be done..
.> if your in the room, sitting still and the lights start dimming then they will turn off completle. You cannon stop this by moving.
Change to be made..(in the "turn.off" procedure):
Test for movement and stop dimming -> increase to 100%
You take the challange?
let me give you status report before accepting new challenge.
So far all fade in out time and speed trimmed.
when we enter in to the dark kitchen we need light immediately so fade in time shortened.
and
it is nice thing to see lights fade out. so fade out time lengthened.
new challenges that planed for this project are;
test for movement and stop dimming.
I thing this is about callibrating sensors and adding new if conditions which i do not have any idea. there may be need to change sensors from modules to raw components.
second and less challenging plan is to control multiple leds with multiple fade in time. (pin, 11,13...)
in 1 year i plan to carry this project to a new platform by controlling RGB lighs as soon as there is enough experience gained.
.yes i take the challenge, please give me homeworks
P.S Another thing that keeps my mind occupied is how to thank you. If you could give me your post adress from PM i will send you some Turkish Delight and some souvenirs. Also you are very welcome if you visit Turkey....
I guess the potensial problem of "no activity" may not apply to a kitchen, so maybe not needes to do changes.
You already can see that you understand how the code works.
Be creative! Almost anything is posible. Good luck!
I have a project like this, but light in a toolcabin.
I have a bit of code going on but it needs to be debugged.
I saw this and wanted to use this instead and some other features...
Hope it's ok??