Let's Make Robots!

For Loop trouble

Hey everyone,

I have a small programming dilemma. I'm working on a school project where I have to build a solar tracking platform on which solar cells can be mounted. The setup I have is a solitary panel, with phototransistors on top and botton. All of this is mounted pan/tilt style using servos. I tried to set up a for loop that would iterate through each servo position and take a reading from the phototransistor, and at the end the servo would jump back to the position where the transistor had the lowest analog reading (the most light).

This is the code I had:

  for(int i = 0; i <= 180; i++) {



    if(analogRead(0) < highV) {

      highV = analogRead(0);

      tPos = i;




It seems like I can't be too far off, but I've already spent like an hour messing around and I'd rather just focus on the build instead of debugging. I just want the servo to make one sweep (it keeps restting and sweeping), and after the sweep return to the position where the analogRead(0) value is lowest. 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

You already got good answrs. But here are my 2 cent since I am also preparing a sloar tracker.

You don't really need that precise. Let your microcontroller check the light on the left and right sensor each 5 or 10 minutes like birdmun said. The sun is not that fast moving and this littel change in 10 minutes will not have a huge impact on the efficiency of your solar station. In this way you will save a lot of power for the system since the servo is just moving during the checking cycle.

Once your sensors send different readings during the checking you can move the servo in the corrected position.

In the time between the check you can power the MC down or let it do some other things.

What I see is that you ...
  • make 181 steps instead of your expected 180 due to the <=
  • read the analog input twice. The analog value could have changed already from the first to the second reading.
  • jump from 181 degrees to 0 degrees and give the servo 50 millies time for this. That might not be enough time depending on your servo.

But as TeleFox said: it looks right.
What exactly is your problem?

Most of the solar trackers I have seen are just simple analog setups. If you must use a microcontroller to track the sun, might I suggest you use a similar setup that the analog trackers do?

Focus on the phototransistors and the divider. The divider is tall enough to keep the transistors from both seeing a strong input unless they are pointed at the sun.

You could use the transistors to a pair of analog inputs and when they are unequal, +/- a deadband, you adjust the panel until they are close. As the sun moves about 1 degree every 4 minutes, as I recall, you should be able to do many other things with the microcontrolller without concern of missing full sunlight.

Everything you've posted looks about right, so there must be something missing - can you post the full code from top to bottom?

It'd also be good to know what type of system you're actually programming =)