Hey guys, i advanced a little bit on my project. I connected 4 servo 360, hr-sr501 sensor and a LED. they all work good from blynk, but my problem now is that some of them work without me activating them. if i run servo 1 another one or two will have a small interference and move a little bit. how can i cancel that?
and also delay commend is pausing all other actions! i understand that there is a way to do it better than delay command, i found this online:
/*********
Rui Santos
Complete project details at https://randomnerdtutorials.com
*********/
// constants won't change. Used here to set a pin number :
const int ledPin = 19; // the number of the LED pin
// Variables will change :
int ledState = LOW; // ledState used to set the LED
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change :
const long interval = 2000; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void loop() {
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
I didn’t understand how i use this code, can someone please explain me this?
And this is my code:
#define BLYNK_PRINT Serial
#include <Servo.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = "afALTS4s5s_lu3Z_yNeimE4wcAdpJ3o0";
char ssid[] = "Dvir";
char pass[] = "dvir5558";
int Sens =2;
int LED =4;
Servo FoodServo;
Servo TreatServo;
Servo WaterServo;
Servo GateServo;
void setup() {
pinMode(Sens, INPUT);// Input from sensor
pinMode(LED, OUTPUT);// OUTPUT to alarm or LED
FoodServo.attach(22);//
TreatServo.attach(23);
WaterServo.attach(19);
GateServo.attach(18);
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);// connect to blyk server
}
void loop() {
Blynk.run();
int motion =digitalRead(Sens);
if(motion){
Serial.println("Motion detected");
digitalWrite(LED,HIGH);
}
else
{
Serial.println("===nothing moves");
digitalWrite(LED,LOW);
}
delay(500);
}
BLYNK_WRITE(V1)
{
int FoodServoState =param.asInt();
if (FoodServoState==1)
{
FoodServo.writeMicroseconds(1000);
delay (2000);
FoodServo.writeMicroseconds(1500);
delay (2000);
FoodServo.writeMicroseconds(2000);
delay (2000);
FoodServo.writeMicroseconds(1500);
}
}
BLYNK_WRITE(V2)
{
int TreatServoState =param.asInt();
if (TreatServoState==1)
{
TreatServo.writeMicroseconds(1000);
delay (2000);
TreatServo.writeMicroseconds(1500);
delay (2000);
TreatServo.writeMicroseconds(2000);
delay (2000);
TreatServo.writeMicroseconds(1500);
}
}
BLYNK_WRITE(V3)
{
int WaterServoState =param.asInt();
if (WaterServoState==1)
{
WaterServo.writeMicroseconds(1000);
delay (2000);
WaterServo.writeMicroseconds(1500);
delay (2000);
WaterServo.writeMicroseconds(2000);
delay (2000);
WaterServo.writeMicroseconds(1500);
}
}
BLYNK_WRITE(V4)
{
int GateServoState =param.asInt();
if (GateServoState==1)
{
GateServo.writeMicroseconds(1000);
delay (2000);
GateServo.writeMicroseconds(1500);
delay (2000);
GateServo.writeMicroseconds(2000);
delay (2000);
GateServo.writeMicroseconds(1500);
}
}
Thank you everyone you are a great community!