I will brief my project first. Its a 4WD robot which can be controlled over internet. I am using esp8266 NodeMCU as my mainboard. For sending data over internet I am using firebase realtime database.
The android app has the following features. It can control the robot movements 2 in different speeds.It will display live reading of 2 sensors (DHT11 and MQ2).
ISSUE
When i tested only with motors it was working fine (There was quick response as soon as I change direction from app)
Then i added the 2 sensor check the same. But this time i was getting delayed response from the motors.
For eg : When the robot is moving forward direction and the backward direction button is pressed i will take around 2 sec for the robot to change it’s direction. Even if its 2 sec it’s getting too hard while controlling it.
Here is code i tested only motor with speed and direction. and it’s working fine
Anyone had similar issue ? I even tried changing the if else ladder to switch case, no positive result.
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
#define FIREBASE_HOST "xxx-xxx.firebaseio.com" //Your Firebase Project URL goes here without "http:" , "\" and "/"
#define FIREBASE_AUTH "xxxxx" //Your Firebase Database Secret goes here
#define WIFI_SSID "xxx" //your WiFi SSID for which yout NodeMCU connects
#define WIFI_PASSWORD "xxx"
int value = 0;
int in1 = D1;
int in2 = D2;
int in3 = D3;
int in4 = D4;
int enA = D6;
int enB = D7;
void setup() {
Serial.begin(9600);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("connected: ");
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.set("S1", 0);
}
void loop() {
value = Firebase.getInt("S1");
if (value == 1) {
Serial.println("Both Forward slow");
analogWrite(enA, 500);
analogWrite(enB, 500);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
else if(value == 2) {
Serial.println("Both Forward fast");
analogWrite(enA, 1023);
analogWrite(enB, 1023);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
else if(value == 3) {
Serial.println("Both backward slow");
analogWrite(enA, 500);
analogWrite(enB, 500);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
else if(value == 4) {
Serial.println("Both backward fast");
analogWrite(enA, 200);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
}
And here is the complete code. I am getting expected values on the app. The only problem is the delay while changing the direction
As soon i pressed the button the value in the firebase is getting changed. So there is no problem with network.
#include "DHT.h" // including the library of DHT11 temperature and humidity sensor
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
#define FIREBASE_HOST "xxx-xxx.firebaseio.com" //Your Firebase Project URL goes here without "http:" , "\" and "/"
#define FIREBASE_AUTH "xxx" //Your Firebase Database Secret goes here
#define WIFI_SSID "xxx" //your WiFi SSID for which yout NodeMCU connects
#define WIFI_PASSWORD "xxx"
#define DHTTYPE DHT11 // DHT 11
#define dht_dpin 0 // Connect Sensor to D3 pin
#define mq2_pin D7
#define ENA D5
#define ENB D6
int fast = 1023;
int slow = 511;
DHT dht(dht_dpin, DHTTYPE);
int S1 = 0;
int speed_value;
int temp_value;
int smoke_value;
int humidity_value;
void setup() {
Serial.begin(9600);
pinMode(A0, INPUT);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
pinMode(D5, OUTPUT);
pinMode(D6, OUTPUT);
pinMode(D7, INPUT);
dht.begin();
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("connected: ");
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
// Firebase.set("S1", 0);
// Firebase.set("Speed", 1);
// Firebase.setInt("Temperature", 0);
// Firebase.setInt("Humidity", 0);
// Firebase.setInt("Smoke", 0);
}
void loop() {
S1 = Firebase.getInt("S1");
speed_value = Firebase.getInt("Speed");
humidity_value = dht.readHumidity();
temp_value = dht.readTemperature();
smoke_value = digitalRead(D4);
Firebase.setInt("Temperature", temp_value);
Firebase.setInt("Humidity",humidity_value);
Firebase.setInt("Smoke",smoke_value);
if (speed_value == 1) {
if (S1 == 0) {
stop_();
}
else if (S1 == 1) {
forward_slow();
}
else if (S1 == 2) {
backward_slow();
}
else if (S1 == 3) {
left_slow();
}
else if (S1 == 4) {
right_slow();
}
}
else if (speed_value == 2) {
if (S1 == 0) {
stop_();
}
else if (S1 == 1) {
forward_fast();
}
else if (S1 == 2) {
backward_fast();
}
else if (S1 == 3) {
left_fast();
}
else if (S1 == 4) {
right_fast();
}
}
}