Go Down

Topic: Interference of codes problem (Read 115 times) previous topic - next topic

MahathirHE

May 08, 2019, 06:15 pm Last Edit: May 08, 2019, 09:45 pm by MahathirHE
I want to ask for some help from you regarding of my final year project. I'm doing a simple SMART HOME project by using Arduino wemos. While compiling the function of the smart home, i've encountered some problem to compile it. Actually this is my first time using arduino and i don't really understand about the arduino ide syntax. Actually, there is no error in my coding. Basically, there are 5 functions that I want to put in that smart home. First, to control the lights,curtains and fans using ON/OFF Button, and security system only by using TSOP and IR, and lastly controlling the roof for the clothesline. So, I've problem with controlling the roof for the clothesline. The roof for the clothesline works when the rain sensor detects water/rain and it actually functioning when I do it separately. But, when I compile the code with the other functions, the roof is actually functioned, but it works only if I switch ON/OFF the lights/fans/curtains.

So, here I attached the source code of my project with four functions, which are for the lights, fans, curtains and roof.

Code: [Select]
#include <ESP8266WiFi.h>
#include <Servo.h>

#define RAIN_SENSOR A0
#define leftRoof_PIN  D2
#define rightRoof_PIN  D3
#define LIGHT_PIN D4
#define FAN_INA  D6
#define FAN_INB  D7
#define curtain_PIN  D5

Servo leftRoof;
Servo rightRoof;
Servo curtain;

int rainAdc = 0;
boolean rainState = false;
boolean previousRainState = false;
boolean changeState = false;
boolean moveServo = false;
int servoLoop = 0;
int pos1 = 0;
int pos2 = 0;

long currentMillis = 0;
long previousMillis = 0;
int interval = 2000;
WiFiServer server(80); //Initialize the server on Port 80

void setup() {

 WiFi.mode(WIFI_AP); //Our ESP8266-12E is an AccessPoint
 WiFi.softAP("SmartHome", "123456789"); // Provide the (SSID, password); .
 server.begin(); // Start the HTTP Server

 IPAddress HTTPS_ServerIP = WiFi.softAPIP(); // Obtain the IP of the Server
 Serial.print("Server IP is: "); // Print the IP to the monitor window
 Serial.println(HTTPS_ServerIP);

 pinMode(LIGHTS_PIN, OUTPUT);
 digitalWrite(LIGHT_PIN, HIGH);

 pinMode(RAIN_SENSOR, INPUT_PULLUP);

 pinMode(FAN_INA,OUTPUT);
 pinMode(FAN_INB,OUTPUT);

 Serial.begin(115200);

 leftroof.attach(leftRoof_PIN);
 rightRoof.attach(rightRoof_PIN);
 
 curtain.attach(curtain_PIN);
}

void loop() {
 WiFiClient client = server.available();
 if (!client) {
   return;
 }
 
 Serial.println("Somebody has connected");

 // Read what the browser has sent into a String class and print the request to the monitor
 String request = client.readString();
 
 Serial.println(request);

 // To Handle the Request

 if (request.indexOf("/OFF") != -1) {
   digitalWrite(LIGHT_PIN, LOW);
 }
 if (request.indexOf("/ON") != -1) {
   digitalWrite(LIGHT_PIN, HIGH);
 }
 if (request.indexOf("/FanON") != -1) {
   analogWrite(FAN_INA,255);
   digitalWrite(FAN_INB,LOW);
 }
 if (request.indexOf("/FanOFF") != -1) {
   digitalWrite(FAN_INA,LOW);
   digitalWrite(FAN_INB,LOW);
 }
 if (request.indexOf("/CurtainOPEN") != -1) {
   curtain.write(180);
   delay(15);
 }
 if (request.indexOf("/CurtainCLOSE") != -1) {
   curtain.write(0);
   delay(15);
 }


// This is for the Servo Motor and Rain Sensor for the roof

 rainAdc = analogRead(RAIN_SENSOR);
 Serial.print("Rain Meter = ");
 Serial.println(rainAdc);
 if (rainAdc < 1000) {
   if (rainState == false && changeState == false) {
     changeState = true;
     previousMillis = millis();
   }
   else if (rainState == true && changeState == true) {
     changeState = false;
   }
 }
 else {
   if (rainState == true && changeState == false) {
     changeState = true;
     previousMillis = millis();
   }
   else if (rainState == false && changeState == true) {
     changeState = false;
   }
 }

 if (changeState == false) {
   previousMillis = millis();
 }

 if (changeState == true) {
   currentMillis = millis();
   if (currentMillis - previousMillis > interval) {
     
     changeState == false;
     
     if (rainState == false) {
       rainState = true;
       moveServo = true;
     }
     else {
       rainState = false;
       moveServo = true;
     }
   }
 }

 if (moveServo == true) {
   if (rainState == true) {
     pos1 = 0;
     pos2 = 90;
     for (servoLoop = 0; servoLoop < 90; servoLoop++) {
       leftRoof.write(pos1++);
       rightRoof.write(pos2--);
       delay(30);
     }
     leftRoof.write(103);
     rightRoof.write(0);

     moveServo = false;
   }

   else {
     pos1 = 103;
     pos2 = 0;
     for (servoLoop = 0; servoLoop < 90; servoLoop++) {
       leftRoof.write(pos1--);
       rightRoof.write(pos2++);
       delay(30);
     }
     leftRoof.write(0);
     rightRoof.write(90);

     moveServo = false;
   }
 }
 
// the HTML document to respond and add buttons:
 String s = "HTTP/1.1 200 OK\r\n";

 s += "Content-Type: text/html\r\n\r\n";

 s += "<!DOCTYPE HTML>\r\n<html>\r\n";

 s += "<br><input type=\"button\" name=\"b1\" value=\"TURN LIGHTS ON\" onclick=\"location.href='/ON'\">";

 s += "<br><br><br>";

 s += "<input type=\"button\" name=\"bi\" value=\"TURN LIGHTS OFF\" onclick=\"location.href='/OFF'\">";

 s += "<br><br><br>";
 
 s += "<br><input type=\"button\" name=\"b2\" value=\"TURN FANS ON\" onclick=\"location.href='/FanON'\">";

 s += "<br><br><br>";

 s += "<br><input type=\"button\" name=\"b3\" value=\"TURN FANS OFF\" onclick=\"location.href='/FanOFF'\">";

 s += "<br><br><br>";

 s += "<br><input type=\"button\" name=\"b5\" value=\"OPEN CURTAIN\" onclick=\"location.href='/CurtainOPEN'\">";

 s += "<br><br><br>";

 s += "<br><input type=\"button\" name=\"b4\" value=\"CLOSE CURTAIN\" onclick=\"location.href='/CurtainCLOSE'\">";

 s += "<br><br><br>";

 s += "</html>\n";

 //clear previous info in the stream
 client.flush ();

 // Send the response to the client
 client.print (s);

 Serial.println("Client disonnected" );

}



PaulS

Code: [Select]
Servo myservo1; // Create servo object to control a servo
Servo myservo2;
Servo curtain;

So, you have a servo that operates the curtain and a servo that operates the myservo and a servo that operates the myservo2. Strange. More meaningful names are in order. Your variables that hold the pin numbers have lousy names, too.

Code: [Select]
void loop() {
 WiFiClient client = server.available();
 if (!client) {
   return;
 }

So, it doesn't matter if is has started raining, or not. If there is no client, don't bother closing the roof if it is raining. Well, OK, it's your house.

Code: [Select]
if (request.indexOf("/CurtainON") != -1) {
   curtain.write(180);
   delay(15);
 }
 if (request.indexOf("/CurtainOFF") != -1) {

So the servo rips the curtains off the window, or puts them back on. Well, that seems a little hard on the curtains. Certainly be easier to open and close them.

Code: [Select]
if (rainAdc < 1000) {
   Serial.println();

What, exactly, are you learning from that blank line?

Code: [Select]
     changeState == false;
A useless comparison is being performed. The value of changeState does NOT change.

Code: [Select]
if (moveServo == true) {
   if (rainState == true) {

Why are there two variables that control whether to move the servos? It's raining, or it isn't. Why does rainState not depend on the value read from the analog pin (to which, presumably, there is a rain sensor connected)?

Code: [Select]
     for (servoLoop = 0; servoLoop < 90; servoLoop++) {
For loop indices should NOT be global variables.

Code: [Select]
//Serve the HTML document to the browser.

 client.flush ()

That is NOT what that code does.

Code: [Select]
//clear previous info in the stream

 client.print (s); // Send the response to the client

I hate comments at the end of individual statements. Especially when it is perfectly obvious what the statement does.

Overall, your comment placement leaves a lot to be desired. Comments should be before the code that they are describing, and should describe why the code is doing what it is doing, not what it is doing.

Code: [Select]
delay(1);
Why?
The art of getting good answers lies in asking good questions.

MahathirHE

so, basically the codes works.but, the only problem is that the servo motor that control the roof which is myservo1 and myservo2 only functioning when i swith ON/OFF the lights/fans/curtains. So, maybe you could explain to me why the interference of codes happens in my whole codes. Thank you very much for your help :)

UKHeliBob

It would be much easier to read your code if all of the pins had sensible names.  Which pin or pins controls the fan, for instance ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

MahathirHE

#4
May 08, 2019, 08:00 pm Last Edit: May 08, 2019, 08:15 pm by MahathirHE
Okay sure. My bad. okay i'll give sensible names to each pins after this. then, i'll repost the codes here. oh yeah, maybe by chance, do you ever encounter problem like this, where the interference of codes. do yo think what is the factor that leads to this problem? :D

MahathirHE

#5
May 08, 2019, 08:36 pm Last Edit: May 08, 2019, 09:47 pm by MahathirHE
i've improved the codes with sensible names to each pins. Do tell me if there is anything wrong with my codes.

UKHeliBob

Has your code really got a smiley in it and why all those blank lines ??

It seems that you did not read this before posting a programming question
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

MahathirHE


UKHeliBob

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up