I am working on a little project and I wrote a sketch to debug what is going on with this sensor and why it is not behaving the way it should.
Basically what this code is doing is waiting a period of time to see if motion is detected and if not, turn on the Red LED. If motion is detected during those 30 seconds, turn on the blue LED and reset the timer. For some reason, it seems when I added the second LED the sketch is not behaving the way it should and not picking up motion. What the heck is going on with this thing!!!
Thanks in advance for your help. I appreciate it. I'm very new to Arduino but I am a data warehousing programmer (Informatica). lol
The code is below:
int Sensor = 2;
int LED = 13;
int LED2 = 10;
int SensorVal = 0; //RCWL-0516 Sensor initial value
unsigned long SenseTime; //Time that motion was detected
unsigned long Duration; //Time in seconds that has passed since last motion detectiont
unsigned long WaitTime =30;
bool Detected = 0;
void setup() {
pinMode(Sensor, INPUT); //RCWL-0516 Sensor as input
pinMode(LED, OUTPUT); //LED as output
pinMode(LED2, OUTPUT); //LED as output
digitalWrite(LED, LOW); //Turn LED off
digitalWrite(LED2, LOW); //Turn LED off
Serial.begin(9600);
SensorVal = millis();
}
void loop() {
// put your main code here, to run repeatedly:
SensorVal = digitalRead(Sensor); //Read Sensor value
if (SensorVal == HIGH) {
digitalWrite(LED, HIGH); //Turn LED on
digitalWrite(LED2,LOW); //Turn Red LED off
//delay(3000);
//Serial.println(SensorVal);
Serial.println("Time from last detection in seconds");
SenseTime = millis();
Detected = true;
Serial.println(" Detected =");
Serial.println(Detected);
if (Detected == 1) {
Serial.println("Gotcha");
}
Serial.println("Blue LED is on");
}
if (SensorVal == LOW) {
digitalWrite(LED, LOW); //Turn Blue LED off
//digitalWrite(LED2, HIGH); //Turn Red LED on
Detected = false;
Duration = ((millis() - SenseTime)/1000);
Serial.println(Duration);
Serial.println("Blue LED is off");
}
if (Duration > WaitTime) {
digitalWrite(LED2, HIGH); //Turn Red LED on
}
delay(1000);
}
Hi Paul,
I was just checking to see if I could refer to "HIGH" in that way. if you notice that portion of code just writes a message. And it works.
.
I think doing the digital writes to turn on the LED's are somehow running in to each other and causing the sketch to not respond correctly but not sure.
Well, unless Arduino is different, boolean is normally either a 1 or a 0. Anyways, that is not the issue I was posting about unless you think somehow this is causing my sketch to derail? Is that what you're saying
You only, ever do anything to LED2 is to set it low. If you thought that made a difference, make anything to do with LED2 a comment and recompile and test.
Would be nice to know exactly what your sensor does when motion is sensed and then not sensed.
Ok.. That is not the issue I'm focused on. Thanks Paul. As I said, I am tinkering with Arduino code to see how it responds. This is my second sketch
What I am trying to figure out is why the counter is not responding as it should. For some reason it seems that the sensor is much less "Sensitive". Not picking up motion only randomly. Very peculiar. I'm going to try using interrupts and see if that makes a difference in the response of the Motion sensor. Its something odd like that. Just started tinkering with Arduino. These things are a lot of fun for sure.
if (Duration > WaitTime) {
digitalWrite(LED2, HIGH); //Turn Red LED on
}
This code gets run when the sensor output is either HIGH or LOW. If it is HIGH, "Duration" has not been recalculated, and might still be higher than "WaitTime", resulting in the led getting turned on unexpectedly.
int Sensor = 2;
int LED = 13;
int LED2 = 10;
int SensorVal = 0; //RCWL-0516 Sensor initial value
unsigned long SenseTime; //Time that motion was detected
unsigned long Duration; //Time in seconds that has passed since last motion detectiont
unsigned long WaitTime =30;
bool Detected = 0;
void setup() {
pinMode(Sensor, INPUT); //RCWL-0516 Sensor as input
pinMode(LED, OUTPUT); //LED as output
pinMode(LED2, OUTPUT); //LED as output
digitalWrite(LED, LOW); //Turn LED off
digitalWrite(LED2, LOW); //Turn LED off
SensorVal = millis();
}
void loop() {
// put your main code here, to run repeatedly:
SensorVal = digitalRead(Sensor); //Read Sensor value
if (SensorVal == HIGH) {
digitalWrite(LED, HIGH); //Turn Blue LED on
SenseTime = millis();
Detected = true;
digitalWrite(LED2, LOW); //Turn Red LED on
}
if (SensorVal == LOW) {
digitalWrite(LED, LOW); //Turn Blue LED off
Detected = false;
Duration = ((millis() - SenseTime)/1000);
}
if ((Duration > WaitTime) && (!Detected)) {
digitalWrite(LED2, HIGH); //Turn Red LED on
}
delay(1000);
}
This is my second sketch bro. Never coded in C++. This "project" is me learning what all this stuff is about. Chill homie. Its ok, really.. How many Paul's are on this board anyways???
Just for reference, I have a "potted" explanation:
What you seem to have been suggesting is not what interrupts are for!
As a beginner, it is incredibly unlikely that interrupts will be useful to you.
A common "newbie" misunderstanding is that an interrupt is a mechanism for altering the flow of a program - to execute an alternate function. Nothing could be further from the truth!
An interrupt is a mechanism for performing an action which can be executed in "no time at all" with an urgency that it must be performed immediately or else data - information - will be lost or some harm will occur. It then returns to the main task without disturbing that task in any way though the main task may well check at the appropriate point for a "flag" set by the interrupt.
Now these criteria are in a microprocessor time scale - microseconds. This must not be confused with a human time scale of tens or hundreds of milliseconds or indeed, a couple of seconds. A switch operation is in this latter category and even a mechanical operation perhaps several milliseconds; the period of a 6000 RPM shaft rotation is ten milliseconds. Sending messages to a video terminal is clearly in no way urgent,
Unless it is a very complex procedure, you would expect the loop() to cycle many times per millisecond. If it does not, there is most likely an error in code planning; while the "delay()" function is provided for testing purposes, its action goes strictly against effective programming methods. The loop() will be successively testing a number of contingencies as to whether each requires action, only one of which may be whether a particular timing criteria has expired. Unless an action must be executed in the order of mere microseconds, it will be handled in the loop().
So what sort of actions do require such immediate attention? Well, generally those which result from the computer hardware itself, such as high speed transfer of data in UARTs(, USARTs) or disk controllers.
An alternate use of interrupts, for context switching in RTOSs, is rarely relevant to this category of microprocessors as it is more efficient to write cooperative code as described above.
SensorVal = digitalRead(Sensor); //Read Sensor value
digitalWrite(LED, SensorVal); //Turn Blue LED on or off
if (SensorVal == HIGH) {
SenseTime = millis();
digitalWrite(LED2, LOW); //Turn Red LED off
}
else if (millis() - SenseTime > WaitTime) {
digitalWrite(LED2, HIGH); //Turn Red LED on
}