Can someone pleeeease help.

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.
What I need is the power to come on and everything starts from where it finished when the power went off.
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));
}

Read how to use this forum, then go back and put code tags round that code.

Simplest way might be to use a rechargeable battery to provide an uninterruptible power supply for the Arduino.

It is not easy to have servos go to a position unless you record the postion in the EEPROM immediately before the power goes off - but how can you foretell that?. And due to the relatively short life of the EEPROM it may not be practical to record the position every time the servo is moved.

how to use the forum

code should look like this

...R

Thanks Robin
I can just have a small power pack run the arduino continuously and the timer and the power to the servo switching on and off.
I am a complete newby to arduino and the forum and it seems I might of upset "Grumpy" ....Sorry... we are not all experts at everything in life. :blush:
I think at the moment the code that I paid a member to write for me has it writing to eeprom every time.
Can this be deleted as the arduino is running 24/7 and will the servo go to or find the new position where ever the arduino is at and then continue on from there when the power comes on
Can you tell me what parts to delete or at least guide me.
Thanks for your valuable time.
Wayne

Sorry... we are not all experts at everything in lif

How much of an expert do you have to be to read a post that is called Read this before posting?

whubbard:
I am a complete newby to arduino and the forum and it seems I might of upset "Grumpy" ....Sorry... we are not all experts at everything in life.

You have upset me with your use of "might of". The correct usage is "might have" and "would have" etc

I doubt if you upset Grumpy_Mike but it would help YOU most of all if you take his advice and modify your original post to make the code easier for people here to read. When it looks like you didn't take any trouble to follow the guidelines many people just pass on to the next post. Code should look like this.

I think at the moment the code that I paid a member to write for me has it writing to eeprom every time.
Can this be deleted as the arduino is running 24/7 and will the servo go to or find the new position where ever the arduino is at and then continue on from there when the power comes on
Can you tell me what parts to delete or at least guide me.
Thanks for your valuable time.

As far as I know the only way to "delete" the EEPROM data is to over-write it. If you are just planning to ignore all the EEPROM data that is hardly necessary. I think the default value for the bytes in the EEPROM in a brand new chip is 255.

...R

whubbard:
I am a complete newby to arduino and the forum and it seems I might of upset "Grumpy" ...

He's not the only one.

A useful title helps people, who might know how to help you, choose to read the thread. Not "Can someone pleeeease help.". Everyone wants help, right?

How to use this forum

Please edit your post, select the code, and put it between [code] .. [/code] tags.

You can do that by hitting the # button above the posting area.

Thanks for the English lesson and the rules of engagement in relation to how to use the forum
My bad!