Dean Beams – Solar Tracker

This question comes from Dean Beams as a part of a personal project

Question:

A little difficult during this busy time of year to stay focused on the training.  I still try to get a little bit every week.
I have been working on a couple of arduino projects,  a solar tracker for solar panels and a moisture sensor system for the greenhouse.  Having trouble with the solar tracker.  Can’t get the program to break out of the if statement.
Your arduino course is excellent.  Very informative and not too difficult. 
 Including my coding for the solar tracker.
Best regards,
Dean

Dean’s Code:

/*
   Run two motors for suntracker, motors turn solar panels to face direct sun rays both horizontal and verticle
   4 photo resistors LDR
   1 L298N motor driver
   4 resistors 10K
   2 power supply
   2 dc motors
   1 breadboard
   several jumper wires
   Horizontal Motor Motor A
   Orientation- Looking at the solar panels from same direction as the sun.
   Left = West = forward = motor A
   UP = panels sun at steeper angle in the sky (summer) = motor B
   bottom = panels facing sun more toward horizon (winter)
   Layout of photo resistors
   1 topleft ldrTL
   1 topright ldrTR
   1 bottom left ldrBL
   1 bottom right ldrBR
*/
const byte ldrTL = A0;  //green wire
const byte ldrTR = A1;  //white wire
const byte ldrBL = A2;  //blue wire
const byte ldrBR = A3;  //yellow wire

//Horizontal Motor A
const int enableH = 3;  //green
const byte inH3 = 4;     //blue
const byte inH4 = 5;     //violet
//Verticle Motor B
const int enableV = 9;  //black
const byte inV1 = 8;     //white
const byte inV2 = 7;     //gray

int tol = 25;

void setup()
{
  pinMode(enableH, OUTPUT);
  pinMode(inH3, OUTPUT);
  pinMode(inH4, OUTPUT);
  pinMode(enableV, OUTPUT);
  pinMode(inV1, OUTPUT);
  pinMode(inV2, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  int topLeft = analogRead(ldrTL);
  int topRight = analogRead(ldrTR);
  int bottomLeft = analogRead(ldrBL);
  int bottomRight = analogRead(ldrBR);

  Serial.print("topleft");
  Serial.println(topLeft);
  Serial.print("topRight");
  Serial.println(topRight);
  Serial.print("bottomLeft");
  Serial.println(bottomLeft);
  Serial.print("bottomRight");
  Serial.println(bottomRight);


  int avgLeft = (topLeft + bottomLeft) / 2;
  int avgRight = (topRight + bottomRight) / 2;
  int avgTop = (topLeft + topRight) / 2;
  int avgBottom = (bottomLeft + bottomRight) / 2;

  int dhorz = abs(avgLeft - avgRight);
  int dvert = abs(avgTop - avgBottom);

  Serial.print("dhorz");
  Serial.println(dhorz);
  Serial.print("dvert");
  Serial.println(dvert);



  if ((avgLeft - tol) > avgRight)
  {
    digitalWrite(enableH, HIGH);
    digitalWrite(inH3, HIGH);
    digitalWrite(inH4, LOW);
    delay(500);
    digitalWrite(enableH, LOW);
    digitalWrite(inH3, LOW);
    digitalWrite(inH4, LOW);
  }
  else if ((avgRight - tol) > avgLeft)
  {
    digitalWrite(enableH, HIGH);
    digitalWrite(inH3, LOW);
    digitalWrite(inH4, HIGH);
    delay(500);
    digitalWrite(enableH, LOW);
    digitalWrite(inH3, LOW);
    digitalWrite(inH4, LOW);
  }

  if ((avgTop - tol) > avgBottom)
  {
    digitalWrite(enableV, HIGH);
    digitalWrite(inV1, HIGH);
    digitalWrite(inV2, LOW);
    delay(500);
    digitalWrite(enableV, LOW);
    digitalWrite(inV1, LOW);
    digitalWrite(inV2, LOW);
  }
  else if ((avgBottom - tol) > avgTop)
  {
    digitalWrite(enableV, HIGH);
    digitalWrite(inV1, LOW);
    digitalWrite(inV2, HIGH);
    delay(500);
    digitalWrite(enableV, LOW);
    digitalWrite(inV1, LOW);
    digitalWrite(inV2, LOW);

  }

}

Your Challenge:

What would you do to troubleshoot this code?

Solution:

Here’s what I thought:

[fvplayer src=”https://vimeo.com/351103672″ transcript=”auto” splash=”https://i.vimeocdn.com/video/802591050_1280x720.jpg?r=pad” caption=”Dean Beam – Solar Tracker”]

Your Thoughts:

It can be tough finding an error in the code sometimes!   Have you ever been stuck on a tough to find bug?

1 Comment

  1. Steven Orcutt on July 8, 2020 at 1:57 am

    Could the panels be programmed to move relative to the time of day versus the use of the ldr’s ? Then the program could be fine tuned for seasonal changes as well as day light savings time changes.

Leave a Comment