dual axis solar tracker via 4 photoresistors and dc motors code cycling issue

hi, after successfully running this program with the proper electronics (i.e. i used a power supply tester with the proper voltage and current settings), i’ve come to the conlusion that something in my code may not be cycling properly.

basically my elevation motor is listed in the loop first that spins clockwise and it briefly spun couterclockwise. however, when i cover up the other photoresistors the results show up in the serial monitor but they azimuth motor never spins and the elevation motor only spins clockwise.

any thoughts?

/ Special thanks to Geo Bruce on instructables.com for his version of the code.
// Enable A and Enable B pins on  dual motors h-bridge must be connected to two pwm (pulse width modulation) pins on arduino uno/micro/pro mini: 3,5,6,9,10,11.
int enA = 3; int in1 = 4; int in2 = 5; // motor azimuth adjustment
int enB = 9; int in3 = 7; int in4 = 8; // motor elevation adjustment

void setup() 
{
 Serial.begin(9600); // initialize the serial port
 pinMode(enA, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(enB, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); // set all the motor control pins to outputs
}
void loop()
{
 // LIGHT SENSOR (in this case a Light Dependent Resistor) pin connections and analog pin on respective arduino board
 int tr = analogRead(0); // top right
 int br = analogRead(1); // bottom right
 int tl = analogRead(2); // top left
 int bl = analogRead(3); // bottom left
 int delaytime = analogRead(A7)*2; // control delay time in milliseconds of LIGHT SENSOR readings
 int tolerance = analogRead(A6)/4; // set range of tolerance between LIGHT SENSOR readings

//print LIGHT SENSOR values to serial monitor for debugging
 Serial.println(tl); Serial.println(bl); Serial.println(tr); Serial.println(br); Serial.println(delaytime); Serial.println(tolerance); Serial.println();

  int count = 0; //start millisecond count of LIGHT SENSOR readings
  count++; //incremental count increase, continues to show LIGHT SENSOR results

  int avt = (tr + tl) / 2; // average value top
  int avd = (bl + br) / 2; // average value down
  int avl = (tl + bl) / 2; // average value left
  int avr = (tr + br) / 2; // average value right
  
  int dv = avt - avd; // average difference of top and bottom LIGHT SENSORS
  int dh = avl - avr;// average difference of left and right LIGHT SENSORS

if (-1*tolerance > dv || dv > tolerance) // check if the difference in top/bottom LIGHT SENSORS is greater than tolerance
{
  if (avt > avd) // if average LIGHT SENSOR values on top side are greater than on bottom side then elevation motor rotates CLOCKWISE
  {
  digitalWrite(in3, LOW);  digitalWrite(in4, HIGH); analogWrite(enB, 200); // set speed out of possible range 0~255 
  }
  else // if average LIGHT SENSOR values on bottom side are greater than on top side then elevation motor rotates COUNTERCLOCKWISE
  {
  digitalWrite(in3, HIGH);  digitalWrite(in4, LOW); analogWrite(enB, 200);
  }
}
  else if (-1*tolerance < dv || dv < tolerance) // if difference is smaller than tolerance, STOP elevation motor
  {
  digitalWrite(in3, LOW); digitalWrite(in4, LOW);
  }

if (-1*tolerance > dh || dh > tolerance) // check if the difference in left and right LIGHT SENSORS is within tolerance range
{
  if (avl > avr) // if average LIGHT SENSOR values on left side are greater than right side, azimuth motor rotates CLOCKWISE
  {
  digitalWrite(in1, HIGH);  digitalWrite(in2, LOW); analogWrite(enA, 200);
  }
  else // if average LIGHT SENSOR values on right side are greater than on left side, azimuth motor rotates COUNTERCLOCKWISE
  {
  digitalWrite(in1, LOW);  digitalWrite(in2, HIGH); analogWrite(enA, 200);
  }
}
  else if (-1*tolerance < dh || dh < tolerance) //if difference is smaller than tolerance, STOP azimuth motor
  {
  digitalWrite(in1, LOW);  digitalWrite(in2, LOW);
  }
  delay(delaytime);
}

Why not add some "Serial.print"s to see if your outputs to motor controller are what's expected?

elevation motor only spins clockwise

If clockwise is down, does the motor have enough "grunt" to raise the panel? EDIT: Or, write a "manual" sketch and work the motors through the serial monitor to see if they're up to the task.

Hi yes I will update the code soon with this thanks.

One other thing, you're starting the motors at reduced PWM (200), you may have to "kickstart" them with a burst of full speed (254) for, say, 30 - 50 mS to get them out of their own tracks, then drop back to desired speed.

Good point I think I had it like this before with the solar panel. Then I thought to myself maybe i am drawing too much current that the panel can't supply it simultaneously for both motors and that's why they can't move. Now I have a power supply to test with (can supply 5 amps and up to 30 volts) so I'll keep everyone posted.

knightridar: hi, after successfully running this program with the proper electronics (i.e. i used a power supply tester with the proper voltage and current settings), i've come to the conlusion that something in my code may not be cycling properly.

If it is not now running with "proper electronics" what is it running with?

It would help if you can post a circuit diagram for the working system and another diagram for the problem system.

If a program was working properly one day but later does not work when you change the power supply why would you assume there is a problem with the program?

...R

I apologize I left a few details out.

There have been a few variables that may have messed me up.
Over time and experimentation i did the following:

  1. I changed the wiring of the circuit from terminal blocks to quick connectors.
  2. I modified the code a little (pwm rates and slight modification to if else statements)

at one point the circuit was working with the solar panel in my backyard.
i had forgotten some of the changes I may have made over time.

I believe edgemoron was right I wasn't spiking the pwm high enough for the current to kick in I believe.
i will test further in the morning.
the code below is working now:

// Special thanks to Geo Bruce on instructables.com for his version of the code.
// Enable A and Enable B pins on  dual motors h-bridge must be connected to two pwm (pulse width modulation) pins on arduino uno/micro/pro mini: 3,5,6,9,10,11.
int enA = 3; int in1 = 4; int in2 = 5; // motor azimuth adjustment
int enB = 9; int in3 = 7; int in4 = 8; // motor elevation adjustment

void setup() 
{
 Serial.begin(9600); // initialize the serial port
 pinMode(enA, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(enB, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); // set all the motor control pins to outputs
}
void loop()
{
 // LIGHT SENSOR (in this case a Light Dependent Resistor) pin connections and analog pin on respective arduino board
 int tr = analogRead(0); // top right
 int br = analogRead(1); // bottom right
 int tl = analogRead(2); // top left
 int bl = analogRead(3); // bottom left
 int delaytime = analogRead(A7)*2; // control delay time in milliseconds of LIGHT SENSOR readings
 int tolerance = analogRead(A6)/4; // set range of tolerance between LIGHT SENSOR readings

//print LIGHT SENSOR values to serial monitor for debugging
 Serial.println(tl); Serial.println(bl); Serial.println(tr); Serial.println(br); Serial.println(delaytime); Serial.println(tolerance); Serial.println();

  int count = 0; //start millisecond count of LIGHT SENSOR readings
  count++; //incremental count increase, continues to show LIGHT SENSOR results

  int avt = (tr + tl) / 2; // average value top
  int avd = (bl + br) / 2; // average value down
  int avl = (tl + bl) / 2; // average value left
  int avr = (tr + br) / 2; // average value right
  
  int dv = avt - avd; // average difference of top and bottom LIGHT SENSORS
  int dh = avl - avr;// average difference of left and right LIGHT SENSORS

if (-1*tolerance > dv || dv > tolerance) // check if the difference in top/bottom LIGHT SENSORS is greater than tolerance
{
  if (avt > avd) // if average LIGHT SENSOR values on top side are greater than on bottom side then elevation motor rotates CLOCKWISE
  {
  digitalWrite(in3, LOW);  digitalWrite(in4, HIGH); analogWrite(enB, 254); // set speed out of possible range 0~255
  Serial.println("ELEVATION MOTOR MOVES CLOCKWISE");
  Serial.println("   "); 
  }
  else // if average LIGHT SENSOR values on bottom side are greater than on top side then elevation motor rotates COUNTERCLOCKWISE
  {
  digitalWrite(in3, HIGH);  digitalWrite(in4, LOW); analogWrite(enB, 254);
  Serial.println("ELEVATION MOTOR MOVES COUNTERCLOCKWISE");
  Serial.println("   "); 
  }
}
  else if (-1*tolerance < dv || dv < tolerance) // if difference is smaller than tolerance, STOP elevation motor
  {
  digitalWrite(in3, LOW); digitalWrite(in4, LOW);
  Serial.println("ELEVATION MOTOR STOPS");
  Serial.println("   "); 
  }

if (-1*tolerance > dh || dh > tolerance) // check if the difference in left and right LIGHT SENSORS is within tolerance range
{
  if (avl > avr) // if average LIGHT SENSOR values on left side are greater than right side, azimuth motor rotates CLOCKWISE
  {
  digitalWrite(in1, HIGH);  digitalWrite(in2, LOW); analogWrite(enA, 254);
  Serial.println("AZIMUTH MOTOR MOVES CLOCKWISE");
  Serial.println("   ");
  }
  else // if average LIGHT SENSOR values on right side are greater than on left side, azimuth motor rotates COUNTERCLOCKWISE
  {
  digitalWrite(in1, LOW);  digitalWrite(in2, HIGH); analogWrite(enA, 254);
  Serial.println("AZIMUTH MOTOR MOVES COUNTERCLOCKWISE");
  }
}
  else if (-1*tolerance < dh || dh < tolerance) //if difference is smaller than tolerance, STOP azimuth motor
  {
  digitalWrite(in1, LOW);  digitalWrite(in2, LOW);
  Serial.println("AZIMUTH MOTOR STOPS");
  Serial.println("   ");
  }
  delay(delaytime);
}

knightridar: I apologize I left a few details out.

There have been a few variables that may have messed me up. Over time and experimentation i did the following:

  1. I changed the wiring of the circuit from terminal blocks to quick connectors.
  2. I modified the code a little (pwm rates and slight modification to if else statements)

at one point the circuit was working with the solar panel in my backyard. i had forgotten some of the changes I may have made over time.

That tells us that you have been "messing around with stuff" but it does not provide any useful information that could form a basis for helping you.

And if you really have forgotten the changes you made the simplest way to get it working may be to start over from scratch. Making more changes on top of unknown changes just tangles the spaghetti even worse.

...R

Turns out it was not a programming issue after all it was just motor power issue and the battery is helping out…

Here is the latest code for reference:

// Special thanks to Geo Bruce on instructables.com for his version of the code.
// Enable A and Enable B pins on  dual motors h-bridge must be connected to two pwm (pulse width modulation) pins on arduino uno/micro/pro mini: 3,5,6,9,10,11.
int enA = 3; int in1 = 4; int in2 = 5; // motor azimuth adjustment
int enB = 9; int in3 = 7; int in4 = 8; // motor elevation adjustment

void setup() 
{
 Serial.begin(9600); // initialize the serial port
 pinMode(enA, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(enB, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); // set all the motor control pins to outputs
}
void loop()
{
 // LIGHT SENSOR (in this case a Light Dependent Resistor) pin connections and analog pin on respective arduino board
 int br = analogRead(0); // BOTTOM RIGHT
 int tr = analogRead(1); // TOP RIGHT
 int tl = analogRead(2); // TOP LEFT
 int bl = analogRead(3); // BOTTOM LEFT
 int delaytime = analogRead(A6)*2; // control delay time in milliseconds of LIGHT SENSOR readings
 int tolerance = analogRead(A7)/4; // set range of tolerance between LIGHT SENSOR readings

//print LIGHT SENSOR values to serial monitor for debugging
 Serial.println(tl); Serial.println(bl); Serial.println(tr); Serial.println(br); Serial.println(delaytime); Serial.println("ms"); Serial.println(tolerance); Serial.println("photoresistor difference"); Serial.println();

  int count = 0; //start millisecond count of LIGHT SENSOR readings
  count++; //incremental count increase, continues to show LIGHT SENSOR results

  int avt = (tr + tl) / 2; // average value top
  int avd = (bl + br) / 2; // average value down
  int avl = (tl + bl) / 2; // average value left
  int avr = (tr + br) / 2; // average value right
  
  int dv = avt - avd; // average difference of top and bottom LIGHT SENSORS
  int dh = avl - avr;// average difference of left and right LIGHT SENSORS

if (-1*tolerance > dv || dv > tolerance) // check if the difference in top/bottom LIGHT SENSORS is greater than tolerance
{
  if (avt > avd) // if average LIGHT SENSOR values on top side are greater than on bottom side then elevation motor rotates CLOCKWISE
  {
  digitalWrite(in3, LOW);  digitalWrite(in4, HIGH); analogWrite(enB, 254); // set speed out of possible range 0~255
  Serial.println("ELEVATION MOTOR MOVES CLOCKWISE");
  Serial.println("   "); 
  }
  else // if average LIGHT SENSOR values on bottom side are greater than on top side then elevation motor rotates COUNTERCLOCKWISE
  {
  digitalWrite(in3, HIGH);  digitalWrite(in4, LOW); analogWrite(enB, 254);
  Serial.println("ELEVATION MOTOR MOVES COUNTERCLOCKWISE");
  Serial.println("   "); 
  }
}
  else if (-1*tolerance < dv || dv < tolerance) // if difference is smaller than tolerance, STOP elevation motor
  {
  digitalWrite(in3, LOW); digitalWrite(in4, LOW);
  Serial.println("ELEVATION MOTOR STOPS");
  Serial.println("   "); 
  }

if (-1*tolerance > dh || dh > tolerance) // check if the difference in left and right LIGHT SENSORS is within tolerance range
{
  if (avl > avr) // if average LIGHT SENSOR values on left side are greater than right side, azimuth motor rotates CLOCKWISE
  {
  digitalWrite(in1, HIGH);  digitalWrite(in2, LOW); analogWrite(enA, 254);
  Serial.println("AZIMUTH MOTOR MOVES CLOCKWISE");
  Serial.println("   ");
  }
  else // if average LIGHT SENSOR values on right side are greater than on left side, azimuth motor rotates COUNTERCLOCKWISE
  {
  digitalWrite(in1, LOW);  digitalWrite(in2, HIGH); analogWrite(enA, 254);
  Serial.println("AZIMUTH MOTOR MOVES COUNTERCLOCKWISE");
  }
}
  else if (-1*tolerance < dh || dh < tolerance) //if difference is smaller than tolerance, STOP azimuth motor
  {
  digitalWrite(in1, LOW);  digitalWrite(in2, LOW);
  Serial.println("AZIMUTH MOTOR STOPS");
  Serial.println("   ");
  }
  delay(delaytime);
}

I just wanted to give anybody that may be interested an update.

I WAS ABLE TO GET IT TO WORK WITHOUT BATTERIES AGAIN!

;D

A few things I’ve changed:

  1. I’m using a BTS7960 43A H-bridge High-power Motor Driver module
    (One for each motor, the L-298 H-bridges are known to inefficient and have voltage drop issues)

  2. My PWM settings are maxed out at 255 now.

  3. I’m not sure if this could have been an issue but there are more pins with this driver vs the L298 and
    there are enable pins for each direction of rotation, I’m wondering if these enable commands may
    have needed to be changed in my previous code using the L298 motor drivers.

Positive outcomes:

1.I’m using new motor drivers that can support up to 43 Amps.

  1. I don’t need to complete a ground connnection from these drivers to the arduino because they have an
    independent MCU. (One less wired connection that I have to make.)

https://www.aliexpress.com/item/Double-BTS7960-43A-H-bridge-High-power-Motor-Driver-module-smart-car/32819713287.html?spm=2114.search0104.3.16.Aeze26&ws_ab_test=searchweb0_0,searchweb201602_3_10152_10065_10151_10068_10130_10084_10083_10080_10082_10081_10110_10178_10137_10111_10060_10112_10113_10155_10114_5360015_10154_438_10056_10055_10054_10182_10059_100031_10099_10078_10079_10103_10073_10102_10189_10052_10053_10142_10107_10050_10051,searchweb201603_1,ppcSwitch_2&btsid=38950507-5a4b-4484-b927-b6aca95b799f&algo_expid=fe72560b-bae7-4787-858d-8ec79aa9426b-2&algo_pvid=fe72560b-bae7-4787-858d-8ec79aa9426b

I am using a small 5V solar panel to power the arduino.
I tried both my small 6V and 12V panels for my 6V motors and 12V motors.
Things move for sure.
Adding a battery would help for cloudy days.
I can tell the motors are not always running at their max torque/speed settings on cloudy days.

Here is my updated code:

// Special thanks to Geo Bruce on instructables.com for his version of the code.
// AZIMUTH and ELEVATION PWM pins on each h-bridge must be connected to FOUR PWM (pulse width modulation) pins on arduino. For uno/micro/pro mini they are: 3,5,6,9,10,11.
int AREN = 2; int ARPWM = 3; int ALEN = 4; int ALPWM = 5; // motor azimuth adjustment
int EREN = 7; int ERPWM = 6; int ELEN = 8; int ELPWM = 9; // motor elevation adjustment

void setup() 
{
 Serial.begin(9600); // initialize the serial port
 pinMode(AREN, OUTPUT); pinMode(ARPWM, OUTPUT); pinMode(ALEN, OUTPUT); pinMode(ALPWM, OUTPUT);  // set all the motor control pins to outputs
 pinMode(EREN, OUTPUT); pinMode(ERPWM, OUTPUT); pinMode(ELEN, OUTPUT); pinMode(ELPWM, OUTPUT);
}
void loop()
{
 // LIGHT SENSOR (in this case a Light Dependent Resistor) pin connections and analog pin on respective arduino board
 int tr = analogRead(0); // top right
 int br = analogRead(1); // bottom right
 int tl = analogRead(2); // top left
 int bl = analogRead(3); // bottom left
 int delaytime = analogRead(A6)*2; // control delay time in milliseconds of LIGHT SENSOR readings
 int tolerance = analogRead(A7)/4; // set range of tolerance between LIGHT SENSOR readings

//print LIGHT SENSOR values to serial monitor for debugging
 Serial.println(tl); Serial.println(bl); Serial.println(tr); Serial.println(br); Serial.println(delaytime); Serial.println("ms"); Serial.println(tolerance); Serial.println("photoresistor difference"); Serial.println();

  int count = 0; //start millisecond count of LIGHT SENSOR readings
  count++; //incremental count increase, continues to show LIGHT SENSOR results

  int avt = (tr + tl) / 2; // average value top
  int avd = (bl + br) / 2; // average value down
  int avl = (tl + bl) / 2; // average value left
  int avr = (tr + br) / 2; // average value right
  
  int dv = avt - avd; // average difference of top and bottom LIGHT SENSORS
  int dh = avl - avr;// average difference of left and right LIGHT SENSORS

if (-1*tolerance > dh || dh > tolerance) // check if the difference in left and right LIGHT SENSORS is within tolerance range
{
  if (avl > avr) // if average LIGHT SENSOR values on left side are greater than right side, azimuth motor rotates CLOCKWISE
  {
  digitalWrite(AREN, HIGH); analogWrite(ARPWM, 255); // set speed out of possible range 0~255
  digitalWrite(ALEN, HIGH);  digitalWrite (ALPWM, LOW);
  Serial.println("AZIMUTH MOTOR MOVES CLOCKWISE");
  Serial.println("   "); 
  }
  else // if average LIGHT SENSOR values on right side are greater than on left side, azimuth motor rotates COUNTERCLOCKWISE
  {
  digitalWrite(ALEN, HIGH); analogWrite(ALPWM, 255);
  digitalWrite(AREN, HIGH); digitalWrite(ARPWM, LOW);
  Serial.println("AZIMUTH MOTOR MOVES COUNTERCLOCKWISE");
  Serial.println("   "); 
  }
}
  else if (-1*tolerance < dh || dh < tolerance) //if difference is smaller than tolerance, STOP azimuth motor
  {
  digitalWrite(AREN, LOW);  digitalWrite(ALEN, LOW);
  Serial.println("AZIMUTH MOTOR STOPS");
  Serial.println("   "); 
  }

if (-1*tolerance > dv || dv > tolerance) // check if the difference in top/bottom LIGHT SENSORS is greater than tolerance
{
  if (avt > avd) // if average LIGHT SENSOR values on top side are greater than on bottom side then elevation motor rotates CLOCKWISE
  {
  digitalWrite(EREN, HIGH); analogWrite(ERPWM, 255);
  digitalWrite(ELEN, HIGH); digitalWrite(ELPWM, LOW);  
  Serial.println("ELEVATION MOTOR MOVES CLOCKWISE");
  Serial.println("   ");  
  }
  else // if average LIGHT SENSOR values on bottom side are greater than on top side then elevation motor rotates COUNTERCLOCKWISE
  {
  digitalWrite(ELEN, HIGH); analogWrite(ELPWM, 255);
  digitalWrite(EREN, HIGH); digitalWrite(ERPWM, LOW); 
  Serial.println("ELEVATION MOTOR MOVES COUNTERCLOCKWISE");
  Serial.println("   "); 
  }
}
  else if (-1*tolerance < dv || dv < tolerance) // if difference is smaller than tolerance, STOP elevation motor
  {
  digitalWrite(EREN, LOW);  digitalWrite(ELEN, LOW);
  Serial.println("ELEVATION MOTOR STOPS");
  Serial.println("   "); 
  }
  delay(delaytime);
}

DUAL_AXIS_SOLAR_TRACKING_USING_BTS7960_H_BRIDGE_DC_MOTOR_DRIVER.ino (4 KB)

Jeez. I have just realized that you have the exact same project in two different Threads (the other one) and I have been wasting my time dealing with both of them.

Don't double post again.

...R

Hi sorry I didn't double post with any bad intent. It's just that I had both hardware and programming issues to deal with for the same project.

It any case I do appreciate your help. Thanks.

I just updated both sections so in case any one runs into similar issues they can have something to refer to both in terms of hardware and programming issues/debugging.

Sir, I try to follow your code for the solar tracker but I don’t understand the follow: int AREN = 2; int ARPWM = 3; int ALEN = 4; int ALPWM = 5; // motor azimuth adjustment int EREN = 7; int ERPWM = 6; int ELEN = 8; int ELPWM = 9; // motor elevation adjustment

If you only have 2 motors, Why 8 connections? 2345 and 7689 ? There are only 4 wires on 2 motors.

Can someone give me schematic please (how to connect the motors on in1,in2,in3 and in4 on a L298N controller)?

I would recommend not using those. the l298 dual h-bridge drivers are inefficient. they created a lot of electrical issues for me. there are other sites mentioning this as well.

  1. you have to complete a ground connection with them to your arduino circuit. with the bts7960 h-bridge driver this is not necessary.

  2. they have voltage drop and this caused problem early on during my project. the 6v solar panels i was using wasn't providing enough of a voltage to the motors.

i was initially using the l298 dual h-bridge drivers then switched over to the bts7960. it works great and is more future proof. it works with higher amperage motors. it's easier to work with. i have not looked into it's efficiency yet. you have to have two of them for this application.

you can download the cad files here: https://grabcad.com/library/bts7960-43a-high-power-h-bridge-module-1

with regards to your first post 8 connections broken down:

4 wires (pwm wires) clock wise and counter clock wise rotation for each motor.

the other 4 wires are the enable pins. they basically send a signal to the driver to allow the pwm to occur. consider them like on / off switches for the pwm. this is my understanding of it.

i'll upload cad files in the near future so you have a clearer idea of how things are connected.

I really do not understand why you reject this circuit for the L298N ?
I have been using this schematic and the sketch for a while and I can not fault it.
I use normal engines with delay use but engines with a reduction … about 100 rpm is good enough.
This means that you do not have to use enA and enB for PWM and only in1 tm in4.
On the motors there is a threaded M8 with a bearing nut which controls the panel horizontally and vertically.
As a result, the displacement is only 1 mm per revolution or 100 mm per minute, if more than enough to adjust the panel quietly and without shocks.

The sketch must be adapted to the use without enA and enB.

Please turn off your capslock … it is now as if you are screaming.

BTW, why do you use count ???

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4); // address 0x27 - 4 rows - 20 character

// motor azimuth L298N
  int in1 = 9;
  int in2 = 10;
// motor elevation L298N
  int in3 = 11;
  int in4 = 12;

void setup() 
{
  Serial.begin(9600); // initialize the serial port
  lcd.init(); // initialize the lcd 

  lcd.backlight(); //ON
  lcd.clear(); //lcd clear
}

void loop()
{

// LIGHT SENSORS 
  int tr = analogRead(0); // LDR top right / brown
  int br = analogRead(1); // LDR bottom right / green
  int tl = analogRead(2); // LDR top left / yellow
  int bl = analogRead(3); // LDR bottom left / orange

// control delay time in milliseconds of LIGHT SENSOR readings
  int delaytime = analogRead(A7)*2;
// set range of tolerance between LIGHT SENSOR readings - Arduino LED indicate time
  int tolerance = analogRead(A6)/4;
 // set range for read 0 - 100%
  int sensetive = map(analogRead(A6), 1022, 0, 0, 100);

 //print LIGHT SENSOR values to serial monitor for debugging
  Serial.println("--------------------");
  Serial.print("Top Left\t");
  Serial.print(tl);
  Serial.println();  
  Serial.print("Bottom Left\t");
  Serial.print(bl);
  Serial.println();  
  Serial.print("Top Right\t");    
  Serial.print(tr);
  Serial.println();  
  Serial.print("Bottom Left\t");  
  Serial.print(bl);
  Serial.println();

  Serial.print("Delaytime\t");
  Serial.print(delaytime);
  Serial.println();
  Serial.print("Tolerance\t");
  Serial.print(tolerance);
  Serial.println();
  Serial.println("--------------------");

  int avt = (tr + tl) / 2; // average value top
  int avd = (bl + br) / 2; // average value down
  int avl = (tl + bl) / 2; // average value left
  int avr = (tr + br) / 2; // average value right
  
  int dv = avt - avd; // average difference of top and bottom LIGHT SENSORS
  int dh = avl - avr;// average difference of left and right LIGHT SENSORS

  if (-1*tolerance > dv || dv > tolerance) // check if the difference in top/bottom LIGHT SENSORS is greater than tolerance
{
  if (avt > avd) // if average LIGHT SENSOR values on top side are greater than on bottom side then elevation motor rotates CLOCKWISE
  {

// set motor Elevation enable
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  Serial.println("ELEVATION MOTOR MOVES UPWARDS");
  lcd.setCursor(0,0);
  lcd.print("ELEVATION :    UP"); // print lcd row 1
  }
  else // if average LIGHT SENSOR values on bottom side are greater than on top side then elevation motor rotates COUNTERCLOCKWISE
  {
// set motor Elevation enable
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  Serial.println("ELEVATION MOTOR MOVES DOWN");
  lcd.setCursor(0,0);
  lcd.print("ELEVATION :  DOWN");// print lcd row 1
  }
}
  else if (-1*tolerance < dv || dv < tolerance) // if difference is smaller than tolerance, STOP elevation motor
  {

// stop Elevation motor
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  Serial.println("ELEVATION MOTOR STOP");
  lcd.setCursor(0,0);
  lcd.print("ELEVATION :  STOP"); // print lcd row 1
  }

  if (-1*tolerance > dh || dh > tolerance) // check if the difference in left and right LIGHT SENSORS is within tolerance range
{
  if (avl > avr) // if average LIGHT SENSOR values on left side are greater than right side, azimuth motor rotates CLOCKWISE
  {

// set motor Azimuth enable
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  Serial.println("AZIMUTH MOTOR MOVES EAST");
  lcd.setCursor(0,1);
  lcd.print("AZIMUTH   :  EAST"); // print lcd row 2
  }
  else // if average LIGHT SENSOR values on right side are greater than on left side, azimuth motor rotates COUNTERCLOCKWISE
  {
// set motor Azimuth enable
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  Serial.println("AZIMUTH MOTOR MOVES WEST");
  lcd.setCursor(0,1);
  lcd.print("AZIMUTH   :  WEST"); // print lcd row 2
  }
}
  else if (-1*tolerance < dh || dh < tolerance) //if difference is smaller than tolerance, STOP azimuth motor
  {
// stop motor Azimuth
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  Serial.println("AZIMUTH MOTOR STOP");
  lcd.setCursor(0,1);
  lcd.print("AZIMUTH   :  STOP"); // print lcd row 2
  }
// print lcd delaytime row 3
  lcd.setCursor(0,2);
  lcd.print("DELAY     :  ");
  char temp[5]; // enough room for 4 numbers
  snprintf(temp, 5, "%4d", delaytime); // %4d = 4 digits, right aligned
  lcd.print(temp);
  lcd.setCursor(18, 2);
  lcd.print("ms"); // time in milliseconds

// print lcd sensitive row 4
  lcd.setCursor(0,3);
  lcd.print("SENSITIVE :   ");
  char temp1[4]; // enough room for 3 numbers
  snprintf(temp1, 4, "%3d", sensetive); // %3d = 3 digits, right aligned
  lcd.print(temp1);
  lcd.setCursor(18, 3);
  lcd.print("%"); // 
  
  delay(delaytime); // wait for next loop
}

hi, i didn't know you could use the l298 without pwm enA and enB options. thanks for the code that could be useful for everyone else and I may try it out too.

I read on other sites the L298 is inefficient. It may have caused a voltage drop when I was powering my motors with a 6V solar panel. The cause for them not working could have been something else. Lots of people on the forum recommended using a battery and it did help and work.

I wanted to try to move the motors without the use of batteries. I ended up getting a larger 6V solar panel that was harder to find but I eventually found it:

https://grabcad.com/library/solar-module-6v-10w-1-6a-pn-cnc340x220-6-1

https://www.aliexpress.com/item/Solar-Module-6V-10W-1-6A-Portable-Module-DIY-Small-Solar-Panel-for-Cellular-Phone-Charger/32831959124.html?spm=a2g0s.9042311.0.0.37854c4dvY10AI

My motors move fine with a good amount of sun.

I would recommend using the L298 for cost effectiveness since it's cheaper than the BTS7960: The bts 7960 is better for larger motors that require more voltage and current.

https://grabcad.com/library/bts7960-43a-high-power-h-bridge-module-1

https://www.aliexpress.com/store/product/Double-BTS7960-43A-H-bridge-High-power-Motor-Driver-module-diy-smart-car-Current-diagnostic/221555_32849254807.html?spm=2114.12010615.0.0.44882c80voOAwR

Here is the original code for reference: http://www.instructables.com/id/Arduino-Solar-Tracker/

I will try out the L298 again myself Another cheap alternative is L9110S I don't know if it's more efficient than the L298 but it's voltage range is not as high.

https://grabcad.com/library?page=1&time=all_time&sort=recent&query=l9110s

I was/am a fan of writing in CAPs but since it's considered screaming I'm not using it. I am noticing though it's easier to code using a both upper case and lower case letters.

I can't recall what the count++ function was for.