I'm trying to use a CT sensor and a PIR sensor to determine whether a cooker is on when nobody is there. The CT sensor and PIR sensor both work by themselves but I'm having trouble getting them to work together the way I want them to and I'm out of ideas. I've attached the code I've got so far and a flowchart of what I want to do. Does anyone have any ideas? Thanks.
#include "EmonLib.h"
EnergyMonitor emon1;
int led = 13;
int sensor = 2;
int buzzer = 8;
int state = LOW;
int val = 0;
bool drawingCurrent = false;
bool personPresent = false;
void setup() {
pinMode(led, OUTPUT);
pinMode(sensor, INPUT);
pinMode(buzzer, OUTPUT);
Serial.begin(9600);
emon1.current(1, 30);
}
void isCookerOn() {
double Irms = emon1.calcIrms(1480);
Serial.println(Irms);
if (Irms > 0.14) {
drawingCurrent = true;
}
else {
drawingCurrent = false;
}
}
void checkMotion() {
val = digitalRead(sensor);
if (val == HIGH) {
digitalWrite(led, HIGH);
delay(100);
if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH;
personPresent = true;
}
}
else {
digitalWrite(led, LOW);
delay(200);
if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW;
personPresent = false;
}
}
}
void loop(){
Serial.println("Checking motion");
checkMotion();
if (state == HIGH) {
for (int i = 0; i <= 299; i++) {
delay(1000);
Serial.println("Checking motion");
checkMotion();
if (state == LOW) {
i++;
}
else {
delay(100);
break;
}
}
for (int i = 0; i <= 4; i++) {
Serial.println("Checking current");
isCookerOn();
if (drawingCurrent == true) {
Serial.println("buzzing");
delay(1000);
break;
}
else {
delay(5000);
}
}
}
else {
delay(1000);
}
}
Thanks for posted information. Well done.
What is the code doing and what differs from the wanted action?
One tip... Use comments telling what the idea in the code is.
Manipulating "i" like i++ in the for loop halfes the total delay but is not recommended. Different compilers might give different or uncertain results.
It looks like your logic is backwards. When state is high (better to use personPresent I think) there is someone there and only then do you check whether the cooker is on.
Its supposed to go into the first for loop when the sensor first detects motion and then keep checking for 5 minutes in case the person doesn't go away. After that it is then supposed to check the current draw every 5 seconds 5 times (in case the heating element isn't on at that time). If it finds nothing it should go back to the start. If it detects drawing current then it sounds a buzzer (just serial prints "buzzing" for now) and then go back to the start.
Right now I'm not really sure what is going in. Sometimes it will detect motion and then carry on checking but some times it detects and then immediately checks the current draw. Am I maybe using the break command wrong?
Manipulating "i" like i++ in the for loop halfes the total delay but is not recommended. Different compilers might give different or uncertain results.
Ok, I've done that and I think the main problem is that during the first for loop I've been trying to use the break command to make the code go back to the start of loop if it detects motion again during the loop. Instead of doing that it just seems to go into the current checking routine. Is there something else I can use to go back to the start of loop since break doesn't work?
I never use break like that, in the middle of the code. Use othef coding than the old fashioned GOTO BASIC.
You use Your private names for the different parts of the code. Not sure what You point at.