dual axis solar tracker + pro mini 3.3 volts + two dc motors + 4 photo resistors

I'm trying to do dual axis solar tracking

I've modified geo bruces code to work with dc motors instead of hobby servo motors.

I'm using an arduino pro mini 3.3 volts
I'm powering it with a 6 volt solar cell (3.5 watts)

Two dc motors are being used (one for azimuth rotation and the other for elevation rotation)

One l298 drok dual h bridge driver
(powered via 10 watt solar cell --> 10 watts/24 volts = .417 amps (theoretical))

I'm using the attached code.
It use to work before for my 24 volt dc motors.
(JGB37-520) --> .8 amps (stall current)
http://www.manchaimccb.com/products/aslong-jgb37-520-gear-motor-micro-dc-geared-motor-6v-12v-24vdc/

I remade the circuit for ease of assembly that's when it stopped working properly.
The serial outputs are correct and displaying in arduino ide serial monitor.

Now I switched back to 6 volt motors to try to get it to work it doesn't.
(TS-32GZ370-5300) --> .66 amps (stall current)
http://www.tsinymotor.com/Products/Worm%20Gear%20Motors/2014/0512/15.html
I switched because they have more torque for future applications.

Then I switched back to the 24 volt motors to try out the application and it still doesn't work.

I was guessing I'm not getting enough peak amperage based on the motor stall torque current/start up current ratings I'm assuming my 10 watt ALEKO solar cell is providing sufficient amperage. I'm using a lm2596 voltage buck converter and dropping the voltage down from 44 volts to 24 volts or 6 volts depending on which motors I use.

I heard online that the l298 dual h bridge has lots of voltage drop at higher currents so I'm going to try a better driver with higher peak amperage. So I've come to the conclusion that it's either the driver or my code that is not causing one of my motors to move. It could also be that my 10 watt solar cell is not delivering enough peak amperage to start both motors at the start time.

Any input as to what's causing only one motor to move and not the other. The motor that doesn't move is the azimuth motor ?

I plan to release all code and cad files on instructables and grabcad respectively as soon as things are working properly.

DUAL_AXIS_SOLAR_TRACKING.ino (3.25 KB)

The 24v motor seems to have a stall current of just 0.8 amps so I doubt if the problem is with your L298.

What are you powering the motor with?

Make a simple pencil drawing showing how you have everything connected and post a photo of the drawing.

...R

I have attached a jpeg image of a breadboard schematic.
The pins in the picture may not be the same as the code since I'm using an arduino pro mini in my real setup vs the arduino micro shown in the picture.
The concept is still the same.

I may have answered my own question.... though still confused why the 6 volt motors don't work.

At 24 Volts, I remember using a 24 volt power supply and I think things worked then.

I am using a 10 watt solar cell for my current setup and
stepped down voltage from 44 volts to 24 volts which in theory would give me .417 amps.
Less then the .8 amps current for stall current
(for two motors moving simultaneously that would be 1.6 amps)
which I am assuming is the same thing as the
motor start current (based on what I've researched online, I'm still unsure).
I think I remember things working in this scenario too.

10 watts/ 6 volts = 1.67 amps
(For the 6 volt motors, total combined stall current for both motors would be 1.32 amps)
So theory the drive should work.

I've attached additional pictures for clarity.

Here is the code I'm using:

// 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);
}

Images from Reply #2 so we don't have to download them. See this Image Guide

...R

I am not prepared to comment based on those images - it would be too easy for me to misunderstand them. As I suggested in Reply #1, please make a pencil drawing based exactly on your system and post a photo of the drawing.

This all seems to me to be mixed up

I am using a 10 watt solar cell for my current setup and
stepped down voltage from 44 volts to 24 volts which in theory would give me .417 amps.
Less then the .8 amps current for stall current
(for two motors moving simultaneously that would be 1.6 amps)
which I am assuming is the same thing as the
motor start current (based on what I've researched online, I'm still unsure).
I think I remember things working in this scenario too.

10 watts/ 6 volts = 1.67 amps
(For the 6 volt motors, total combined stall current for both motors would be 1.32 amps)
So theory the drive should work.

First of all, a 10 watt panel will only produce 10 watts in very bright sunshine when aimed directly at the sun and when matched carefully to the load so that it operates at its most effective voltage. I suspect it would be more sensible to assume you panel provides 1 watt and treat any extra as a bonus. Maybe even 1 watt is optimistic.

You have not said how the voltage is stepped down. Many ways of doing so simply do it by deliberately wasting energy.

IMHO there is no prospect of running motors directly from a solar panel without a suitable battery to store energy. When the motor moves it will probably need more current than the panel can provide and that can be supplied by the battery.

The stall current of a motor is the worst-case scenario. In normal operation the current draw is likely to be considerably less.

...R

hi, I've attached a drawn out schematic and the actual soldered and assembled circuit on its side.
the results on the arduino ide serial monitor are reporting properly.

to drop down voltage I'm using a lm2596 buck converter.

cad models of it can be downloaded here:

cad models of the solar panel are here:

I've reposted the 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 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);
}


I was hoping you would use the link i gave in Reply #3 to post your image so I would not have to do it

Image from Reply #5

...R

The image in Reply #5 is just a waste of time. Just post the schematic on its own.

...R

Solar panels dont perform very well at all when fed directly into Buck Converters, because a Solar panel behaves like a current source and a buck converter expects a voltage source.
The problem becomes worse when the solar intensity is low .
You need a battery between the Solar panel and the LM2596.

Don't expect wires poked into the Arduino vias to make a reliable contact.
It might have been better if you had used a breadboard.
Two motor wires seem to be very close together. If they short, the chip could fry.
+1 on the battery buffer. A solar panel is basically a current source.
Leo..

@Robin2 thanks. I get what you mean by posting pictures I overlooked that post. I will update.

@wawa
I have the wires soldered on to the pro mini.
They are single strand 22 gauge wires.

@mauried
I was trying to avoid batterys I wanted basically anyone from having to buy batteries ie if they are in wilderness, off grid, 3rd world remote location, etc... Any chance for alternatives I. E. Capacitors? Using a multi meter I am getting the required voltage can't remember about current though.will measure soon.

well i hooked everything up to a power supply tester.
dr. meter hy3005b

have it set at 24 volts and 1.6 amps
to account for total stall torque amperage rating of 2 motors.
the power supply automatically goes to constant voltage mode
and runs at 24 volts and ~.13 amps

http://www.manchaimccb.com/products/aslong-jgb37-520-gear-motor-micro-dc-geared-motor-6v-12v-24vdc/

the elevation motor moves clockwise
after that nothing else seems to move when i cover the photoresistors.
i am reading my serial outputs and they are correct.

these are the 24 volt motors with a stall torque rating of .8 amps.

any thoughts?
my guess is it's a code issue
so I may have to move this over to the programming section of the forum.

Please don't make major changes to older Replies. Your changes to Reply #5 have invalidated my Replies #6 and #7. Post new material in the correct chronological order so other readers can follow the discussion.

Your updated diagram is still very hard to read and shows no sign of a battery for storing the output of the solar panels even though that was suggested in Replies #4. #8 and #9

...R

Ok got it.

The power supply though works better than the battery so it must mean something is wrong with the program.

knightridar:
The power supply though works better than the battery so it must mean something is wrong with the program.

You have not provided any of the information that would be necessary to make a useful comment about that.

...R

Poor choice of words meant that the power supply works better than the solar panel and voltage buck converter combination.

The power supply allows me to vary voltage between 0 to 30 volts and current between 0 to 5 amps.

Still not nearly enough information.

For example you have described the limits of your power supply but you have not told us what settings you have it at when the project is working.

Post a clear diagram of the circuit for the system that works and another for the system that does not so we can easily compare them.

What sort of battery are you using with the solar panel?
And post a link to the datasheet for the buck converter.

And tell us everything else that might help us to help you.

...R

Okay... it's working now.

I finally bought a 12 volt lead acid battery,

12 volts, 1.3Ah

https://grabcad.com/library/kt-1213-12-v-1-3-ah-agm-lead-acid-battery-1

I bought a solar charge controller to to charge the battery.
MODEL: LMS2430.
BRAND: HOMPIE (not sure if this is reseller or brand?)
12/24 V, 30 A (overkill for my 24 volt, 10 watt panel)

I have the battery hooked up to the charge controller and now I hooked up the LM2596 VOLTAGE BUCK CONVERTER to the battery. The voltage is dropped down from 12 volts to 6 volts to power my 6 volt motors. They are successfully moving.

When I tested my 6 volt motors with the power supply they run at 50 mA at no load (matches with motor data sheet description).

Here is a link to the buck converter I bought:
https://www.amazon.com/gp/product/B0140SW9D8/ref=oh_aui_search_detailpage?ie=UTF8&psc=1

The website below provides more details for the specs of the buck converter:
https://www.importitall.co.za/Qunqi-LM2596-Buck-Converter-4040-to-1337V-Adjustable-StepDown-Power-Module-with-LED-Display-Voltmeter-ap-B0140SW9D8.html

I've also attached a pdf for the lm2596 chip but not the converter itself.

Here is my 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);
}

Thanks!

TS-32GZ370.pdf (263 KB)

LM2596-D.PDF (449 KB)

knightridar:
Okay... it's working now.

Good to hear it.

...R

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)