Help with add "time" library

I have this code in total below

Code:

// ===================================================================================
// = 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 600 //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 =
// ===================================================================================
byte ServoPos[] = {0,51,102,204,255};

// ===================================================================================
// = Structure to hold state =
// ===================================================================================
struct ServoParams_t
{
byte Position;
int Direction;
} ServoParams;

// ===================================================================================
// = Setup code =
// ===================================================================================
void setup()
{
oServo.attach(9); // Attach the oServo object to use pin 9
LoadSettings(); // Read from the EEPORM
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)
{
NextEvent = now() + (Interval); // Set the next interval time
ServoParams.Position += ServoParams.Direction; // Increment or decrement the servo posistion array pointer
if (ServoParams.Position >= 4 || ServoParams.Position <= 0) ServoParams.Direction = -ServoParams.Direction; // check for range end and swap direction
oServo.write(ServoPos[ServoParams.Position]); // Write value to oServer Object
SaveSettings(); // Store state in EEPROM
}
}

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

Returns these errors.....

pondo:1: error: function definition does not declare parameters
pondo:19: error: 'time_t' does not name a type
pondo.ino: In function 'void setup()':
pondo:48: error: 'NextEvent' was not declared in this scope
pondo:48: error: 'now' was not declared in this scope
pondo.ino: In function 'void loop()':
pondo:57: error: 'now' was not declared in this scope
pondo:57: error: 'NextEvent' was not declared in this scope

I have been told to install the "time" library and have done that and can see it in library's but do I have to add this to the code or does it find it automatically.
I am a total Newby so please excuse my dumbness.
Can some one give me a laymans account of what I need to do.
Thanks
Wayne

This is what I am trying to achieve turning a pipe 45 deg so I will gear down the 180 degrees of the servo by half for more torque on the pipe.
Power comes on. The unit starts at 0 deg and stays there. 5 minutes moves to 10 deg then at 10 minutes to 20 deg, 15 minutes to 30 deg then at 20 minutes sweeps out continuous to 45 deg and back to 30 deg quickly, back to 20 deg for 10 minutes and 10 deg back to 0 to complete the loop.
Ideally this would happen with power switching off an on with a random timer and the unit just stopping wherever it is and then starting where it left off.

Hi Wayne

After installing the library, did you exit and restart the Arduino IDE?

And can you confirm the path to the folder where you installed the library?

In the libraries folder, there should be a folder called "Time". In that folder, I would expect at least two files "Time.h" and "Time.cpp", and typically also a folder called "examples". "Time" must have a capital T to match the library file names.

All the best

Ray

Hi Ray
Thanks for the reply
I have never programed or even played with any of this so it is very very much greek to me but its incredibly interesting and I will get in to it as time goes by.
Right now I just need this code to work so your help is really appreciated.

I the folder i have Docs/libraryies/Time/Time/Examples/ 8 files from Processing to TimeSerialDate
What do I do with these
Do I have to load or add them to to the code that I have or does it find it.

Docs/libraryies/Time/Time/Examples/

Looks like you have a Time folder in the Time folder? Try moving the files up a level, so that the path is ...

Docs/libraries/Time/ (the .h and .cpp files are here)
Docs/libraries/Time/Examples/ (those other folders / files are here)

After moving them, try File - Examples in the Arduino IDE. Do you see an entry for Time example programs?

Yep
I think I now have that

Lib/Time/6 files Data Strings through to Time.cpp and Time.h
Now what do I do
I feel like such a dunce....Ha

Load your program up in the Arduino IDE and try compiling it (CTRL-R). Do you still get error messages?

I just opened the code as before.
went to
Sketch
Import library
In contributed the was a file named time and clicked that.
As far as I can see it added this to the top
#include <Time.h>

So now I have
#include <Time.h>

// ===================================================================================
// = 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 600 //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 =
// ===================================================================================
byte ServoPos[] = {0,51,102,204,255};

// ===================================================================================
// = Structure to hold state =
// ===================================================================================
struct ServoParams_t
{
byte Position;
int Direction;
} ServoParams;

// ===================================================================================
// = Setup code =
// ===================================================================================
void setup()
{
oServo.attach(9); // Attach the oServo object to use pin 9
LoadSettings(); // Read from the EEPORM
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)
{
NextEvent = now() + (Interval); // Set the next interval time
ServoParams.Position += ServoParams.Direction; // Increment or decrement the servo posistion array pointer
if (ServoParams.Position >= 4 || ServoParams.Position <= 0) ServoParams.Direction = -ServoParams.Direction; // check for range end and swap direction
oServo.write(ServoPos[ServoParams.Position]); // Write value to oServer Object
SaveSettings(); // Store state in EEPROM
}
}

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

Returned this problems

sketch_may31a:19: error: 'time_t' does not name a type
sketch_may31a.ino: In function 'void setup()':
sketch_may31a:48: error: 'NextEvent' was not declared in this scope
sketch_may31a:48: error: 'now' was not declared in this scope
sketch_may31a.ino: In function 'void loop()':
sketch_may31a:57: error: 'now' was not declared in this scope
sketch_may31a:57: error: 'NextEvent' was not declared in this scope

Just installed the Time library and compiled your code. Worked OK.

Could you try closing down the Arduino IDE, then restart, open your program. Do not import library again. Just CTRL-R.

Also, in Arduino IDE, can you do File - Preferences. At the top will be the path to the sketchbook folder. Can you please copy and paste that path in a post?

I think Its working
Thanks sooooo much for your help.
I don't know how to add karma but karma to you
Thanks again
Wayne

No problem. Karma is a little green + sign underneath the poster's details in the left margin of each post :wink:

R U still there Hackscribble
I have no idea what I am looking at with this code.
It was written for me.
Do I have to load parameters in to this code to make the server start at 0 deg then after 10 minutes mover directly to position 51 and then 10 minutes later position 102 then 10mins later 204 then ten mins sweep to 255 then back to 204 continuously then time at 10 min intervals to 0 to start the loop again.
I am just a rank beginner.
Are all the beginning dialog up to heading main program loop for my instruction only or are they part of the program.
W

Hi, just :slight_smile:

All the code looks to be needed.

From a quick read, it appears to read previous servo position and direction of travel from EEPROM. Then after 10 minutes, it moves to the next position (either left or right depending on direction). Each time it moves, it saves the latest position and direction in EEPROM. When it gets to the end positions, it changes direction.

The five servo positions are defined by this:

byte ServoPos[] = {0,51,102,204,255};

I think the comment by this means you may need to adjust these when you test it with the real hardware.

I've not used the EEPROM storage handlers myself, so can't say if they are correct.

The program includes an error check in case, when it reads the previous settings from EEPROM, there is a bogus value for direction. However, I don't think it does the same for previous servo position. Probably not a major problem.

The program uses the Time library just to measure 10 minute intervals. This could have been done using the built-in millis() function. That library is more often used if there is a real-time clock attached to the Arduino and the program needs to handle actual time of day, date, etc.

And there is a limit of about 100,000 on how many times you can write a byte in EEPROM before it wears out. I don't think these storage handlers spread out the usage across different parts of the EEPROM. If it writes the settings every 10 minutes, the problem could start to hit after 1 million minutes, which is about 2 years of continuous operation. In practice, I guess it could well be a lot longer than that, unless this program is meant to run 24/7.

It looks as if the time library is not installed correctly. However, instead of fixing that I suggest you get rid of it completely - what you're trying to do is trivial and certainly doesn't need any time libraries to do it. Just use millisecond values directly.

static unsigned long lastTime = 0;
if(millis() - lastTime >= Interval)
{
    lastTime += Interval;
    ServoParams.Position += ServoParams.Direction; // Increment or decrement the servo position array pointer
    if ((ServoParams.Position >= 4) || (ServoParams.Position <= 0))
    {
       ServoParams.Direction = -ServoParams.Direction; // check for range end and swap direction
    }
    oServo.write(ServoPos[ServoParams.Position]);  // Write value to oServer Object
    SaveSettings();  // Store state in EEPROM
}

Note that the argument to Servo.write() is an angle in degrees, and servos don't typically have a range of travel from 0 to 255.

Also, I suggest you constrain the range of the ServoParams.Position value read from EEPROM, as well as direction. Data read from uninitialised EEPROM locations will usually be 255, which will give you an invalid index into your position array. The Arduino API provides a constrain() function which reduces the amount of code needed to do that.

What would happen if the value of Direction read from EEPROM was zero? I think you will find the ternary operator useful to validate Direction and coerce it to +/-1.

Any particular reason for making Direction an int rather than a byte? It's not going to make any difference here, but it's a good idea to get into the habit of choosing the appropriate data type for each variable.

You could declare ServoPos as const to eliminate any possible bugs that tried to modify the content.