asking an alternative of minimising my codes as I did a solar tracker.

//set inital values 
int x = 0; // set initial zero value for the counter 
int preE = 512; // set initial value for light intensity in east direction 
int preW = 512; // set initial value for light intensity in west direction 
int predtime = 512; // set initial value for delay time 
int pretol= 512; // set initial value for tolerance value 
void setup() 
{ 
  Serial.begin(9600); // set the baud rate for the serial monitor 
  
  // first set the relays off 
  pinMode(7, OUTPUT); 
  pinMode(8, OUTPUT);
  pinMode(9,OUTPUT); 
  pinMode(5,OUTPUT);
  digitalWrite(7,HIGH); 
  digitalWrite(8,HIGH);
  digitalWrite(9,HIGH);
  digitalWrite(5,HIGH);
   
  // began the loop 
  } 
  void loop() 
  {
    // Read the first reading
    //read the east LED
    int E1 = analogRead(5); 
    //read the west LED 
    int W1 = analogRead(4);
   // read the delay time potentiometer 
    int dtime1 = analogRead(1);
   //read the tolerance potentiometer 
   int tol1 = analogRead(0);
  // Check if the readings reasonable, otherwise set them to previous ones. 
   if (E1 < 10 || E1 > 1000 ) 
   { 
    E1 = preE ; 
    } 
    if (W1 < 10 || W1 > 1000 ) 
    {
      W1 = preW ; 
      }
      if (dtime1 < 10 || dtime1 > 1000 )
      {
        dtime1 = predtime ;
        }
        if (tol1 < 10 || tol1 > 1000) 
        {
          tol1 = pretol ; 
          }
          delay(100); // delay for one 0.1 second and take the next reading 
          int E2= analogRead(5);
          int W2= analogRead(4);
          int dtime2= analogRead(1); 
          int tol2= analogRead(0);
          if (E2 < 10 || E2 > 1000 ) 
          {
            E2= preE ;
          }
if (W2 < 10 || W2 > 1000 )
{
  W2= preW ;
  }
  if (dtime2 < 10 || dtime2 > 1000 )
  {
    dtime2= predtime ; 
    }
    if (tol2 < 10 || tol2 > 1000 ) 
    {
      tol2= pretol ;
      }
      delay(100); // delay for one 0.1 second and take the third reading 
      int E3= analogRead(5); 
      int W3= analogRead(4);
      int dtime3= analogRead(1);
      int tol3= analogRead(0);
      if (E3 < 10 || E3 > 1000 )
      {
        E3= preE ;
        }
        if (W3 < 10 || W3 > 1000 )
        {
          W3= preW ;
          }
          if (dtime3 < 10 || dtime3 > 1000 )
{
  dtime3= predtime ;
  } 
  if (tol3 < 10 || tol3 > 1000 ) 
  {
    tol3= pretol ;
    }
    int E4 = analogRead(5); 
    int W4 = analogRead(4); 
    int dtime4= analogRead(1); 
    int tol4= analogRead(0); 
    if (E4 < 10 || E4 > 1000 )
    {
      E4= preE ;
    }
    if (W4 < 10 || W4 > 1000 ) 
    {
      W4= preW ;
      }
      if (dtime4 < 10 || dtime4 > 1000 ) 
      {
        dtime4= predtime ; 
        }
        if (tol4 < 10 || tol4 > 1000 )
        {
          tol4= pretol ;
          }

delay(100); 
int E5 = analogRead(5);
int W5 = analogRead(4); 
int dtime5= analogRead(1); 
int tol5= analogRead(0);
if (E5 < 10 || E5 > 1000 ) 
{
  E5= preE ;
  }
  if (W5 < 10 || W5 > 1000 ) 
  {
    W5= preW ; 
    }
    if (dtime5 < 10 || dtime5 > 1000 )
    {
      dtime5= predtime ;
      }
      if (tol5 < 10 || tol5 > 1000 ) 
      {
        tol5= pretol ;
        }
        delay(100);
        int E6 = analogRead(5);
        int W6 = analogRead(4); 
        int dtime6= analogRead(1);
        int tol6= analogRead(0); 
        if (E6 < 10 || E6 > 1000 )
        
       {

E6= preE ;
} 
if (W6 < 10 || W6 > 1000 ) 
{
  W6= preW ;
  } 
  if (dtime6 < 10 || dtime6 > 1000 )
  {
    dtime6= predtime ;
    }
    if (tol6 < 10 || tol6 > 1000 ) 
    {
      tol6= pretol ;
      } 
      delay(100); 
      int E7 = analogRead(5);
      int W7 = analogRead(4);
      int dtime7= analogRead(1); 
      int tol7= analogRead(0); 
      if (E7 < 10 || E7 > 1000 )
      {
        E7= preE ;
        }
        if (W7 < 10 || W7 > 1000 ) 
        {
          W7= preW ;
          }
          if (dtime7 < 10 || dtime7 > 1000 )

{ 
  dtime7= predtime ; 
  }
  if (tol7 < 10 || tol7 > 1000 )
  {
    tol7= pretol ;
    }
    delay(100); 
    int E8 = analogRead(5);
    int W8 = analogRead(4);
    int dtime8= analogRead(1);
    int tol8= analogRead(0); 
    if (E8 < 10 || E8 > 1000 ) 
    {
      E8= preE ; 
      }
      if (W8 < 10 || W8 > 1000 ) 
      {
        W8= preW ;
        }
        if (dtime8 < 10 || dtime8 > 1000 ) 
        {
          dtime8= predtime ; 
          }
          if (tol8 < 10 || tol8 > 1000 )
          { 
            tol8= pretol ;
            }

delay(100);
int E9 = analogRead(5); 
int W9 = analogRead(4); 
int dtime9= analogRead(1);
int tol9= analogRead(0);
if (E9 < 10 || E9 > 1000 )
{
  E9= preE ;
  }
  if (W9 < 10 || W9 > 1000 ) 
  {
    W9= preW ;
    }
    if (dtime9 < 10 || dtime9 > 1000 ) 
    {
      dtime9= predtime ; 
      }
      if (tol9 < 10 || tol9 > 1000 )
      {
        tol9= pretol ; 
        }
        delay(100);
        int E10 = analogRead(5);
        int W10 = analogRead(4); 
        int dtime10= analogRead(1);
        int tol10= analogRead(0); 
        if (E10 < 10 || E10 > 1000 ) 
        {
E10= preE ; 
}
if (W10 < 10 || W10 > 1000 ) 
{ 
  W10= preW ;
  } 
  if (dtime10 < 10 || dtime10 > 1000 ) 
  { 
    dtime10= predtime ; 
    } 
    if (tol10 < 10 || tol10 > 1000 ) 
    {
      tol10= pretol ; 
      }
      //Average the ten readings 
  int E=(E1+E2+E3+E4+E5+E6+E7+E8+E9+E10)/10;//light intensity in east 
  int W =(W1+W2+W3+W4+W5+W6+W7+W8+W9+W10)/10; // light intensity in west 
  int dtime=(dtime1+dtime2+dtime3+dtime4+dtime5+dtime6+dtime7+dtime8+dtime9+dtime10)/10; 
  int tol = (tol1+tol2+tol3+tol4+tol5+tol6+tol7+tol8+tol9+tol10)/10;// tolerance value 
  int delaytime = (dtime *10);// increase the delay by magnitude of 10
  int tolerance = tol / 40; 
      // display the readings through serial monitor 
      Serial.println("Light intesity in the East"); 
      Serial.println(E); 
      Serial.println(" "); 
      Serial.println("Light intesity in the west"); 
      Serial.println(W);
      Serial.println(" "); 
      Serial.println("delay time");
      Serial.println(delaytime); 
      Serial.println(" "); 
      Serial.println("tolerance");
      Serial.println(tolerance);
      //check the difference and open the coresponding relays 
      
      // check the diffirence of light intensity in east and west 
      
      int diff = E - W; 
      
      // check if the diffirence is in the tolerance else rotates the motor 
      
      if (-1*tolerance > diff || diff > tolerance) 
      { 
        if (E> W)// check if light intensity in east is greater than in west
        { 
          if ( x <30)// check counter to ensure the motor does not run infinitely 
          {
            Serial.println("Turn motor towards east"); 
            digitalWrite(5, LOW);// Turn ON Relay 1 
      
            delay(2000); 
            digitalWrite(5,HIGH); //Turn OFF Relay 1 
           
            ++x; 
            } } 
            
            //check if light intensity in west is greater than in east 
            else if (W > E) 
            {
              if (x > -30) 
              {
Serial.println("Turn motor toward west"); 
digitalWrite(9, LOW);// Turn ON Relay 4 
 
delay(2000); 
digitalWrite(9, HIGH);//Turn OFF Relay 4 

--x; // decrease counter
          }
        }
      }


 

//store the values for the next loop
preE = E; 
preW = W; 
pretol= tol; 
predtime = dtime; 
Serial.println(" ");
Serial.println("--------------------------------------------------------------------- "); 
// delay the next loop 
delay(1000);
}

Isn't
text
really
difficult to
read
when
it weaves
all
over the
screen?
Use the IDE's auto-format tool.

Give your pins some nice names - it'll make your code easier to understand

int E=(E1+E2+E3+E4+E5+E6+E7+E8+E9+E10)/10;//light intensity in east 
  int W =(W1+W2+W3+W4+W5+W6+W7+W8+W9+W10)/10;

Arrays. We love arrays.

Hello thank you for your advise it was very important I already did the auto-format and my codes now looks wow... so how to use arrays?can you please help me on that?

Joe71993:
so how to use arrays?can you please help me on that?

I usually hate this kind of replies but this is really calling for a lmgtfy link... "C arrays".

I'm afraid there is a larger issue here than just not using arrays. The same lengthy code sequence is also repeated 10 times. That should be a function that is called 10 times. When averaging a series of quantities, there is no need to save 10 intermediate results just to add them at the end.

Also there is a strange premise that readings will be somehow "unreasonable".

 // Check if the readings reasonable, otherwise set them to previous ones.

Why? What would make it unreasonable? How does replacing it with an estimate help?

The tolerance potentiometer code is strange too. Why would you need to read and average it 10 times as well? Surely it's not changing when you take readings. Same thing with the delay time potentiometer...

Your variable names are too short and cryptic, it makes the code hard to decipher.

Use a standard indentation to clearly show the code blocks. Never put more than one statement per line. Place any brackets by themselves on a separate line. Use blank lines sparingly, no more than one at a time. Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read. Another is to give things descriptive names. Use descriptive variable names, for example "temperature" instead of "t". You can name numerical constants, pin numbers, variables and many other things in this way. For example, you can refer to a pin and an output level by number, like digitalWrite(3,0). But such a statement doesn't reveal anything about the purpose. digitalWrite(hornRelayPin, LOW) does. You can do that by declaring const byte hornRelayPin = 3; before setup() in your program. Many such names are already defined for you by the compiler and the Arduino IDE. Here are some:

#define HIGH 0x1
#define LOW  0x0
#define PI 3.1415926535897932384626433832795

Use them. There are many more. Use compiler math to compute values so you can see where they came from (or at least document them). For example, if you see the number 73, you would be hard put to explain the significance of it. But if you see "daysPerYear/5", it is obvious. One more thing. When you work on program continuously, you become familiar with it. So many things seem obvious even if they are not spelled out explicitly. But try looking at your own code six months later. It will be as if a stranger wrote it. So write for strangers, not yourself.

Loop can be highly simplified and indeed no arrays needed.

What are those pre- values supposed to do, really? You can use them as initial value, but you can also just not change the values at all in the function… like this (code may have typos and so, didn’t try to compile):

loop() {
  ETotal = 0;
  WTotal = 0;
  dtimeTotal = 0;
  tolTotal = 0;
  for (i=0, i<10, i++) {
    takeReading(E, W, dtime, tol);
    ETota += E;
    WTotal += W;
    dtimeTotal += dtime;
    tolTotal += tol;
    delay(100);
  }
  //Average the ten readings
  EAvg = ETotal/10;
  WAvg = WTotal/10;
  dtimeAvg = dtimeTotal/10;
  tolAvg = tolTotal/10

  // continue original code.

}

void takeReading(int* W, int* E, int* dtime, int* tol) {

  //read the east LED
  int E1 = analogRead(5);
  if (E1 >= 10 && E1 <= 1000 ) E = E1;

  //read the west LED
  int W1 = analogRead(4);
  if (W1 >= 10 && W1 <= 1000 ) W = W1;

  // read the delay time potentiometer
  int dtime1 = analogRead(1);
  if (dtime1 >= 10 && dtime1 <= 1000) dtime = dtime1;

  //read the tolerance potentiometer
  int tol1 = analogRead(0);
  if (tol1 >= 10 && tol1 <= 1000) tol = tol1;
}

The above is equivalent to the first 220 or so lines of your code in loop(). Using pointers the takeReading() function updates the variables - and only if they are “reasonable” so in the 10-1000 range, otherwise the previous value is kept which is what your comments say you actually want.

Also do a bit about your style. Be consequent in where to add spaces (around parenthesis, commas, etc), comments with proper grammar and punctuation, white lines before comments, that kind of things make your code more readable overall.

thank you it helped me