I am working on relay and want to turn it on after a specific time if the condition remains true...
but I am unable to make such logic. Sometimes the relay turned on instantly or after 2 , 3 seconds
void THEFT(){
if (Diff >= 0.1){
theft = true;
if(timeElapsed >= 5000){
if (Diff < 0.1)
theft = false;
timeElapsed = 0;
return;
}}
else{
theft = false;
}
}
Diff = abs(iin - IRx);
if(Diff>=0.1) //Condition
{
THEFT();
if (timeElapsed >= 5000 && theft==true){
digitalWrite(relay,HIGH);
timeElapsed = 0;
}
}
Well in that snippet of code you compare timeElapsed to interval. Neither are defined, timeElapsed is only ever set to 0 and interval has no value.
The complete program might make sense but that doesn't.
Steve
Actually it is just a part of the code...but i did define the other parameters
Just post your full program and don't dither.
If you want to tell if something is true for a certain period you need to record the value of millis() every time you detect a false condition. Then if millis() - lastFalseMillis >= interval you will know that the condition was true throughout the interval.
...R
Before you post ALL of your code, put every } on a line BY ITSELF. Put every { on a line BY ITSELF.
Also, look at that code. The first thing it does is use the value in Diff. Then, it computes a value for Diff. Why?
The THEFT() function can call itself. Why?
Here is my complete code that i am working on. There is CT, RF, LCD and a relay is attached.
There are two CT's. Second one is connected to another arduino on end of line. Both are communicating through RF. If I attach a load between the line, the first CT detect it but second CT not. And thus the diff detected i.e THEFT
I wish that if that THEFT remains there for 5 second then relay turned on otherwise if it is just for 2 or 3 seconds then it will remain off. I think that you will understand it better now...
I also attach the working schematic of it
#include <SPI.h>
#include <RF24.h>
#include <LiquidCrystal.h>
#include <elapsedMillis.h>
LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
RF24 radio(2, 3);
const uint64_t pipe = 0x123456789ABC;
elapsedMillis timeElapsed;
float IRx=0.0;
int sample = 0;
float iin = 0.0;
float Diff = 0.0;
int relay = A5;
boolean theft = false;
void setup()
{
Serial.begin(9600);
pinMode(relay,OUTPUT);
radio.begin();
radio.setChannel(115);
radio.openReadingPipe(0, pipe);
radio.startListening();
lcd.begin(16, 2);
}
void LCDDisplay()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("InI=");
lcd.print(iin);
lcd.print(" OI=");
lcd.print(IRx);
lcd.setCursor(3, 1);
lcd.print("Diff= ");
lcd.print(Diff);
lcd.noCursor();
}
void THEFT(){
if (Diff >= 0.1){
theft = true;
else{
theft = false;
}
}
}
void loop() {
// taking 150 samples from CT with an interval of 2sec and then average the samples data collected
for(int i=0;i<150;i++)
{
sample += analogRead(A1); //read the current from sensor
delay(2);
}
sample = sample/150;
iin = (5.0*sample)/1023;
iin = (iin*4730) / 5.0;
iin = iin / 1000;
if (radio.available())
{
radio.read(&IRx, sizeof(IRx));
}
Diff = abs(iin - IRx);
LCDDisplay();
if(Diff>=0.1){
THEFT();
if (timeElapsed >= 5000 && theft==true){
digitalWrite(relay,HIGH);
timeElapsed = 0;
}
}
}
The whole code is working fine except that of theft implementation that i want...
I tried more than 50 logics and this one is my last try that i post
iin = (5.0*sample)/1023;
iin = (iin*4730) / 5.0;
iin = iin / 1000;
Why do you multiply by 5 and then divide by 5? Why are there no comments to describe WHY this code exists?
if(Diff>=0.1){
THEFT();
if (timeElapsed >= 5000 && theft==true){
digitalWrite(relay,HIGH);
timeElapsed = 0;
}
THEFT simply sets theft to true or false. It hardly seems useful to have a function that does so little. I'd expect THEFT() to also make a scene - make some noise, send a text message, etc. - something to alert someone that a theft had occurred.
Silencing the alarm later has NOTHING to do with Diff. It is COMPLETELY independent of Diff. It depends ONLY on whether there is an alarm going off (the value in timeElapsed should tell you that - 0 means no alarm) and how long the alarm has been going off (again, timeElapsed and millis() will tell you that).
Put comments in your code, like you were explaining why the code does what it does, to your grandma.
I reckon you have your } brackets wrong in the THEFT() function. If you use the AutoFormat tool to indent your code consistently that sort of thing will be obvious.
...R
Problem solved...
Thanks to all for your guidance..
The problem was that the values should be update again while in this case the values were not updating before the next loop...
The condition should have to be applied again after x time on the updated values....
Thanks again...