i have tried to add second PIR to this code but it is not stable. Could you make quick touch up?
/*
* PIR Triggered LED Fader V2
*/
#define fadePin 9 //PWM Pin on ATtiny85
#define pirPin A2 //Input Pin on ATtiny85
#define pirPin A1 //Input Pin on ATtiny85
#define maxOn 252 //must be multiple of upSpeed
#define minOn 0
#define upSpeed 6 //multiples of x to remove flashes
#define downSpeed 1
#define waitOn 1500 //how many milliseconds to remain on
int pirState = LOW;
int stateCheck = LOW;
int fadeVal = 10;
void setup()
{
pinMode(fadePin, OUTPUT);
pinMode(A1 , INPUT);
pinMode(A2 , INPUT);
analogWrite(fadePin, fadeVal); //at fadeVal
//Serial.begin(9600); //for debugging
}
void fadeUp()//should always fade all the way up
{
while (fadeVal < maxOn)
{
analogWrite(fadePin, fadeVal);
fadeVal += upSpeed;
//Serial.print("fadeVal = ");
//Serial.println(fadeVal);
delay(30);
}
fadeVal = maxOn;
analogWrite(fadePin, fadeVal);//must be done outside or flashes at max
//Serial.print("fadeVal = ");
//Serial.println(fadeVal);
}
void fadeDown()//can be partially 'interupted' by only doing on interation at a time
{
if (fadeVal > minOn)
{
analogWrite(fadePin, fadeVal);
fadeVal -= downSpeed;
//Serial.print("fadeVal = ");
//Serial.println(fadeVal);
delay(1000);
}
else
{
fadeVal = minOn;
analogWrite(fadePin, minOn);//to turn the lights all the way to minOn
} //might be a better way to do this, not sure, want to just have the arduino in low power and not constanly sending
}
void loop()
{
stateCheck = digitalRead(pirPin);
if (stateCheck == HIGH)
{
if (pirState == LOW)
{
fadeUp();
pirState = HIGH;
delay(waitOn); //minimum on time
//Serial.println("Motion");
}
}
else
{
//fadeout here
if (pirState == HIGH)
{
//fadeDown();
pirState = LOW;
//Serial.println("Motion ended");
}
fadeDown();
}
}
/************************************************************
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***********************
and it became something like this. However A2 pin does not read the trigger action. How can it be corrected?
/************************************************************
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 = A3; // PIR signal out
const byte pir2 = A2; // 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(pir2,INPUT);
pinMode(pir,INPUT);
Serial.begin(9600);
}
//********************************
// small functions and procedures - to make reading of main prog easier
boolean heWantsLightsOn()
{
return (digitalRead(pir)==HIGH);
return (digitalRead(pir2)==HIGH);
}
//********************************
void turnLightsOn(byte period)
{
// now the LED shall increase light intensity over "turnOnTime" seconds
long delayTime = period * 5 ;
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)
{
// now the LED shall decrease light intensity - slowly to zero
long delayTime = period*5 ;
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 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 * 600; // 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***********************
I have paid lots of money to this ORIGINAL ( ??!!) arduino board. Mr AWOL would you be so kind to just read the code and help? Who can help if a customer can not get assistance from arduino platform?
The function returns a boolean that will be true or false
digitalRead() returns the state of the pin and that will be HIGH or LOW
Although it is syntactically correct it is nonsense to assign a value to a digitalRead()
The return statement immediately returns the value of the expression after it and exits the function
Summary :
The expression after the return statement is nonsense.
You could just return the value of digitalRead(pir) if you wanted to
You cannot add a second return after the first one because it will be too late as the function will exit before the command is executed.
EDIT : I have just realised that my reply above is rubbish and that digitalRead(pir)==HIGH is perfectly valid and will evaluate to true or false but the point about return exiting the function immediately is still valid.
That will work. Or at least it will return true if either input is high. Whether the rest of the code reacts properly to the return value is yet to be determined.
But personally, I've always thought it was silly to have an IF statement evaluate a logical expression that gives true and false, and then return true and false based on that. This following code gives exactly the same result, is more efficient and will run faster, and I think it's easier to read:
This can be further simplified. In addition to eliminating the redundant if/else, there are a couple redundant equality checks here. In logical expressions, zero is considered false, and a non-zero value is considered true. (false is actually defined as 0, true is defined as 1.) Furthermore, LOW is defined as 0, and HIGH is defined as 1. So the code is basically checking whether (digitalRead(pir)==true || digitalRead(pir2)==true). The result of either comparison is true if digitalRead() returns true and false if it returns false. Like the if/else statement, the comparisons don't add any value to the process, they just take time.
The parenthesis around the whole logical expression aren't actually needed, but I think they help readability, and they don't cause any execution overhead.
ShapeShifter:
That will work. Or at least it will return true if either input is high. Whether the rest of the code reacts properly to the return value is yet to be determined.
But personally, I've always thought it was silly to have an IF statement evaluate a logical expression that gives true and false, and then return true and false based on that. This following code gives exactly the same result, is more efficient and will run faster, and I think it's easier to read:
This can be further simplified. In addition to eliminating the redundant if/else, there are a couple redundant equality checks here. In logical expressions, zero is considered false, and a non-zero value is considered true. (false is actually defined as 0, true is defined as 1.) Furthermore, LOW is defined as 0, and HIGH is defined as 1. So the code is basically checking whether `(digitalRead(pir)==true || digitalRead(pir2)==true)`. The result of either comparison is true if digitalRead() returns true and false if it returns false. Like the if/else statement, the comparisons don't add any value to the process, they just take time.
The simplest form:
The parenthesis around the whole logical expression aren't actually needed, but I think they help readability, and they don't cause any execution overhead.
Can't argue with that. It's late night here and I just wrote what immediately came to mind.
(I'm glad I said "Maybe you want something like this" and not "This is how it should be done" )
PHOTOGUY:
I have paid lots of money to this ORIGINAL ( ??!!) arduino board. Mr AWOL would you be so kind to just read the code and help? Who can help if a customer can not get assistance from arduino platform?
I did read the code.
I read the code in its original thread, and in its repeat thread, so I merged the threads, which I did in my own time.
I then provided you a link to some basic documentation about "return", which was a clue for you to go away and read further on the subject. In fact, the reference page described the exact issue you were having.
The price you paid for your hardware is of no concern to me - I'm a volunteer here.
Now I find my time further wasted by having to respond to your report to moderator whine, and I see that you're complaining about an error message, which you didn't post, in some code which you didn't post.
Mr AWOL have understood clearly that you are volunteer here. However if people prefer chineese clones then what will you be volunteer for?
if you believe in dreams of Massimo Banzi David Cuartielles, David Mellis, Tom Igoe, and Gianluca Martino and many other supporters then please hold hands of newbies or go volunteer for something else