Hello,
I need a little help with a code.
It is an command depending on the light intensity of an 12V motor for a retractable awning.
If it is sunny, the motor starts in the open direction. If shaded, the motor start reverse.
For the stop I have ends limiters.
At the moment, it’s almost ok, I found the value for LDR for “sun” / “shadow” but the problem for which I ask for help, is when the lighting is around the value set in the code, the motot receives open / closed commands without reaching the ends. I don’t know how to explain now, a kind of “oscilating” …
For commands I use Blink slider (1=automatic - LDR value 1023 / 2= closed / 3=open)
I use a Wemos D1 mini module and a module with two relays for motor control (windshield at 12V)
Code: (there are a few more nonsense lines, I also wanted to order a cooling fan with a sensor, but I use a dedicated module )
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
// Blynk
char auth[] = "...";
char ssid[] = "...";
char pass[] = "...";
// OTA
#ifndef STASSID
#define STASSID "..."
#define STAPSK "..."
#endif
int ldrValue = 0; // LDR initializare la 0
int sliderValue = 0; // Blynk slider: 1-automat(soare); 2-inchis; 3-deschis;
// OTA
const char* ssid1 = STASSID;
const char* password = STAPSK;
BLYNK_WRITE(V1)
{
sliderValue = param.asInt(); // assigning incoming value from pin V1 to a variable
}
void setup() {
Serial.begin(115200);
//OTA
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid1, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_FS
type = "filesystem";
}
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
//************************
//pentru STOP?
pinMode(LED_BUILTIN, OUTPUT);
pinMode(5, OUTPUT); // releu sens deschidere wemos D1
pinMode(4, OUTPUT); // releu sens inchidere wemos D2
digitalWrite(5, HIGH);
digitalWrite(4, HIGH);
digitalWrite(LED_BUILTIN, HIGH);
Blynk.begin(auth, ssid, pass);
}
void loop(){
ArduinoOTA.handle();
Blynk.run();
if(sliderValue == 1)
{
ldrValue = analogRead(A0);
if(ldrValue >= 1023)
{
digitalWrite(5, HIGH); //deschidere D1
digitalWrite(4, LOW);
digitalWrite(LED_BUILTIN, HIGH);
Serial.print("V1 Slider value is: ");
Serial.println(sliderValue);
Serial.println("deschidere marchiza");
}
else
if(ldrValue < 1023){
digitalWrite(5, LOW); //retragere
digitalWrite(4, HIGH);
digitalWrite(LED_BUILTIN, HIGH);
Serial.print("V1 Slider value is: ");
Serial.println(sliderValue);
Serial.println("retragere marchiza");
}
}
if(sliderValue == 2)
{
digitalWrite(5, LOW); //retragere
digitalWrite(4, HIGH);
digitalWrite(LED_BUILTIN, HIGH);
Serial.print("V1 Slider value is: ");
Serial.println(sliderValue);
Serial.println("retragere marchiza");
}
if(sliderValue == 3)
{
digitalWrite(5, HIGH); //deschidere D1
digitalWrite(4, LOW);
digitalWrite(LED_BUILTIN, HIGH);
Serial.print("V1 Slider value is: ");
Serial.println(sliderValue);
Serial.println("deschidere marchiza");
}
}
Problem apear when, in the code above, the line with ( ```
else
if(ldrValue < 1023)… )
if I changed the value 1023 into 800 (for histerezis between 1023 sunny / 800 shaded). Now after the first command, the module disconnects. It appears is “offline” and must be reset.
Any ideas ???