Go Down

Topic: Help with a basic code (Read 13933 times) previous topic - next topic

whubbard

Thanks Pete
I was reading and following these and should be OK
I have to do do some stuff for an hour.
Be back and I will have a crack.
W

Bainesbunch

no worries it is my bedtime now I'll catch up in the morning

Cheers Pete.
EmbeddedAT .. From Concept to Prototype to Production
Electronics and firmware design and project mentoring
I do answer personal requests for help when accompanied with a PayPal receipt ;)

whubbard

Pete
I have it going now but it just goes left to right about 160 degrees every 5 seconds continually.
I have changed the time to 5 seconds for testing
I am not sure if this has anything to do with it.
Wayne

whubbard


Here is the loaded code



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

Bainesbunch

#19
May 31, 2014, 08:30 am Last Edit: May 31, 2014, 12:33 pm by Bainesbunch Reason: 1
Hi Wayne,


Like I explained in the message i sent to you with the code  you have to adjust the values in the array to match the positions that you want to see

byte ServoPos[] = {0,51,102,204,255};  each of thes represents 5,10,15,20,25 min intervals and then it moved back down again.

Cheers Pete.
EmbeddedAT .. From Concept to Prototype to Production
Electronics and firmware design and project mentoring
I do answer personal requests for help when accompanied with a PayPal receipt ;)

whubbard

Thanks Pete
Your assuming that I know something about programing....I dont!
I am the biggest dumb ass you will find...Sorry
I need to plug and play and then I might be able to spot the important entry's and change them but at the moment the code you gave me moves the servo back and forth 180 deg instead of what I asked for.
Please review the parameters I sent you and see if you can make my servo go from 0 deg step say 3 times to about 130 deg and then sweep out to 180 deg and back to 130 in say 10 seconds or what ever and then step backwards say in 3 steps back to 0 deg. then return to the loop.
Maybe I am asking too much
I am not sure if this is able to be done but that's what I have needed all along and happy to pay as soon as I can load this program and my servo stop stop starts and swings and stop stop back.
Sorry again buddy but this is dutch to me.
I have no time and not an expert on this stuff.
If you can write it or give be a blow by blow instruction that a six year old can understand we are set like jelly.
Thanks
Wayne
Wayne

Bainesbunch

#21
May 31, 2014, 02:42 pm Last Edit: May 31, 2014, 02:52 pm by Bainesbunch Reason: 1
OK so here it is for a "dumb ass" :)

in the code you need to look for the line that looks like this

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

Now in simple terms this is a list of the location that the servos will move to and then back through.

Since I dont have your servo I CANNOT give you the ACTUAL numbers you need to put in here.

However it is a simple process to replace each number (by trial and error) with one that moves the servo to the position that you  want it to be for that stage of the pattern.

Now looking at the values that are in the array by default (the ones i used) you have (assuming a 10 second step time)
@ 0  Seconds 0
@ 10 Seconds 51
@ 20 Seconds 102
@ 30 Seconds 204
@ 40 Seconds 255

What you need to be doing is changing the value that the servo gets for each of the time slots

The easy way to do this is to use the sweep program and work out the limits of the servo.

as a guess i would try using the following values

byte ServoPos[] = {0,10,20,30,45};

These values should move it according to your ORIGINAL requirement of 0-45 deg and back in 5 steps


Cheers Pete.
EmbeddedAT .. From Concept to Prototype to Production
Electronics and firmware design and project mentoring
I do answer personal requests for help when accompanied with a PayPal receipt ;)

whubbard

OK now I feeling really dumb.
I have tried to change those values
If I change the.....

Global variable to hold time and interval

that works perfectly...No probs.

If I make changes to the values as below

// =                        Array to hold servo positions                   =
// =   these are the servo PWM signals that you will need to tune to your servo      =
// ===================================================================================
byte ServoPos[] = {1,2,3,4};

I get a sweep of 160 deg........
I then changed it to .......

byte ServoPos[] = {1,10,20,30};

Same 160 sweep........

I had a go at changing the values in the below sections between brackets and outside brackets and rebooted each time and got different results but nothing I could make sense of...........

=                        Setup code                                  =
// ===================================================================================

// ===================================================================================
// =                     Main Program loop      
}
Am I required to make changes or entries to these sections or are they set in concrete.
Again I can only say sorry.
This is like reading a book in Italian.....You write perfect Italian but I can only speak a few words and a few words doesn't let me understand the story.
If as you say all I need to do is change the values in the ........                        

Array to hold servo positions                   =
// =   these are the servo PWM signals that you will need to tune to your servo      =

Then this is not working at all. If i need to change these and insert them in to the Main Program Loop then I need (now 2 year old) instructions for a super dumb asssss!


Thanks for your patience.
Wayne Dumbo.

Bainesbunch

OK I tell you what i will set this up on my UNO with a servo and video it for you. I am not about today daytime but i will get to it this evening for you.

Cheers Pete.
EmbeddedAT .. From Concept to Prototype to Production
Electronics and firmware design and project mentoring
I do answer personal requests for help when accompanied with a PayPal receipt ;)

whubbard


whubbard

Any Joy Pete
I am itching to give you your money...
Wayne

Bainesbunch

Hi Wayne,

Sure sorry i got caught up with the family on the weekend . I will get to it today for you.

Cheers Pete.
EmbeddedAT .. From Concept to Prototype to Production
Electronics and firmware design and project mentoring
I do answer personal requests for help when accompanied with a PayPal receipt ;)

Bainesbunch

#27
Jun 02, 2014, 05:53 pm Last Edit: Jun 02, 2014, 06:03 pm by Bainesbunch Reason: 1
Hi Wayne,

OK so here is the code again tested against my Futaba RC servo that I use in my camera gimbal on my quatcopter.

Attached and in the thread.

It is running a full 90 deg rotation.

If you only want 45 deg then divide the values in the array by 2

I.E. int ServoPos[] = {1000,1080,1160,1240,1320};

It is currently moving every second using the timing variable that you have already worked out how to adjust.

Have Fun

Cheers Pete.

Code: [Select]

// ===================================================================================
// = 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 1 //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[] = {1000,1160,1320,1480,1640};

// ===================================================================================
// = 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 > 4) ServoParams.Position = 4; // 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
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
Serial.println(ServoPos[ServoParams.Position]);
}
}


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

EmbeddedAT .. From Concept to Prototype to Production
Electronics and firmware design and project mentoring
I do answer personal requests for help when accompanied with a PayPal receipt ;)

Bainesbunch

Hello Wayne,

Another version of the code with the last step in the sequence sweeping into and from the last step in 10 seconds attached and inline for you.

Code: [Select]

// ===================================================================================
// = 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 1 //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[] = {1000,1080,1160,1240,1320};

// ===================================================================================
// = 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 > 4) ServoParams.Position = 4; // 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 == 4 )
{
Serial.println(ServoPos[ServoParams.Position]);
int CurrentPos = ServoPos[3];
int DestPos = ServoPos[4];
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));
}



Cheers Pete.
EmbeddedAT .. From Concept to Prototype to Production
Electronics and firmware design and project mentoring
I do answer personal requests for help when accompanied with a PayPal receipt ;)

Bainesbunch

HI Wayne ... is this now doing what you wanted it to ?

Cheers Pete
EmbeddedAT .. From Concept to Prototype to Production
Electronics and firmware design and project mentoring
I do answer personal requests for help when accompanied with a PayPal receipt ;)

Go Up