Control Motor alignment according to Solar Alignment

Hi friends :slight_smile:

I know that most of us around here are too good at arduino programming but I am a newbie, and just started working with it. I have worked on some of simple projects, but I am now struck with one of my actual project (for which I started with arduino), the Solar Alignment platform for PV Cells.

I have a very simple configuration, using an Arduino UNO board with a Motor Driver Controller L298D, that controls 2 powerful high torque motor (Not servo but Geared Motors). My light sensor is a voltage divider made of 4 LDR’s, that send signals to 4 analog inputs. I coded most of the part to align the face of LDR’s to exact location of the light source but I am having a hard time now making a bug get fixed :cold_sweat:. Basically, each motor works individually fine but actually as they don’t know the other motor’s position; so as soon as one motor gets to the point where arduino told it to be, the other motor changes the position of the platform completely so they never seem to get aligned to the light source.

It’s quite similar to the what Geo Bruce did here http://www.instructables.com/id/Arduino-Solar-Tracker/, just the difference is that I am using a motor driver with Geared Motors, instead if Servo Motors, means I cant use the Servo.h for it.

|
O | O

O | O
|

The design of the Sensor is like this, LDR’s starting from the Top Left > Top Right > Bottom Left > Bottom Right are marked as a0, a1, a2, a3 respectively

Here is my Code:

//MOTOR 1
int ENA=7;//connected to Arduino's port D7
int IN1=5;//connected to Arduino's port D5
int IN2=3;//connected to Arduino's port D3

//MOTOR 2
int ENB=13;//connected to Arduino's port D13
int IN3=11;//connected to Arduino's port D11
int IN4=9;//connected to Arduino's port D9

int sensitivity = 0; // Used to set the sensitivity of the LDR's
void setup()
{
  
  Serial.begin(9600);
  
 pinMode(ENA,OUTPUT);
 pinMode(ENB,OUTPUT);
 pinMode(IN1,OUTPUT);
 pinMode(IN2,OUTPUT);
 pinMode(IN3,OUTPUT);
 pinMode(IN4,OUTPUT);
 
 //CODE FOR INIT MOTOR 2
		digitalWrite(ENB,HIGH);
		digitalWrite(IN3,LOW);
		digitalWrite(IN4,LOW);
 
  //CODE FOR INIT MOTOR 1
		digitalWrite(ENA,HIGH);
		digitalWrite(IN1,LOW);
		digitalWrite(IN2,LOW);
}
void loop()
{
  int a0 = analogRead(A0);
  int a1 = analogRead(A1);
  int a2 = analogRead(A2);
  int a3 = analogRead(A3);
  
  int a01 = a0+a1;
  int a23 = a2+a3;
  int a12 = a1+a2;
  int a03 = a0+a3;
  
    
  if(a01-a23 > sensitivity || a01-a23 < -sensitivity)// vertical adjustment
  {
    if(a01<a23)
    {
     digitalWrite(IN3,HIGH);
     digitalWrite(IN4,LOW);//Setting Motor 2's Direction clock-wise
    }
    else if(a01>a23)
    {
      digitalWrite(IN3,LOW);
     digitalWrite(IN4,HIGH);//Setting Motor 2's Direction anticlock-wise
    }
  }
  else
  {
     digitalWrite(IN3,LOW);
     digitalWrite(IN4,LOW);//Stop Motor 2
  }
  
  if(a12-a03 > sensitivity || a12-a03 < -sensitivity)// horizontal adjustment
  {
    if(a12<a03)
    {
		digitalWrite(IN1,HIGH);
		digitalWrite(IN2,LOW);//Setting Motor 1's Direction clock-wise
    }
    else if(a12>a03)
    {
       digitalWrite(IN1,LOW);
       digitalWrite(IN2,HIGH);//Setting Motor 1's Direction anticlock-wise
    }
  }
  else
  {
		digitalWrite(IN1,LOW);
		digitalWrite(IN2,LOW);//Stop Motor 1
  }
}

Before looking at your code can you describe your mechanical system. Perhaps post a drawing of it?

If the motor for one axis upsets the other axis it sounds like a problem with the mechanical design.

...R

Hey Robin2... :slight_smile:

Well the design is similar to the link I posted Arduino Solar Tracker : 5 Steps (with Pictures) - Instructables. One motor is mounted on the other and the sensor is mounted to this second motor. The problem is that, as I have written code for both to get aligned at the same time and probably that is what I hope is causing the problem. One motor starts to align according to its position, while at the same time, the second motor is looking and setting its own position. This causes the sensor to never get aligned.

I may be barking up the wrong tree, but you appear doing some odd things in your code

e.g

 if(a01<a23)
    {
     digitalWrite(IN3,HIGH);
     digitalWrite(IN4,LOW);//Setting Motor 2's Direction clock-wise
    }
    else if(a01>a23)
    {
      digitalWrite(IN3,LOW);
     digitalWrite(IN4,HIGH);//Setting Motor 2's Direction anticlock-wise
    }

in this code you don’t ever appear to stop Motor 2
i.e your code doesn’t do anything if a01==a23

You can probably code this as

 if(a01!=a23)
{
      digitalWrite(IN3,(a01<a23));// uses boolean test of a01<23 to make the HIGH or LOW (which are just TRUE or FALSE)
     digitalWrite(IN4,!(a01<a23));// Opposite of the test above i.e !
}
else
{
// I presume that if a01==a23 you want to stop this motor or it will jitter all the time ?????
     digitalWrite(IN3,LOW);
     digitalWrite(IN4,LOW);//Stop Motor 2
}
 int a0 = analogRead(A0);
  int a1 = analogRead(A1);
  int a2 = analogRead(A2);
  int a3 = analogRead(A3);
  
  int a01 = a0+a1;
  int a23 = a2+a3;
  int a12 = a1+a2;
  int a03 = a0+a3;

I can see why a0 and a1 as one side and a2 and a3 as the other side for one axis.
But using your layout shouldn't it be
a02 = a0+a2
a13= a1+a3
ie parallel sensors for the other axis.
your a12 and a03 are crossed units.

I can draw a diag if you like.

Tom..... :slight_smile:

I had a quick look at your code. I think something much simpler should be sufficient.

Assuming A0 and A1 are the two LDRs on one axis - and assume higher values = more light

If A0 > A1 it means the device needs to move towards A0. This would mean code in loop() like this

void loop() {
   int deadband = 50; // to stop unnecessary movement for small changes
   int leftVal = analogueRead(A0);
   int rightVal = analogueRead(A1);

   if (leftVal - rightVal > deadband) {
      movePanel(left);
   }
   if (rightVal - leftVal > deadband) {
      movePanel(right);
   }
}

My idea is that the function movePanel() will just move the panel a degree or two each time.

...R