# If statement / algorithm question

First steps first topic of mine

HELLO WORLD !

I want to make a solar tracking system… yeah yeah I know A LOT OF examples out there but I did it more complicated than it is.

The problem is AC motor. I can’t get any position data from it so I run for 1.3 sec and tracks the sun.
Until sunset everything is fine. But, if there is no light tracker must be ready for sunrise. Turns to the east nearly 10 seconds.
I set a variable and it indicates day or night and want to use whole void loop (maybe it is not possible I don’n know)
If it is day track the sun
If it is turn to east ONCE and wait for the sun

Here is main problem
I use if statement and use a variable which is LT and changes INSIDE if / else if so it means a local variable. Is there any way to make it global? Or there is other ways to do it?

I know code is missing

``````// Solar tracker for non-servo AC motor
// v 0.1
// Code testing

#define Relay1 6
#define Relay2 7

void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(19200);
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
}

void loop() {
//Serial.println("Running ");

int DLDR1 = 350; // Difference between LDRs for turning to west.
int DLDR2 = 100; // Difference between LDRs for staying same position.
int calibration = 0 ; //Calibration value for LDR difference

int WestLDR = analogRead(A1) + calibration ;

int LDR_Dif1 = EastLDR - WestLDR;
int LDR_Dif2 = WestLDR - EastLDR; // Not needed so much maybe used for error correction.
int RelayStepTime = 1300; //Relay run for 1300 ms  default.
int RelayWaitTime = 10000; //Relay waits for 10 sec default.
int AvarageLight = ((EastLDR + WestLDR) / 2); // For nightmode

if ( AvarageLight >= 600 )
{
int LT = 1;

if ( LDR_Dif1 >= DLDR1 ) // Daytime and turning to west situation
{
Serial.print("Daytime and turning to the west situation Wait for 1 sec");
Serial.print(" ");
Serial.print(" ");
Serial.print(EastLDR, DEC);
Serial.print(" ");
Serial.print(WestLDR, DEC);
Serial.println();
//digitalWrite(Relay1,LOW);
//delay(RelayStepTime);

//digitalWrite(Relay1,HIGH);          // Turns Relay Off
delay(1000);

}

else if ( LDR_Dif1 <= (DLDR1 * (-1)) ) // Daytime and turning to east situation / error correction needs to adjust RelayStepTime
{
Serial.print("Error Daytime and turning to the east situation Wait for 1 sec");
Serial.print(" ");
Serial.print(EastLDR, DEC);
Serial.print(" ");
Serial.print(WestLDR, DEC);
Serial.println();
//digitalWrite(Relay2,LOW);
//delay(RelayStepTime);

//digitalWrite(Relay2,HIGH);          // Turns Relay Off
delay(1000);

}

else if ( LDR_Dif1 >= DLDR2 )
{
Serial.print("Equal, Wait for 1 sec ");
Serial.print(EastLDR, DEC);
Serial.print(" ");
Serial.print(WestLDR, DEC);
Serial.println();
delay(1000);

}

}

else if ( AvarageLight < 600 )
{
int LT = 0;
if (LT == 0)
{
Serial.print("Detected night and changing variable to ");
Serial.print(LT);
Serial.print(" ");
Serial.print(AvarageLight, DEC);
Serial.print(" ");
Serial.print(EastLDR, DEC);
Serial.print(" ");
Serial.print(WestLDR, DEC);
Serial.println();
delay(1000);

}

}

}
``````
``````   int LT = 0;
if (LT == 0)
``````

What ?

``````   int LT = 1;
``````
``````    int LT = 0;
``````

These are different variable with the same name and different scopes

is there any way to make it global?

Declare it as a global variable at the start of the program.

any variable defined in ino file outside functions is global. I am sure you know it.

you can use static if the scope of your variable should by limited to that block
http://www.cprogramming.com/tutorial/statickeyword.html

UKHeliBob:
What ?

I know it is silly but function not started if I set variable "int LT".
I accept I'm just noob and coding for 1 week

UKHeliBob:

``````   int LT = 1;
``````
``````    int LT = 0;
``````

These are different variable with the same name and different scopes
Declare it as a global variable at the start of the program.

I tried set variable under loop. Under void loop I accept it is a global but need to change and call back as global variable. If I change variable it changes locally.

Juraj:
any variable defined in ino file outside functions is global. I am sure you know it.

you can use static if the scope of your variable should by limited to that block
http://www.cprogramming.com/tutorial/statickeyword.html

Yes I know and use static and not uploading to arduino if I do that. Mdsaybe an arduino glitch not my mistake.

Another question is If I use if statement it loops under if how can I "break" like while and start from begining of the code? If I use instead of if statement is it solves this problem?

I tried set variable under loop. Under void loop I accept it is a global but need to change and call back as global variable. If I change variable it changes locally.

Sorry, but I don't know what you mean.

Try this

Before setup() enter

``````int LT = 0;  //declare a global variable named LT and initialise its value to zero
``````

In the loop() function change

``````   int LT = 1;
``````

to

``````   LT = 1;  //give the global LT variable a value of 1
``````

and

``````   int LT = 0;
``````

to

``````   LT = 0;  //give the global LT variable a value of 0
``````

I still don't get what use you think these two lines achieve

``````   int LT = 0;
if (LT == 0)
``````

You know that LT is zero so why test it ?

UKHeliBob:
Sorry, but I don’t know what you mean.

I mean changing a local variable won’t change the global value (But it’s possible with static). It’s hard to express for me sorry for that.

UKHeliBob:
Try this

Before setup() enter

``````int LT = 0;  //declare a global variable named LT and initialise its value to zero
``````

In the loop() function change

``````   int LT = 1;
``````

to

``````   LT = 1;  //give the global LT variable a value of 1
``````

and

``````   int LT = 0;
``````

to

``````   LT = 0;  //give the global LT variable a value of 0
``````

It make sense. I will try it and post the code maybe I learn that way more easily

UKHeliBob:
I still don’t get what use you think these two lines achieve

``````   int LT = 0;
``````

if (LT == 0)

``````

You know that LT is zero so why test it ?
``````

Actually there is two stations in solar tracking.
“LT = 1” for day and “LT=0” for night.
I don’t know how to “break” if / else if so I put firstly in the void loop “LT=1” and checked tracking works or not
Then I put “LT=0” to other statement to check night mode is working or not that is the reason why I put “int LT =0” before if statement.

I will ask something different. I don’t have any position control or something like that, controlling with time,.
I think if statement is not proper for because there is a loop inside a if statement.
If avarage light smaller than 600 I want to turn it ONCE to the east and wait for the avarage sun value bigger than 600. I think i can use for this while. Inside while i can use if for waiting the sun. Then … hard to express Can you check this code?

``````void setup()
{
.....
// Solar tracker for non-servo AC motor
// v 0.1
// Code testing

#define Relay1 6
#define Relay2 7

void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(19200);
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
}

void loop() {
//Serial.println("Running ");

int DLDR1 = 350; // Difference between LDRs for turning to west.
int DLDR2 = 100; // Difference between LDRs for staying same position.
int calibration = 0 ; //Calibration value for LDR difference

int WestLDR = analogRead(A1) + calibration ;

int LDR_Dif1 = EastLDR - WestLDR;
int LDR_Dif2 = WestLDR - EastLDR; // Not needed so much maybe used for error correction.
int RelayStepTime = 1300; //Relay run for 1300 ms  default.
int RelayWaitTime = 10000; //Relay waits for 10 sec default.
int AvarageLight = ((EastLDR + WestLDR) / 2); // For nightmode

// Section 1
if ( AvarageLight >= 600 )
{
int LT = 1;

if ( LDR_Dif1 >= DLDR1 ) // Daytime and turning to west situation
{
Serial.print("Daytime and turning to the west situation Wait for 1 sec");
Serial.print(" ");
Serial.print(" ");
Serial.print(EastLDR, DEC);
Serial.print(" ");
Serial.print(WestLDR, DEC);
Serial.println();
//digitalWrite(Relay1,LOW);
//delay(RelayStepTime);

//digitalWrite(Relay1,HIGH);          // Turns Relay Off
delay(1000);

}

else if ( LDR_Dif1 <= (DLDR1 * (-1)) ) // Daytime and turning to east situation / error correction needs to adjust RelayStepTime
{
Serial.print("Error Daytime and turning to the east situation Wait for 1 sec");
Serial.print(" ");
Serial.print(EastLDR, DEC);
Serial.print(" ");
Serial.print(WestLDR, DEC);
Serial.println();
//digitalWrite(Relay2,LOW);
//delay(RelayStepTime);

//digitalWrite(Relay2,HIGH);          // Turns Relay Off
delay(1000);

}

else if ( LDR_Dif1 >= DLDR2 )
{
Serial.print("Equal, Wait for 1 sec ");
Serial.print(EastLDR, DEC);
Serial.print(" ");
Serial.print(WestLDR, DEC);
Serial.println();
delay(1000);

}

}
// Section 2

else if ( AvarageLight < 600 )
{
while ( AvarageLight < 600 )
{
LT = 0; // Changing global value that indicates night

Serial.print("Detected night and changing variable and turns once to east ");   // These
Serial.print(LT);                                                                                         // runs
Serial.print(" ");                                                                                        // ONCE
Serial.print(AvarageLight, DEC);
Serial.print(" ");
Serial.print(EastLDR, DEC);
Serial.print(" ");
Serial.print(WestLDR, DEC);
Serial.println();
delay(1000);
if ( AvarageLight > 600)            // After turning east checks for avarage light
{
LT = 1;
Serial.print("Detected sunset");
Serial.print(LT);
Serial.print(" ");
Serial.print(AvarageLight, DEC);
Serial.print(" ");
Serial.print(EastLDR, DEC);
Serial.print(" ");
Serial.print(WestLDR, DEC);
Serial.println();
// At this point I want to go Section 1

}

}

}

}
``````

I know my code is wrong. I write in the code in section 2 is what I want to do.

And another thing to want to ask
I learned python a little bit and I remember I can write functions and call back when its needed.
Is it possible to do that in C ?

``````// Section 1
if ( AvarageLight >= 600 )
{
int LT = 1;
``````

LT is local to that if statement, not global

And another thing to want to ask
I learned python a little bit and I remember I can write functions and call back when its needed.
Is it possible to do that in C ?

That is the whole point of functions.

that is the reason why I put "int LT =0" before if statement.

But if you set LT to zero and test it immediately afterwards what other value could it have but zero ?

UKHeliBob:
But if you set LT to zero and test it immediately afterwards what other value could it have but zero ?

Not testing LT = 0 or LT = 1 sitoation.
I want to see how if statement works. Not enugh information about variables and if /else if. Just want to run if statement specified condition (LT = 1)
I want to see if statement works if I set this value and get response from serial. Just try to run and fail then run and fail learn something fail until I get it. This is how learning work I think. I'm at learning stage and my code and questions may be silly but I'm noob.
In conclution it is not a important thing.

UKHeliBob:
But if you set LT to zero and test it immediately afterwards what other value could it have but zero ?

As messy as this entire post is, and if I understand the slop on words, I think he is just using the int LT = 0; to test that the code is working, and will remove that line in the final version? That way he can test night conditions without it having to actually be night.

ClydeFrog04:
As messy as this entire post is, and if I understand the slop on words, I think he is just using the int LT = 0; to test that the code is working, and will remove that line in the final version? That way he can test night conditions without it having to actually be night.

Yes actually it is.
Oh god thank you!

moved a few things around to give you the basic idea where they go.

``````#define Relay1 6
#define Relay2 7

//////////////global area////////////////

int DLDR1 = 350; // Difference between LDRs for turning to west.
int DLDR2 = 100; // Difference between LDRs for staying same position.
int calibration = 0 ; //Calibration value for LDR difference
int RelayStepTime = 1300; //Relay run for 1300 ms  default.
int RelayWaitTime = 10000; //Relay waits for 10 sec default.
int LT = 1;
int EastLDR = 0;
int WestLDR = 0;
int AvarageLight = 0;
int LDR_Dif1 = 0;
int LDR_Dif2 = 0;

void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(19200);
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
}

void loop() {
//Serial.println("Running ");
LDR_Dif1 = EastLDR - WestLDR;
LDR_Dif2 = WestLDR - EastLDR; // Not needed so much maybe used for error correction.

getAvarageLight();//function call

// Section 1
if ( AvarageLight >= 600 )
{
LT = 1;
if ( LDR_Dif1 >= DLDR1 ) // Daytime and turning to west situation
{
Serial.print("Daytime and turning to the west situation Wait for 1 sec");
Serial.print(" ");
Serial.print(" ");
Serial.print(EastLDR, DEC);
Serial.print(" ");
Serial.print(WestLDR, DEC);
Serial.println();
//digitalWrite(Relay1,LOW);
//delay(RelayStepTime);

//digitalWrite(Relay1,HIGH);          // Turns Relay Off

delay(1000);
}
else if ( LDR_Dif1 <= (DLDR1 * (-1)) ) // Daytime and turning to east situation / error correction needs to adjust RelayStepTime
{
Serial.print("Error Daytime and turning to the east situation Wait for 1 sec");
Serial.print(" ");
Serial.print(EastLDR, DEC);
Serial.print(" ");
Serial.print(WestLDR, DEC);
Serial.println();
//digitalWrite(Relay2,LOW);
//delay(RelayStepTime);

//digitalWrite(Relay2,HIGH);          // Turns Relay Off
delay(1000);

}
else if ( LDR_Dif1 >= DLDR2 )
{
Serial.print("Equal, Wait for 1 sec ");
Serial.print(EastLDR, DEC);
Serial.print(" ");
Serial.print(WestLDR, DEC);
Serial.println();
delay(1000);
}
} //up to here is ( AvarageLight >= 600 )
// Section 2

else if ( AvarageLight < 600 )
{
while ( AvarageLight < 600 )//  only code in while loop will run
// things like  LDR_Dif1 = EastLDR - WestLDR; running in the main loop
//will not be calculated or updated in a while loop unless told to do so

{
getAvarageLight(); // made AvarageLight a function so this should fix it

LT = 0; // Changing global value that indicates night
Serial.print("Detected night and changing variable and turns once to east ");   // These
Serial.print(LT);                                                                                         // runs
Serial.print(" ");                                                                                        // ONCE
Serial.print(AvarageLight, DEC);
Serial.print(" ");
Serial.print(EastLDR, DEC);
Serial.print(" ");
Serial.print(WestLDR, DEC);
Serial.println();
delay(1000);
if ( AvarageLight > 600)            // After turning east checks for avarage light
{
LT = 1;
Serial.print("Detected sunset");
Serial.print(LT);
Serial.print(" ");
Serial.print(AvarageLight, DEC);
Serial.print(" ");
Serial.print(EastLDR, DEC);
Serial.print(" ");
Serial.print(WestLDR, DEC);
Serial.println();
// At this point I want to go Section 1
}
}
}
}//end loop

void getAvarageLight() {
WestLDR = analogRead(A1) + calibration ;
AvarageLight = ((EastLDR + WestLDR) / 2); // For nightmode
}
``````

I understand that some things don’t make sense when you are trying to learn.

Its bad coding but stick mainly to global’s until you get the basic understanding of the rules of defining.

Its even more fun when you realize that the compiler is really smart and will simply remove code that has no use

so
x=1;
if (x=1){
//do this;
}

just means the compiler simply did

//do this;

worse if you made the If fail as the compiler simply deleted all of the code after it as it could never be true

helibob knows this which is why he sees it as a waste of time that can bite you later (memory usage suddenly jumps just by changing a simple IF statement)

gpop1:
Its even more fun when you realize that the compiler is really smart and will simply remove code that has no use

so
x=1;
if (x=1){
//do this;
}

just means the compiler simply did

//do this;

worse if you made the If fail as the compiler simply deleted all of the code after it as it could never be true

I'm enlightened. So by doing this I can test code inside if statement but can't test the whole code. It's kind of pointless what I done.

gpop1:

``````void getAvarageLight() {
``````

WestLDR = analogRead(A1) + calibration ;
AvarageLight = ((EastLDR + WestLDR) / 2); // For nightmode

So this means a void function and I can call back where I want. That makes everything much more easy for me THANK YOU SO MUCH !

gpop1:
helibob knows this which is why he sees it as a waste of time that can bite you later (memory usage suddenly jumps just by changing a simple IF statement)

I understood much more than before what "if" means and underlying logic.

gpop1:
stick mainly to global's until you get the basic understanding of the rules of defining.

I will Checked arduino.cc for usage but it doesn't make sense without examples. I mean good examples just like you showed me void function example. Searched a lot but I can't find what I want. On youtube I learn basic things but not enough just arduino things. I guess searching for C examples much efficient for me.

Thank you so much again it is very helpful
I'll post the progress at here

most of the simple examples are in the learning section (next to the forum tab).

The main points seem to be that the program is read from top to bottom left to right on a single core processor. (this means that delay 1000 means the processor came to a halt and didn't do nothing for 1 second) look at the millis timer examples to avoid delay.

Using if then if else is a way to speed up the processor as it uses the if else as a quick release (as soon as one if in the chain is correct then don't check the others and skip to the end)

In the beginning don't worry about processor speed just use if or else and avoid if else as that can be confusing. Alt-t keeps the program lined up so you can look down the left side and see where things open and close.

I read a lot but I learn a lot more by trawling interesting things in the forum and remember its open source so copy programs then tear them to bit to see how they work.

Looong time of research and bless the coffee
Here is a working code (test code)

``````// Solar tracker for  AC motor (non-servo)
// v 0.2.1
// Test Code for memory optimizations

// #define Relay1 6
// #define Relay2 7
boolean LT = 0 ;  // Begins with night, in the beginning there was darkness.
boolean MT = 0 ; // 1 Indicates motor TURNED! and When it's turned it is daytime

void setup() {

Serial.begin(9600);
//pinMode(Relay1, OUTPUT);
//pinMode(Relay2, OUTPUT);
}

void loop() {

byte DLDR1 = 88; // Difference between LDRs for turning to west.
byte DLDR2 = 24; // Difference between LDRs for staying same position.

// byte LDR_Dif2 = WestLDR - EastLDR;  Not needed so much maybe used for error correction.
// int RelayStepTime = 1300; Relay run for 1300 ms  default.
// int RelayWaitTime = 10000; Relay waits for 10 sec default.

if (LT == 0) { // Night mode

if ( AvarageLight <= 40 ) {

if (MT == 0) {
// Relay code here
MT = 1;
Serial.println(F("Motor turned"));
return loop();
}
else if (MT == 1) { // if motor already turned
Serial.print(F("NT wait sun "));//
Serial.print(AvarageLight, DEC);
Serial.println();
delay(5000); // ** wait for 1 hour
return loop();
}
else {
return loop();
}
}

else if ( AvarageLight > 50) {

LT = 1; // Sun is shining :)
Serial.println(F("Sun is shining :)"));
return loop();
}
else {
return loop();
}
}
else if (LT == 1)   { // Day mode

if ( AvarageLight >= 50)  {

if ( LDR_Dif >= DLDR1 ) { // Daytime and turning to west situation

Serial.println(F("DT Turning west"));
//digitalWrite(Relay1,LOW);
//delay(RelayStepTime);

//digitalWrite(Relay1,HIGH);
delay(1000);
return loop();

}
else if ( LDR_Dif < (DLDR1 * (-1)) ) { // Daytime and turning to east situation or error correction needs to adjust RelayStepTime

Serial.println(F("Err DT turning east"));

//digitalWrite(Relay2,LOW);
//delay(RelayStepTime);

//digitalWrite(Relay2,HIGH);           Turns Relay Off
delay(1000);
return loop();
}
else if ( LDR_Dif > DLDR2 ) {

Serial.println(F("Daytime Equal"));
delay(1000);
return loop();
}
else {
return loop();
}
}
else if ( AvarageLight < 50 ) {

LT = 0;
MT = 0;
Serial.print(F("Turns once to east "));
Serial.println();
delay(1000);
return loop();
}
else {
return loop();
}
}
else {
return loop();
}
}
``````

What I’ve learned?

1. What is a global variable where the put global and how to change the variable in if statement locally and globaly thanks to @gpop1

2. How to run the code in the begining return loop(); and its so easy, I didn’t expect that

3. How to make an algorithm and that is the hardest part for me

4. Patience and believe searching especially searching for C++ articles helps A LOT

This progress encouraged for next step. I’ll change byte variable to char because of minus value for “Err DT turning east” situation but I need to sleep now

Programmers don't sleep.

.

There is more to learn, as always

``````boolean LT = 0 ;  // Begins with night, in the beginning there was darkness.
boolean MT = 0 ; // 1 Indicates motor TURNED! and When it's turned it is daytime
``````

boolean variables can either have a value of true or false, Whilst 0 and 1 will work it is better to use the correct values.

``````return loop();
``````

Why not just return ?

``````        delay(5000); // ** wait for 1 hour
``````

It is less confusing if the comments match the code

``````  else
{
return loop();
}
}
``````

Why ? The loop() function is about to end and run again anyway.

Your code layout could do with some work too. Why so many blank lines ?

Hi,

The problem is AC motor. I can't get any position data from it so I run for 1.3 sec and tracks the sun.
Until sunset everything is fine. But, if there is no light tracker must be ready for sunrise. Turns to the east nearly 10 seconds.

To tackle this problem, you need to have a limit switch positioned in the East Park position.
When darkness falls you just run the motor East until the limit switch is hit, then stop and wait for morning.
A West limit switch is also usually fitted to stop over tracking to the West.

Hope it helps.

Tom..

Working and tested solar tracking without servo

Here is code

``````#include <MultiMap.h>

// Solar tracker for  AC motor (non-servo)
// v 1.2 debugging mode
// Debugging mode contains Serial values to check if there is an error
// Test Code for memory optimizations

#define WR 6        // West Relay
#define ER 7       // East Relay
boolean LT = 0 ;  // Begins with night, in the beginning there was darkness.
boolean MT = 0 ; // 1 Indicates motor TURNED!

void setup() {

Serial.begin(9600);
pinMode(WR, OUTPUT);
pinMode(ER, OUTPUT);

}

void loop() {

byte DLDR1_West = 40;  // Difference between LDRs for turning to west.
byte DLDR1_East = 40;
byte DLDR2 = 39; // Difference between LDRs for staying same position.
byte Min_Light = 50;

int EastLDR_raw = analogRead(A1);        //Raw values used for multimap
int East_out[] = {1023, 681, 340, 0};   //1024 divided to 4 equal parts
int East_in[] = {0, 977, 985, 1015};   // 0 for 0, 977 for no light, 985 for shadow, 1015 for max light      *Shadow of upside down "T" shape LDR base
int EastLDR;
int EastLDR_linear;
EastLDR_linear = multiMap(EastLDR_raw, East_in, East_out, 4); // Actual multimap
EastLDR=map(EastLDR_linear, 0, 1065, 255, 0); // Changing the range 0-1024 to 0-255

int WestLDR_raw = analogRead(A0);                                         //       ||         --->  T SHAPE LDR BASE
int West_out[] = {1023, 681, 340, 0};                                    //        ||
int West_in[] ={0, 990, 1003, 1018};                                    //         ||
int WestLDR_linear;                                                    //          ||
int WestLDR;                                                          //      ====o==o=====   --->  2 LDR's near the corners
WestLDR_linear = multiMap(WestLDR_raw, West_in, West_out, 4);
WestLDR=map(WestLDR_linear, 0, 1065, 255, 0);

// Extra explanation for multimap
// There is 3 main status of light: Max light, Shadow with T shape base under light, and darkness
// When I use raw values Max light and Shadow with T shape base under light difference is 15. It's fine when sun is on top but have some issues except noon.
// Map function is useless with this situation and its nearly perfect to calibrate LDR's this 3 status.
// Measuring and calibrating LDR's most time consuming part of this project so be patient and understand wisely.
// DLDR1_West = Difference LDRs = Max light on East - West Shadow with T shape base
// DLDR1_East = Difference LDRs = Max light on West - East Shadow with T shape base
// DLDR2 must close these 2 numbers. If it is not you get no response with serial monitor because this situation not defined. So I recomended DLDR1_West(or DLDR1_East) - 1 = DLDR2
// If DLDR_West and DLDR_East not equal get the smaller DLDR1 and --> DLDR(smaller) - 1 = DLDR2

int LDR_Dif = (WestLDR)-(EastLDR);
byte LDR_Dif_abs = abs(LDR_Dif);

int RelayStepTime = 1200;   // Relay run for 1200 ms  default. It changes situation to situation
int RelayWaitTime = 1200; // Relay waits for 1200 sec default.
int RelayDelayTime = 5000;
int RelayDelayTime2= 7000;
byte AvarageLight = ((WestLDR + EastLDR) / 2); // For nightmode
digitalWrite(ER,HIGH);
digitalWrite(WR,HIGH);

if (LT == 0) { // Night mode

if ( AvarageLight <= Min_Light ) {

if (MT == 0) {

digitalWrite(ER, LOW);   // Relay running small amount times for less powerful AC motor.
delay (RelayWaitTime);
digitalWrite(ER,HIGH);

delay(RelayDelayTime);

digitalWrite(ER, LOW);
delay (RelayWaitTime);
digitalWrite(ER,HIGH);

delay(RelayDelayTime);

digitalWrite(ER, LOW);
delay (RelayWaitTime);
digitalWrite(ER,HIGH);

delay(RelayDelayTime);

digitalWrite(ER, LOW);
delay (RelayWaitTime);
digitalWrite(ER,HIGH);

delay(RelayDelayTime);

digitalWrite(ER, LOW);
delay (RelayWaitTime);
digitalWrite(ER,HIGH);

delay(RelayDelayTime);

digitalWrite(ER, LOW);
delay (RelayWaitTime);
digitalWrite(ER,HIGH);

delay(RelayDelayTime);

digitalWrite(ER, LOW);
delay (RelayWaitTime);
digitalWrite(ER,HIGH);

delay(RelayDelayTime2);

digitalWrite(ER, LOW);
delay (RelayWaitTime);
digitalWrite(ER,HIGH);

delay(RelayDelayTime2);

digitalWrite(ER, LOW);
delay (RelayWaitTime);
digitalWrite(ER,HIGH);

MT = 1;

Serial.println(F("Motor turned to East"));
delay (1500);
return;
}
else if (MT == 1) { // if motor already turned
Serial.print(F("NT wait sun "));//
Serial.print(AvarageLight, DEC);
Serial.println();
delay(600000); // ** wait for 10 minutes
return;
}
else {
return;
}
}

else if ( AvarageLight > Min_Light) {

LT = 1; // Sun is shining :)
Serial.println(F("Sun is shining :)"));
return;
}
else {
return;
}
}
else if (LT == 1)   { // Day mode

if ( AvarageLight >= Min_Light)  {

if ( LDR_Dif_abs > DLDR1_West ) { // Daytime and turning to west situation
Serial.print(LDR_Dif);
Serial.print(F(" "));
Serial.print(F("DT Turning to West"));
Serial.println(F(" "));
digitalWrite(WR,LOW);
delay(RelayStepTime);
digitalWrite(WR,HIGH);

delay(3000);
return;

}
else if ( LDR_Dif_abs > DLDR1_East  ) { // Daytime and turning to east situation or error correction needs to adjust RelayStepTime
Serial.print(LDR_Dif);
Serial.print(F(" "));
Serial.print(F("Error DT turning East"));
Serial.println(F(" "));
digitalWrite(ER,LOW);
delay(RelayStepTime);
digitalWrite(ER,HIGH);
digitalWrite(WR,HIGH);

delay(3000);
return;
}
else if ( LDR_Dif_abs < DLDR2 ) {        // When it's day
Serial.print(abs(LDR_Dif));
Serial.print(F(" "));
Serial.print(abs(AvarageLight));
Serial.print(F(" "));
Serial.print(F("Daytime Equal"));  //
Serial.println(F(" "));
delay(3000);                        // Checks every 3 minutes for sun position
return;
}
else {
return;
}
}
else if ( AvarageLight < Min_Light ) {

LT = 0;
MT = 0;
Serial.print(F("Skips to Night Mode "));
Serial.println();
delay(1500);
return;
}
else {
return;
}
}
else {
return;
}
}
``````

Code for calibrating LDRs with multimap

``````#include <MultiMap.h>

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);

}

// the loop routine runs over and over again forever:
void loop() {

int East_out[] = {1023, 681, 340, 0};
int East_in[] = {0, 977, 985, 1015};
int EastLDR;
int EastLDR_linear;
EastLDR_linear = multiMap(EastLDR_raw, East_in, East_out, 4);
EastLDR=map(EastLDR_linear, 0, 1065, 255, 0);

int West_out[] = {1023, 681, 340, 0};
int West_in[] ={0, 990, 1003, 1018};
int WestLDR_linear;
int WestLDR;
WestLDR_linear = multiMap(WestLDR_raw, West_in, West_out, 4);
WestLDR=map(WestLDR_linear, 0, 1065, 255, 0);

//int AvarageLight = ((WestLDR + EastLDR) / 2);
Serial.print(" East_raw ");
Serial.print(EastLDR_raw, DEC);
Serial.print(" | ");
delay(350);
Serial.print(" West_raw ");
Serial.print(WestLDR_raw, DEC);
Serial.print(" | ");
delay(500);
Serial.print(" East ");
Serial.print(EastLDR, DEC);
Serial.print(" | ");
delay(500);
Serial.print(" West ");
Serial.print(WestLDR, DEC);
Serial.println(" | ");
delay(350);

delay(1500);        // delay in between reads for stability
}
``````
``````        digitalWrite(ER, LOW);   // Relay running small amount times for less powerful AC motor.
delay (RelayWaitTime);
digitalWrite(ER, HIGH);
delay(RelayDelayTime);
digitalWrite(ER, LOW);
delay (RelayWaitTime);
digitalWrite(ER, HIGH);
delay(RelayDelayTime);
digitalWrite(ER, LOW);
delay (RelayWaitTime);
digitalWrite(ER, HIGH);
delay(RelayDelayTime);
digitalWrite(ER, LOW);
delay (RelayWaitTime);
digitalWrite(ER, HIGH);
delay(RelayDelayTime);
digitalWrite(ER, LOW);
delay (RelayWaitTime);
digitalWrite(ER, HIGH);
delay(RelayDelayTime);
digitalWrite(ER, LOW);
delay (RelayWaitTime);
digitalWrite(ER, HIGH);
delay(RelayDelayTime);
digitalWrite(ER, LOW);
delay (RelayWaitTime);
digitalWrite(ER, HIGH);
delay(RelayDelayTime2);
digitalWrite(ER, LOW);
delay (RelayWaitTime);
digitalWrite(ER, HIGH);
delay(RelayDelayTime2);
digitalWrite(ER, LOW);
delay (RelayWaitTime);
digitalWrite(ER, HIGH);
``````

If you feel that you really must use delay() like this at least put the repeated code in a for loop to make it more elegant. Is that a rogue delay(RelayDelayTime2) in there or is it deliberate ?