LarryD:
This may help:
#include <Servo.h>
unsigned long currentMillis;
unsigned long previousMillis;
unsigned long mydelay = (60601000); //one hour
Servo myservo;
// Control and feedback pins
int servoPin = 9;
int feedbackPin = A0;
// Calibration values
int minDegrees = 0;
int maxDegrees = 180;
int minFeedback = 130;
int maxFeedback = 490;
int tolerance = 11; // max feedback measurement error
int pos = 0;
int luxPin = A1;
int maxLuxReading = 0;
int maxLuxPosition = 0;
//**************************************
void setup()
{
myservo.attach(servoPin);
Serial.begin(9600); // open the serial port at 9600 bps:
previousMillis = millis(); //<<<<<<<< Add this line
} //***** END OF setup() *****
void loop()
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > mydelay)
{
previousMillis = currentMillis; // get ready for next hour
yourfunction(); //Call the code you want to run every hour
}
// do other stuff if you want
} //****** END OF loop() *****
void yourfunction()
{
// put your code you want to run here
} //***** END OF yourfunction() *****
Yes that did help, it actually allowed me to get the timer working using the milli timer instead of the other recommended libraries. It is rather simple, I just had to learn how to use it to call other functions instead of LED's, which I have learned so thats a step in the right direction.
Here is the code that works:
#include <Servo.h>
unsigned long currentMillis;
unsigned long previousMillis;
unsigned long mydelay = (30*1000); //one hour
Servo myservo;
// Control and feedback pins
int servoPin = 9;
int feedbackPin = A0;
// Calibration values
int minDegrees = 0;
int maxDegrees = 180;
int minFeedback = 130;
int maxFeedback = 490;
int tolerance = 11; // max feedback measurement error
int pos = 0;
int luxPin = A5;
int maxLuxReading = 0;
int maxLuxPosition = 0;
//**************************************
void Seek(Servo servo, int analogPin, int pos)
{
// Start the move...
myservo.write(pos);
// Calculate the target feedback value for the final position
int target = map(pos, minDegrees, maxDegrees, minFeedback, maxFeedback);
// Wait until it reaches the target
while(abs(analogRead(analogPin) - target) > tolerance){} // wait...
} //***** END OF yourfunction() *****
//*********************************************************
void setup()
{
Serial.begin(9600); // open the serial port at 9600 bps:
myservo.attach(servoPin);
previousMillis = millis(); //<<<<<<<< Add this line
} //***** END OF setup() *****
//*********************************************************
void loop()
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > mydelay)
{
previousMillis = currentMillis; // get ready for next hour
sweepForPeak(); //Call the code you want to run every hour
}
// do other stuff if you want
} //****** END OF loop() *****
//*********************************************************
int sweepForPeak()
{
for (pos = 0; pos < 180; pos += 5)
{
Seek(myservo, 0, pos);
int luxReading = analogRead(luxPin); // take a sensor reading here
if (luxReading > maxLuxReading)
{
maxLuxReading = luxReading;
maxLuxPosition = pos;
}
Serial.print("luxReading-- ");
Serial.print(luxReading);
Serial.print(" ");
Serial.print("maxLuxPosition -- ");
Serial.print(maxLuxPosition);
Serial.print(" ");
Serial.print("position-- ");
Serial.print(pos);
Serial.println();
delay(400);
}
}
//***** END OF yourfunction() *****
Now I just need to figure out how to drive the servo to the recorded maxLuxPosition after the sweepForPeak for loop finishes the full servo sweep.
Here is what I want the servo to do after the for loop has completed and before it reverts back to the void loop routine: Seek(myservo, 0, maxLuxPosition);
I can't just use a 2nd timer because the times to make the servo do the full scan sweep will vary depending on outside weather conditions since it is powered by a solar panel. I'm using Analog Feedback servos to track the actual position of the servo and to make sure servo actually gets to the position we tell it to reach.
I guess my question is. "How do I make this code Seek(myservo, 0, maxLuxPosition);
run once at the end of this for loop without calling a new function to do it?
int sweepForPeak()
{
for (pos = 0; pos < 180; pos += 5)
{
Seek(myservo, 0, pos);
int luxReading = analogRead(luxPin); // take a sensor reading here
if (luxReading > maxLuxReading)
{
maxLuxReading = luxReading;
maxLuxPosition = pos;
}
Serial.print("luxReading-- ");
Serial.print(luxReading);
Serial.print(" ");
Serial.print("maxLuxPosition -- ");
Serial.print(maxLuxPosition);
Serial.print(" ");
Serial.print("position-- ");
Serial.print(pos);
Serial.println();
delay(400);
}
}
//***** END OF yourfunction() *****