Hello guys. I have a weird one for you. I’m trying to light up a bulb (through a relay) by using an LDR. The problem that I have is that when the light approaches the target value it has small variation that turns ON/OFF the bulb. In order to stop that I tried to an array to store the latest values and then make an average out of that, but something is not working. When the light approaches the target value the bulb starts to rapidly turn ON/OFF. I was expecting, that my code, every 2 seconds (unsigned long IntervalReflectoare = 2000;), it will read the sensor, add the value to the array and calculate the average.
I’ve tried doing some debugging, and I’ve got some weird results. This are the lines that I have added to the code:
Serial.print(TimpReflectoare);
Serial.print(" ");
Serial.print(millis());
Serial.print(" ");
Serial.print(IntervalReflectoare);
Serial.print(" ");
Serial.println(millis() - TimpReflectoare);
And this is the result:
Here is the whole code.
#define REMOTEXY_MODE__WIFI_CLOUD
#include <WiFi.h>
#include <RemoteXY.h>
// RemoteXY connection settings
#define REMOTEXY_WIFI_SSID "xxx"
#define REMOTEXY_WIFI_PASSWORD "xxx"
#define REMOTEXY_CLOUD_SERVER "cloud.remotexy.com"
#define REMOTEXY_CLOUD_PORT 6376
#define REMOTEXY_CLOUD_TOKEN "7f9e099ee89e878ad23e6e9f9fe32d8d"
//Reflectoare
int Fotorezistor = 34; // Fotorezistor
int Releu_Reflectoare = 5; // Releu reflectoare
unsigned long Fotorezistor_Variabila = 0;
unsigned long Fotorezistor_Variabila_Suma = 0;
unsigned long Fotorezistor_Variabila_Medie = 0;
int Fotorezistor_Limita = 3600;
unsigned long IntervalReflectoare = 2000;
unsigned long TimpReflectoare = 0;
const int Dimensiune_Array = 10;
int Array[Dimensiune_Array];
unsigned long counter = 0;
// RemoteXY GUI configuration
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] = // 118 bytes
{ 255,1,0,23,0,111,0,19,0,0,0,0,31,1,108,200,1,1,8,0,
130,0,0,55,32,24,31,129,8,3,40,7,64,8,82,101,102,108,101,99,
116,111,97,114,101,0,70,3,13,13,13,16,16,135,0,10,18,13,34,13,
50,0,31,31,79,78,0,0,79,70,70,0,67,57,2,25,8,102,94,13,
11,67,57,12,25,8,102,94,13,11,129,82,3,28,8,64,8,73,110,115,
116,97,110,116,0,129,82,13,24,8,64,8,77,101,100,105,101,0 };
// this structure defines all the variables and events of your control interface
struct {
// input variables
uint8_t Reflectoare_Buton; // =1 if state is ON, else =0
// output variables
uint8_t Reflectoare_LED; // from 0 to 1
char Fotorezistor_Variabila_Text[11]; // string UTF8 end zero
char Fotorezistor_Variabila_Medie_Text[11]; // string UTF8 end zero
// other variable
uint8_t connect_flag; // =1 if wire connected, else =0
} RemoteXY;
#pragma pack(pop)
/////////////////////////////////////////////
// END RemoteXY include //
/////////////////////////////////////////////
void setup() {
Serial.begin(115200);
delay(100);
RemoteXY_Init ();
delay(1000);
pinMode(Fotorezistor,INPUT);
pinMode(Releu_Reflectoare,OUTPUT);
digitalWrite(Releu_Reflectoare,!LOW);
}
void loop() {
// START loop
RemoteXY_Handler ();
//START Reflectoare
if (RemoteXY.Reflectoare_Buton == 1) {
digitalWrite(Releu_Reflectoare,!HIGH);
} else {
if( (millis() - TimpReflectoare) > IntervalReflectoare){
TimpReflectoare = millis();
Fotorezistor_Variabila = analogRead(Fotorezistor);
dtostrf(Fotorezistor_Variabila, 0, 0, RemoteXY.Fotorezistor_Variabila_Text);
for (byte i = Dimensiune_Array; i > 0; i = i - 1) {
Array[i] = Array[i-1];
}
Array[0] = Fotorezistor_Variabila;
Fotorezistor_Variabila_Suma = 0;
for (byte j = 0; j < Dimensiune_Array; j = j + 1) {
Fotorezistor_Variabila_Suma = Fotorezistor_Variabila_Suma + Array[j];
}
Fotorezistor_Variabila_Medie = Fotorezistor_Variabila_Suma/Dimensiune_Array;
dtostrf(Fotorezistor_Variabila_Medie, 0, 0, RemoteXY.Fotorezistor_Variabila_Medie_Text);
if (Fotorezistor_Variabila_Medie > Fotorezistor_Limita) {
digitalWrite(Releu_Reflectoare,!HIGH);
RemoteXY.Reflectoare_LED = 1;
} else {
digitalWrite(Releu_Reflectoare,!LOW);
RemoteXY.Reflectoare_LED = 0;
}
Serial.print(TimpReflectoare);
Serial.print(" ");
Serial.print(millis());
Serial.print(" ");
Serial.print(IntervalReflectoare);
Serial.print(" ");
Serial.println(millis() - TimpReflectoare);
}
}
//STOP Reflectoare
} // END LOOP
What I would like to achieve it that at night, when the light gets to the target value to turn the bulb on and stay on until the morning.




