Home automation code

Hey guys,

I developped an Arduino program to switch on and off home devices as follow but it doesn't work:
When current is different than 0 (electricity is back), I switch on the fridge only and keep other devices off, wait for 1 minute then switch on other devices.
I switch on the WM when no one is home.
I switch off LIGHT and OUTL when no one is home.

I used PIR motion sensor HCSR501 to detect motion and current sensor ACS712-05 to measure current.
Below my code but it doesn't work.
Could you help me?

const int FRID = 12;
const int WM = 11;
const int OUTL = 10;
const int HEATE = 9;
const int LIGHT = 8;
#define pirPin 7
// Create variables:
int val = 0;
bool motionState = false; // We start with no motion detected.

#include <Wire.h>
#define currentPin A0

float multiplier =0.185; //Sensibility in Voltios/Ampers for the 5A model
float SensorRead;
float CurrentSensorRead;
float difference;
float currentValue;
float previousValue;
void setup()
{
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode (FRID, OUTPUT);
pinMode (OUTL, OUTPUT);
pinMode (HEATE, OUTPUT);
pinMode (WM, OUTPUT);
pinMode (LIGHT, OUTPUT);
pinMode(currentPin, INPUT);
}

void loop()
{
read_data(); //read data from inputs
manage_devices();
}

void read_data(void) {
SensorRead= analogRead(currentPin);
val=digitalRead(pirPin);

}

void comparing_data(void) {
currentValue = SensorRead;
difference = currentValue - previousValue;
Serial.println(difference);
previousValue = currentValue;
delay(5000);

}

void manage_devices(void) {
if (difference<=0){
digitalWrite(FRID, HIGH);
digitalWrite(OUTL, LOW);
delay(3600);
digitalWrite(OUTL, HIGH);
digitalWrite(HEATE, LOW);
delay(3600);
digitalWrite(HEATE, HIGH);
digitalWrite(WM, LOW);
delay(3600);
digitalWrite(WM, HIGH);
digitalWrite(LIGHT, LOW);
delay(3600);
digitalWrite(LIGHT, HIGH);

}
else if (val=0){
digitalWrite(LIGHT, LOW);
}
else if (val=1){
digitalWrite(WM, HIGH);
}
}

Please take a moment and read this: How to get the best out of this forum - Using Arduino / Installation & Troubleshooting - Arduino Forum

Then, go back and edit your post so you code is inside code tags. It helps people help you.

Then, "doesn't work" is not a helpful description. How is it not working? What are you expecting v. what are you actually seeing? A better, detailed description will also help

And, if you turn on all warnings when compiling, your error will be obvious

C:\Users\brianh\AppData\Local\Temp\arduino_modified_sketch_927965\sketch_jul01a.ino: In function 'void manage_devices()':
C:\Users\brianh\AppData\Local\Temp\arduino_modified_sketch_927965\sketch_jul01a.ino:70:16: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   else if (val = 0) {
            ~~~~^~~
C:\Users\brianh\AppData\Local\Temp\arduino_modified_sketch_927965\sketch_jul01a.ino:73:16: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   else if (val = 1) {
            ~~~~^~~

'=' is for assignment, '==' is for comparison

Placeholder... thinking...
By the way, there are lot more than 3600 milliseconds in a minute.

Your code is broken, maybe you know? If you don't ever call 'comparing_data(void)', 'difference' isn't updated, and so 'manage_devices(void)' always runs with a stale copy of it.

You only ever set 'FRID' HIGH, never LOW. Is that intentional?

You have a function called "comparing_data()" but as far as I can tell you never call it.

Thank you for your reply.

Do you mean I should add comparing_data function in void loop ?

Thank you for your reply.

Do you mean I should write comparing_data in void loop?

Yeah FRID is always HIGH because the fridge is operating 24h/24

I would not have put it in a separate function at all.

You wrote it or copied someone who wrote it. Where did you intend to use it or where did they call it?

I intend to use it separately from void loop because in void loop, when motion state is high, the led light on and off and doesn't always stay light on or off as required.

Functions don't run unless you call them.

Why do you use two different methods of variable specification? Is this a mash up of cut and pasted codes?

Also, you were asked to edit the post and put it in code tags, see reply #2.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.