How to improve my code

Hello everyone! I have been developing a project using steppers motor to move a solar panel, called Solar Tracker! I’ m using two motors to get the axes in motion (one motor for horizontal axis and other, for the vertical axis). So, in my code, for controlling both axes, I put the same structure twice. It means I have written the function Tracking and Potentiometer twice (one for each axis), changing only the names for the commands. I would like to optimize my code, by using the same structure (and function ) for both axis. In this situtation, it’s better we have only one function for controlling the two axis, not two different functions, as I mentioned earlier. For example, how can I join the functions Rastreamento and Rastreamento1 and join the MediaMovel and MediaMovel1?


int pinoDrive[6] = {9, 10, 8, 2, 3, 4}; // stepper pin//direction pin//Reset Pin//Mode 0 pin//Mode 1 pin// Mode 2 pin//
pinoDrive[0] == 9;
pinoDrive[1] == 10;
pinoDrive[2] == 8;

int pinoLDRS [4] = {0,1,2,3}; //LdrE Pin// LdrD Pin// LdrE1 Pin// LdrD1Pin//
pinoLDRS[0] == 0;
pinoLDRS[1] == 1;
pinoLDRS[2] == 2;
pinoDrive[3] == 3;

const int nstepsMin = 10;
const int nstepsMax = 25;
const float numbersStepsMin = 1;
const float numbersStepsMax = 3453;
const float mintolerance = 2;
const float maxtolerance = 60;
const int stepDelay = 1000; //time between steps//

boolean dir;

int readingsE[NUMREADINGS];
int readingsD[NUMREADINGS]; //number of readings
int readingsE1[NUMREADINGS];
int readingsD1[NUMREADINGS];
int indice = 0; // current index
int leituraE, leituraD, leituraE1, leituraD1; // variable to store sensor readings//
float totalD, total E, totalD1 , totalE1 = 0; // total moving average//
float error;
int newCustom ;

float ldrEvalue, ldrDvalue, ldrE1value, ldrD1value= 0;
float difldr,difldr1= 0; // difference between the two LDR’s in the same axis//
const int K = 1.28;
const int K1 = 1.44;
const float angulominimo = -169.73;
const float angulomaximo = 169.73;

int contadorpinodrive; // used to store the pins attached to Driver
int contadorpinoLDR; //used to store the pins attached to the LDR sensor
int nP = 6; // number of pins attached to the DRV8825
int nLDR=4; // number of LDR’s (2 for each axis)

void setup() {

for (contadorpinodrive=0;contadorpinodrive<nP;contadorpinodrive++) {
pinMode(pinoDrive[contadorpinodrive], OUTPUT); // Drive attached to the digital pins //

for (contadorpinoLDR=0; contadorpinoLDR<nLDR; contadorpinoLDR++){
pinMode(pinoLDRS[contadorpinoLDR], INPUT); // Sensor attached to the analogic pins//

digitalWrite(Mode0,HIGH); /// DRV8825 step modes (1/8 mode)
digitalWrite(Mode1, HIGH);
digitalWrite(Mode2, LOW);

for (int i = 0; i < NUMREADINGS; i++){ //numbers of readings in moving average//
readingsE, readingsD, readingsE1_, readingsD1 = 0; // start all the reading with 0//
void loop()
void mediamovel () { //do 7 readings to obtain a average value for the LDR’s readings //
totalE -= readingsE[indice];
totalD -= readingsD[indice]; // substract the last reading //
leituraE = analogRead(pinoLDRS[0]); // read the sensor//_

_leituraD = KanalogRead(pinoLDRS[1]); // read the sensor//_

_readingsE[indice] = sqrt(10analogRead(pinoLDRS[0]));// use of a square function to equalize the reading//
readingsD[indice] = sqrt(10*analogRead(pinoLDRS[1]));
totalE += readingsE[indice]; // add reading to total //
totalD += readingsD[indice]; //
indice = (indice + 1); // go to the next //
if (indice >= NUMREADINGS) { // if the index is in the end of the vector//
indice = 0; } } // … it comes back to the begin of the index (or 0)
ldrEvalue = (totalE)/ NUMREADINGS;
ldrDvalue = (totalD)/ NUMREADINGS; // // calculate the moving average //

void mediamovel1 () { // it works at the same way that the previous function (mediamovel)
totalE1 -= readingsE1[indice];
totalD1 -= readingsD1[indice];
leituraE1 = analogRead(pinoLDRS[2]);_

_leituraD1 = K1analogRead(pinoLDRS[3]);_

_readingsE1[indice] = sqrt(10analogRead(pinoLDRS[2]));_

_readingsD1[indice] = sqrt(10analogRead(pinoLDRS[3]));
totalE1 += readingsE1[indice];
totalD1 += readingsD1[indice];
indice = (indice + 1);
if (indice >= NUMREADINGS)
indice = 0;
ldrE1value = (totalE1)/ NUMREADINGS;
ldrD1value = (totalD1)/ NUMREADINGS;


void rastreamento (){ // this function is responsible for tracking the sun, by making comparison between the two LDR’s in one axis//
for(int i=1;i<=7;i++)

mediamovel(); // call for the function seven times to fill in the matrix of moving average and generate the average value from the readings //

float medValue = ((ldrEvalue + ldrDvalue)/2) ; // calculate the average value between the LDRs located in opposite sides//
difldr = (ldrEvalue - ldrDvalue); // calculate the difference between the average value from each side (left or right) //_

_float correction = 100(difldr/medValue);
error = abs(correction); //value for making comparison to minimum thresold//
while(error > mintolerance)

mediamovel(); // call for the function moving average //
float medValue = ((ldrEvalue + ldrDvalue)/2) ;
difldr = (ldrEvalue - ldrDvalue);_

_float correction = 100(difldr/medValue);
error = abs(correction);
digitalWrite(pinoDrive[2],HIGH); // turn on the RESET Pin//
if (correction<0)
dir = HIGH; if the signal of the correction is negative, the motor wil rotate to one direction
dir = LOW; if the signal of the correction is positive, the motor wil rotate to other direction
newCustom = map(error,mintolerance,maxtolerance,nstepsMin,nstepsMax); // function to generate the numbers of steps acording to the error (thresold)//
digitalWrite(pinoDrive[1],dir); //set the Direction Pin//
for(int i =1; i<=newCustom;i++){ //number of iterations acording to the numbers of steps//
digitalWrite(pinoDrive[0], HIGH); // turn on the Step Pin//
digitalWrite(pinoDrive[0],LOW); // turn off the Step Pin//

void rastreamento1 (){ it works at the same way as the previous function (rastreamento)//
for(int i=1;i<=7;i++)
float medValue = ((ldrE1value + ldrD1value)/2) ;
difldr1 = (ldrE1value - ldrD1value);_

_float correction = 100(difldr1/medValue);
error = abs(correction);
while(error > mintolerance)
float medValue = ((ldrE1value + ldrD1value)/2) ;
difldr1 = (ldrE1value - ldrD1value);
float correction = 100*(difldr1/medValue);
error = abs(correction);
if (correction<0)
dir = HIGH;
dir = LOW;
newCustom = map(error,mintolerance,maxtolerance,nstepsMin,nstepsMax);
for(int i =1; i<=newCustom;i++){
digitalWrite(pinoDrive[0], HIGH);
*_</em></em> <em><em><em>**</em></em></em> <em><em>_*

First learn how to use code tags.
Second, learn how to indent & format your code so it’s easy to read. The IDE has an autoformat function (tools menu)

Now, this is correct! Thank you for the tips

luciocr: Now, this is correct! Thank you for the tips

Not quite! Give the code tags another try.

Next have a look at the very basic errors in your code:

Braces that don't match. Functions defined inside other functions. Comments that don't start with // Undefined variables. I don't know what's going on with pinoDrive[] and pinoLDRS[] on lines 4-6 & 9-12 but it doesn't compile.

Maybel uciocr has it correct but it still has not been posted correctly.



readingsE, readingsD, readingsE1, readingsD1 = 0; // start all the reading with 0//

probably does not do what is expected, even after the italics and the indices have been corrected.


indice = 0;      }         }             // ... it comes back to the begin of the index (or 0)

(two right curly brackets) looks suspicious but may be OK.

I would like to know if it is possible to have only one code for the function Rastreamento, for reading the four LDR's, instead of two functions Rastreamento (each function reads two LDR's) . I want to avoid repeating the same structure twice, so it would be nice to know how to do this, only interchanging the commands that refers to a specific axis!