Help with code please

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));
}

I haven't studied your code - it is long and it is not in code tags to make it look like this

If your servo problem arises when the Arduino is switched on it may not be trivial to solve.

One of the problems is how will the Arduino know where the servo was when the Arduino was powered off - especially if the power was cut without a proper shut-down procedure.

At startup the Arduino will command the servo to move to some position. You can set a position with servo.wite() before you do servo.attach().

When a servo is powered up but is not receiving a control signal it can behave unpredictably. I have found it helpful to connect a 4700 ohm resistor between the servo signal line and ground.

...R

Schematic of servo ?
Since when do servos run on 7.5V ?
Post a link for your servo.

I see you're saving your settings in EEPROM. Is that working correctly? (If it wasn't, how would you know?)

Have you considered what would happen if ServoParams.Direction == 0?

You don't move the servo to the last saved position in setup(). I would have thought you ought to do this before you attach the servo, so that it remains at the position it was in last time it ran. This is probably why it is initially jumping to the wrong position.

Wouldn't it make more sense to save the updated settings as soon as you complete the servo movement, rather than waiting until the start of the next movement?

I don't understand why you're using the time library. As far as I can see, all you're doing is running code at fixed regular intervals, and all you need to do that is millis().

click the MODIFY button in the upper right of the post window.
Highlight all you code.
click the "#" CODE TAGS button on the toolbar above just to the left of the QUOTE button.
click SAVE (at the bottom).
When you post code on the forum, please make a habit of using the code tags "#" button.

PeterH:
I see you’re saving your settings in EEPROM. Is that working correctly? (If it wasn’t, how would you know?)

Have you considered what would happen if ServoParams.Direction == 0?

You don’t move the servo to the last saved position in setup(). I would have thought you ought to do this before you attach the servo, so that it remains at the position it was in last time it ran. This is probably why it is initially jumping to the wrong position.

Wouldn’t it make more sense to save the updated settings as soon as you complete the servo movement, rather than waiting until the start of the next movement?

I don’t understand why you’re using the time library. As far as I can see, all you’re doing is running code at fixed regular intervals, and all you need to do that is millis().

I am a complete novice and had a need so had someone help me with this but am loving trying to get my head around it
You are absolutely right with all of that.
I just need basic start “where it finished” wait there for (sec) move to the next spot(sec)4 or 5 stops to say 30 deg. and then sweep out slowly to 45deg. and back to 30deg to continue the 4 or 5 stops in revers and when the power goes off then it just stops and starts where it finished.
I would really appreciate your help and my skill level will be able to see the difference in the two and I can make adjustments in timings and deg but I am not yet good enough to write what you are are saying.
Thanks for your time so far

skitch_email.png

Number one: In setup, don't attach the servo until you have read back the current settings and set the servo to the correct initial position.

Thanx