Notifications
Clear all

Help with code please

56 Posts
6 Users
16 Reactions
5,410 Views
(@ianns)
Member
Joined: 4 years ago
Posts: 18
Topic starter  

This code controls a solar tracker, I have borrowed it from youtube.

The project has two ldr's, an Arduino Nano and a servo motor.

The solar tracker is see-sawing backwards and forwards, I know that one of the ldr's is reading a 100 more than the other. I have increased the threshold from 20 to 600 just to stop it see-sawing.

A chance for me to learn more would be gratefully received.

 

#

include <Servo.h>
Servo servo1;

int serv = 0;

int sensorPin1 = A1;
int sensorPin2 = A0;
int sensorValue1 = 0;
int sensorValue2 = 0;

int counter = 0;
int valAverage1 = 0;
int valAverage2 = 0;
int numAverage = 10;

int threshold = 600;


void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
servo1.attach(4);
}

void loop() {
// put your main code here, to run repeatedly:

valAverage1 = 0;
valAverage2 = 0;

for (counter = 0; counter < numAverage; counter++) {
sensorValue1 = analogRead(sensorPin1);
sensorValue2 = analogRead(sensorPin2);

valAverage1 = valAverage1 + sensorValue1;
valAverage2 = valAverage2 + sensorValue2;
}

valAverage1 = valAverage1 / numAverage;
valAverage2 = valAverage2 / numAverage;
delay(45);
Serial.print(valAverage1, DEC);
Serial.print(",");
Serial.println(valAverage2, DEC);

if (valAverage2 > valAverage1 + threshold)
{
if (serv < 112) {
serv += 1;
servo1.write(serv);
}
} else if (valAverage1 > valAverage2 + threshold)
{
if (serv > 4) {
serv -= 1;
servo1.write(serv);
}
}
}



   
Quote
Inq
 Inq
(@inq)
Member
Joined: 3 years ago
Posts: 1904
 
Posted by: @ianns

This code controls a solar tracker, I have borrowed it from youtube.

The project has two ldr's, an Arduino Nano and a servo motor.

The solar tracker is see-sawing backwards and forwards, I know that one of the ldr's is reading a 100 more than the other. I have increased the threshold from 20 to 600 just to stop it see-sawing.

A chance for me to learn more would be gratefully received. 

Welcome to the forum.

It sounds like you were able to solve your problem yourself.  So... exactly what aspect of your "chance to learn" are you interested?  The chasm in front of you is pretty large.

Reason I asked if you are really only interested in the solar tracker... I recall seeing a YouTube or some article on a solar tracker that used a motor, but no digital computing.  It was totally an analog device that was brilliant in its simplicity.  I might need to dig it up (or someone else here might have it bookmarked).

VBR,

Inq

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2587
 

@ianns

Welcome to the forum.

Another alternative is to (manually) point the tracker at the sun so that both LDRs are completely exposed. Then point them at something which is unlit (or dimly lit) and measure them again.

At that point you'll have upper and lower limits for their sensitivity. So then, during normal operations, take the values measured for sensor A and B and use the Arduino map(current reading for A, lowRangeForA, highRangeForA, 0, 1023); and similarly for sensor B. This will map both of them into the range 0-1023 but now scaled to their sensitivity.

You can use these resulting values (which are now directly comparable) to determine which direction to move.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@ianns)
Member
Joined: 4 years ago
Posts: 18
Topic starter  

@inq I have not fixed it.

It is not working as intended

This post was modified 3 years ago by IanNS

   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7782
 

@ianns It would help if you used the auto format option in the IDD under Tools.

A picture of the setup would help, I expect to see a small board pointed straight up, a solar sensor either side. Adjusting how far away from the board and the timer to trigger the measurement event is critical.

Remember, the sun travels 360 degrees in 24 hrs, therfore 15 degrees in 1 hr or 60 mins, that means 1 degree in 4 minutes. I would set a timer for 20 mins so you get some strong difference between the sunny and shaded sensor. Then simpy drive the motor the right way until they are siilar rewadings. No need for super accuracy, I would settle for 80% to 120%.

If you overdo it you burn as much energy as you gain. I don't even tilt my solar panels unless I am going to be in one spot for a while and I am facing the right way. In fact in the sunniest time of the year where I live flat is better since the early and late sun is actually north of us. Use the website 

https://pvwatts.nrel.gov/pvwatts.php

to calculate your exact solar input, try different angles and see what is best.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7782
 

@ianns You need to provide some details, telling us it doesn't work is no help. Since you 'borrowed' the code, you have little to no intellectual familiarity with it so troubleshooting will be hard.

If I were in that situation I would write pseudo code from the 'borrowed' code and then return the borrowed code and write all new code from the pseudo code. That way your intellectual familiarity is high and you will either be able to fix it yourself or at least explain what is wrong to those trying to help.

Does that make sense to you?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
(@ianns)
Member
Joined: 4 years ago
Posts: 18
Topic starter  

I would really like somebody to explain the intention of the code and advise whether I can correct the difference between the two ldr's. Would the correction stop the see-sawing backwards and forwards.

The author of the code has not responded to my email.

If somebody can explain the code I can then learn from it.

Yours hopefully

Ian

 

 


   
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 3 years ago
Posts: 1904
 

@ianns - What @will said sounds like a great reason.  I'm sure the design is expecting the two sensors to be matched and thus get the same readings under the same light conditions.  Also...

  1. I like pictures, video even better.  Maybe a start might be the YouTube you mentioned.  
  2. Compared to the video, is yours identical.... hardware, configuration software?
  3. As far as behavior...  is it wild swings or just twitching back and forth when pointing at the sun?  (rough guestimate the angle)

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7782
 

@ianns Don't you think you would learn more if you read and understood the code?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
(@ianns)
Member
Joined: 4 years ago
Posts: 18
Topic starter  

@inq Mine is slightly different, the lady that wrote the code had her ldr's mounted on the panel centre line  of the pivot with a divider in between.

(Science with Jessica) on YouTube

I mounted mine at either end of the panel.

As soon as the servo gets to one end of it's travel it returns to the other end.

 


   
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 3 years ago
Posts: 1904
 
if (valAverage2 > valAverage1 + threshold)
{
    if (serv < 112)
    {
        serv += 1;
        servo1.write(serv);
    }
} 
else if (valAverage1 > valAverage2 + threshold)
{
    if (serv > 4) 
    {
        serv -= 1;
        servo1.write(serv);
    }
}

 

This part of the code looks a little flaky to me.  If I remember correctly, you're looking to reach 90 degrees.  So, I would have thought the "4" and the "112" to be about the same distance apart from 90.  (86 vs 22 seems wrong)  I would think maybe the 112 should be something around 176  to be the opposite of "4"  ????

 

 

 

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7782
 

@ianns I just grabbed the code and formatted it. Yes, it is a twitchy variant. I would add a timer.

If you continuously adjust the panel, you will likely gain nothing, the very slight gain in panel output will be used to run the motors. Put a 20 minute delay in, that's only 5 degrees. You will want to play with the distance between the LDR's and the separator board, the closer together they are the more precise the zero point will be but can result in missing the mark under the right conditions.

I would also add a run timer. If a cloud crosses the sun at the right time and place you may miss the difference so make sure the motor only runs for whatever # of secs translates into 5 degrees, my gut hunch is 10 secs. After 10 secs just stop and start another 20 min timer. Then maybe allow a timeout timer of 20 secs for the next step. Get the idea? If you see the 2 sensors acting normal, keep the motor running, but if anything ;looks weird shut it down.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7782
 

@ianns Her set up is correct, yours is not.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7782
 

@inq What has 90 degrees got to do with it? It's just a blocking board, run motor till sensor outputs are equal. Stock solution.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7782
 

Here is what the tracker looks like. When aligned sun shines equally, when the sun moves a shadow is cast.

I see no timers in his code so all the saved power will be expended in constantly adjusting the panel. You only need to adjust every 20 mins or so.

Screen Shot 2022 05 05 at 13.47.29

 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
Page 1 / 4