So I am having a little bit of trouble with my project. I am using an ultrasonic HCSR-04 to detect objects and then send a notification to my phone, all while the servo is simply pivoting or sweeping and an LED blinks. I want to get all of these to work simultaneously but right now, the servo completes its sweep, then the LED blinks once, then the ultrasonic detects an object and then I receive the notification. This won’t work as I need the notification as soon as the ultrasonic detects the object, it can’t wait until the servo and LED have finished their functions. (Here is a video of my testing to hopefully help understand the situation better: “https://www.youtube.com/watch?v=jfmx9rcqlw4”). Is this a problem to do with the coding? Or is it because the NodeMCU simply can’t power all these components and must do it individually? Much help would be appreciated. Thanks!
(p.s. I left out my auth token and SSID name and password for obvious reasons)
Hardware: NodeMCU-12E, Servo and HCSR04
Software: Using IOS 13.3.1 (Blynk App)
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>
#define TRIGGERPIN D1
#define ECHOPIN D2
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "RandomNumbersAndLetters";
char ssid[] = "Wi-FiNetwork";// Your WiFi credentials.
char pass[] = "Password";// Set password to "" for open networks.
WidgetLCD lcd(V1);
int LED = 15;
void setup() {
pinMode(LED, OUTPUT);
myservo.attach(0); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
pinMode(TRIGGERPIN, OUTPUT);
pinMode(ECHOPIN, INPUT);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
lcd.clear(); //Use it to clear the LCD Widget
lcd.print(0, 0, "Distance in cm"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
// Please use timed events when LCD printintg in void loop to avoid sending too many commands
// It will cause a FLOOD Error, and connection will be dropped
}
void loop() {
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(500);
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
lcd.clear();
lcd.print(0, 0, "Distance(cm)"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
long duration, distance;
digitalWrite(TRIGGERPIN, LOW);
delayMicroseconds(3);
digitalWrite(TRIGGERPIN, HIGH);
delayMicroseconds(12);
digitalWrite(TRIGGERPIN, LOW);
duration = pulseIn(ECHOPIN, HIGH);
distance = (duration/2) / 29.1;
Serial.print(distance);
Serial.println("Cm");
lcd.print(7, 1, distance);
Blynk.run();
if (distance < 5){
Blynk.notify("Notification Testing");
}
delay(500);
}