Below is code that works nearly!
What I have is a kambrook household plug in timer that switches on 3 things
1 A water pump
2. A 12v powerpack that powers the arduino this directs a servo to sweep 45 degs.
3. A 7.5v powerpack powering the servo.
Problem is that each time the system turns on via the timer the servo swings to the full range clockwise and stops at a position that it is not meant to stop and it has to get to the next signal to move to a new spot before it operates as it should however this is not good because it switches of every 2 triggers so never gets to do the whole swing.
It is probably something simple but I can not see it. It seems that when the power goes of with the timer the whole system forgets where it is until it cycles through to the next signal
Thanks
W
// ===================================================================================
// = library includes =
// ===================================================================================
#include <avr/eeprom.h>
#include <Servo.h>
#include <Time.h>
// ===================================================================================
// = Servo object instatiation =
// ===================================================================================
Servo oServo;
// ===================================================================================
// = Global variable to hold time and interval =
// ===================================================================================
time_t NextEvent;
#define Interval 130
//this is 600 seconds or 10 minutes
// ===================================================================================
// = Array to hold servo positions =
// = these are the servo PWM signals that you will need to tune to your servo =
// ===================================================================================
int ServoPos = {800,850,900,950,1050,1800,};
// ===================================================================================
// = Structure to hold state =
// ===================================================================================
struct ServoParams_t
{
int Position;
int Direction;
} ServoParams;
// ===================================================================================
// = Setup code =
// ===================================================================================
void setup()
{
Serial.begin(9600);
oServo.attach(9); // Attach the oServo object to use pin 9
LoadSettings(); // Read from the EEPORM
if(ServoParams.Position > 5) ServoParams.Position = 5; // just a bit of safety in the case of the EEPROM value being out of range
if(ServoParams.Position < 0) ServoParams.Position = 0; // just a bit of safety in the case of the EEPROM value being out of range
if(ServoParams.Direction > 1) ServoParams.Direction = 1; // just a bit of safety in the case of the EEPROM value being out of range
if(ServoParams.Direction < -1) ServoParams.Direction = -1; // just a bit of safety in the case of the EEPROM value being out of range
NextEvent = now() + (Interval); // Set the first interval time
}
// ===================================================================================
// = Main Program loop =
// ===================================================================================
void loop()
{
if(now() >= NextEvent)
{
SaveSettings(); // Store state in EEPROM
ServoParams.Position += ServoParams.Direction; // Increment or decrement the servo posistion array pointer
if (ServoParams.Position == 5 )
{
Serial.println(ServoPos[ServoParams.Position]);
int CurrentPos = ServoPos[4];
int DestPos = ServoPos[5];
int msSteps = 10000/ (DestPos - CurrentPos);
for(int SweepPos = CurrentPos;SweepPos < DestPos;SweepPos++ )
{
oServo.write(SweepPos); // Write value to oServer Object
delay(msSteps/2);
}
for(int SweepPos = DestPos;SweepPos > CurrentPos;SweepPos-- )
{
oServo.write(SweepPos); // Write value to oServer Object
delay(msSteps/2);
}
ServoParams.Direction = -ServoParams.Direction;
}
else
{
if (ServoParams.Position <= 0) ServoParams.Direction = -ServoParams.Direction; // check for range end and swap direction
oServo.write(ServoPos[ServoParams.Position]); // Write value to oServer Object
Serial.println(ServoPos[ServoParams.Position]);
}
NextEvent = now() + (Interval);
}
}
// ===================================================================================
// = EEPROM Storage Handlers =
// ===================================================================================
void LoadSettings(void)
{
eeprom_read_block((void*)&ServoParams, (void*)(sizeof(ServoParams)), sizeof(ServoParams));
}
void SaveSettings(void)
{
eeprom_write_block((const void*)&ServoParams, (void*)(sizeof(ServoParams)), sizeof(ServoParams));
}