How can i utilize timer at this coding?

Hello, I'm a student from korea.

I'm looking for someone who are willing to help me.

I'm making a solar tracker to graduate now, and I'm almost done except for one thing.

The thing is, to improve the efficiency of it, i need to use timer.

There is a servo motor to track the light of the sun, and it makes some power loss.

So, I want to activate the motor periodically not in real time.

In other words, I have no idea of how to utilize it at my coding

This is what i made so far. Please refer to this.

 


 

#include <Servo.h>


Servo tracker;  // create servo object to control a servo

int eastLDRPin = 0;  //Assign analogue pins

int westLDRPin = 1;

int eastLDR = 0;   //Create variables for the east and west sensor values

int westLDR = 0;

int error = 0;

int calibration = 5;  //Calibration offset to set error to zero when both

 

sensors receive an equal amount of light

 

int trackerPos = 60;    //Create a variable to store the servo position


void setup()

 

{

  tracker.attach(11);  // attaches the servo on pin 11 to the servo object

}

 

void loop()

 

{

  eastLDR = calibration + analogRead(eastLDRPin);    //Read the value of each of the east and west sensors

  westLDR = analogRead(westLDRPin);

  if(eastLDR<0 && westLDR<0)  //Check if both sensors detect very little light, night time

  {

    while(trackerPos<=160)  //Move the tracker all the way back to face east for sunrise

    {

      trackerPos++;

      tracker.write(trackerPos);

      delay(3000);

    }

  }

  error = eastLDR - westLDR;          //Determine the difference between the two sensors.

 

direction

 

 

if(error>15)        //If the error is positive and greater than 15 then move the tracker in the east direction

    if(trackerPos<=160)  //Check that the tracker is not at the end of its limit in the east direction

    {

      trackerPos= trackerPos+18;

      tracker.write(trackerPos);  //Move the tracker to the east

        else if(trackerPos<=130)

           { trackerPos= trackerPos+9;

   delay(1800000);

  }

 delay(3600000);       

 } 

 

}

 

Hi,

Do you mean you want the servo to not consume any power while it’s not being used ?

If so you would need to shut the power connection to the servo. A timer wouldn’t help you. The software can’t stop it from consuming power.

The only thing you could do is to tracker.deatch() so the servo no longer receives a signal it tries to match thus presumably consuming much less power. I when it should move again you could tracker.attach(pin) again.

I hope that’s what you need and it helps you.

 

opidopi

Arduino periodic tasks

You may want to look into the “Alarm” functions for scheduling periotic tasks. For example:
        Alarm.timerRepeat(5, lampTask);
        Alarm.timerRepeat(2, waterTask);
        Alarm.timerRepeat(60*10, dataLogTask);                                  // log collected data every 10 minutes
        Alarm.timerRepeat(2, sensorTask);                                            // data collected every 10 seco$
        Alarm.timerRepeat(1, timeTask);                                                // update lcd every second
It also requires:
        Alarm.delay(1);                                                                         // wait one second betw$
 
in the main loop. The tasks are written as standard functions. I do not know if there are any special mechanisms for exclusive us of data locks.