After some thought, I suppose preprogrammed movements for later could also work well for my application. I'm building a small robot arm andwant it to do some basic sequenced movements in a repeated pattern, if that helps (probably should have mentioned it earlier, sorry ). Later, I'd implement it for autonomous function (btw, this is a VERY basic arm, so I'm not into anything too complex ).

Without some kind of external input (analog or digital / photoresistor or basic switch) you can't have anything but preprogrammed movements. Getting the preprogrammed movements to work seems to be a prerequisite to making the movements work in relation some sort of external input.

Without some kind of external input (analog or digital / photoresistor or basic switch) you can't have anything but preprogrammed movements. Getting the preprogrammed movements to work seems to be a prerequisite to making the movements work in relation some sort of external input.

Come to think of it, you're right...Thanks

I suppose now it's just a matter of writting the program. Would it involve moreheader files? Actually, how do you write header files? Are they much different from writing a C program (whenever I get the option to save as a .c file, .h file is included with the option). I suppose this is general knowledge I'm going to have to learn if I'm going to do more advanced robots later on

You allready have an sample program that does "preprogrammed movements". Did you try it? It's just a variation of the original program (the one that centers the servo). You should try it and then come back with questions based on that.

As for header files: technically they are the same thing as "c" files - the "h" is just a convention. Practically you may think of them as something you need to "#include" so you can access functions from a library. In your sample program you had to include the "delay.h" header so you can use the DelayMs() function and the DelayUs() function. You'll only need to write header files of your own when you'll write bigger programs. The "sequenced movements" program is tiny by any measure.

Here's the a quote from the post where I give you the sample program code:

Quote

What the code does. Imagine the two windshield wipers on a car. Imagine you've got two servos, one for each windshield wiper. The first sequence will make the two wipers move from left to right and back, at the same time. It will take 10 seconds to move from left to right and another 10 seconds to move from right to left. When that's done the two servos will start moving in the opposite directions (one "wiper" moves from left to right, the other "wiper" moves from right to left). This will happen 4 times faster then the first example!

Here's a quote from your request (your request was a reply to the post where I'm giving you code):

Quote

Center servo

Loop Forever

for 500 milliseconds servo1 turn right servo2 turn left

pause for 250 milliseconds

FOR 250 milliseconds servo2 go right servo1 center End

Put the code I gave into your MCU and see what it does. Try to modify it so it does what you want. If you encounter difficulties with anything ask again.

I made an error when I wrote that code. The function name I wanted was "DelayMs", not "Delay". Sorry about that. Change the name of the function or use this reviewed code, this time I got around to test it MPLAB, it compiles just fine. I no longer have the circuit built so I'll just assume it also works fine

// This routine takes two parameters, from 0 to 250, that will represent 250 possible positions// for every one of the two servos.void servopulse(unsigned char PulseRA2, unsigned char PulseRA1) { // First pulse RA2 RA2=1; DelayMs(1); DelayMs(PulseRA2); DelayMs(PulseRA2); DelayMs(PulseRA2); DelayMs(PulseRA2); RA2=0; // Now pulse RA1 RA1=1; DelayMs(1); DelayMs(PulseRA1); DelayMs(PulseRA1); DelayMs(PulseRA1); DelayMs(PulseRA1); RA1=0; // Wait for a while; The delay is shorter then the // original delay to account both 2ms pulses DelayMs(16); }

// This is one possible first sequence for the servos. Both servos will move at the same time// to the same positions. I want the full swing from left to right to take about 10 seconds; Since I'll// have 250 steps for each servo, the servo will need to hold each of those 250 steps for// (20 * 1000) / 250 = 40 ms. Since the "servopulse" routine takes about 20 ms I'll call that routine// twice for each of the 250 positions.

void sequence1(void) { unsigned char pos; // swing from left to right for(pos=0;pos<250;pos++) { servopulse(pos,pos); servopulse(pos,pos); } // swing from right to left for (pos=250;pos>0;pos--) { servopulse(pos,pos); servopulse(pos,pos); }}

// This second sequence will make the servos move in oposite directions. This time I want the swing// to take about 2.5 seconds; If I'd stop at each of the 250 possible positions along the way// I'd need to make each position hold for only 10ms. This is not possible because the "servopulse"// routine takes about 20ms to run, so I'll simply skip steps.

I finally managed to throw the circuit back together and tested the code. When I tested it, the servos would only turn in one direction and not anywhere else. I think it's because servopulse is only dictated to go in one direction, so I tried to change it with this result:

// This routine takes two parameters, from 0 to 250, that will represent 250 possible positions// for every one of the two servos.void servopulse(unsigned char PulseRA2) {// First pulse RA2RA2=1;DelayMs(1);DelayMs(PulseRA2);DelayMs(PulseRA2);DelayMs(PulseRA2);DelayMs(PulseRA2);RA2=0;// Wait for a while; The delay is shorter then the// original delay to account both 2ms pulsesDelayMs(16); }void servopulse2(unsigned char PulseRA2){RA2=1;DelayMs(2);DelayMs(PulseRA2);DelayMs(PulseRA2);DelayMs(PulseRA2);DelayMs(PulseRA2);RA2=0;// Wait for a while; The delay is shorter then the// original delay to account both 2ms pulsesDelayMs(16); }

// This second sequence will make the servos move in oposite directions. This time I want the swing// to take about 2.5 seconds; If I'd stop at each of the 250 possible positions along the way// I'd need to make each position hold for only 10ms. This is not possible because the "servopulse"// routine takes about 20ms to run, so I'll simply skip steps.

*****EDIT*****I've fixed that error, but it still doesn't scan. I've also tried to change a few things in the code, but the servo pulses once, then spins all the way to th other end and keeps trying to turn at the limits. Here's the version of the code Im trying:

If it's an new circuit, is it the same circuit? Are you sure?And is it the same project or have you started an new project from scratch?

Your servos are not going to the right place because you've got the timing wrong. The best way to test that (besides using an oscilloscope or an multimeter with a frequency meter function) is to use an LED and make it blink once per second.

Here's a quote from myself

Quote

Open up the "delay.h" file and edit the first line, that's where the MCU clock speed is set up. Change that to read "4Mhz" (don't tuch the zeroes, change the "16" with "4")

Have you got an LED on the circuit you're using for the servos? If I remember well you're using an PicKit1 - but that has no headers for connecting an servo. Put an LED on the circuit you're testing the servos with and make it blink once per second, to make sure your code works and timing is right.

Can you post your current version of the code? I don't remember giving you code that would blink once per second and then go into "pulsing frenzy". If you were to hook a LED to the servo signal line that's just about what you'd see, a blinking frenzy - but you say it starts by blinking at about once per second: that's not a servo signal.

Other then this I'm getting ready to give up No more ideas. It should work!

// This routine takes two parameters, from 0 to 250, that will represent 250 possible positions// for every one of the two servos.void servopulse(unsigned char PulseRA2, unsigned char PulseRA1) { // First pulse RA2 RA2=1; DelayMs(1); DelayMs(PulseRA2); DelayMs(PulseRA2); DelayMs(PulseRA2); DelayMs(PulseRA2); RA2=0; // Now pulse RA1 RA1=1; DelayMs(1); DelayMs(PulseRA1); DelayMs(PulseRA1); DelayMs(PulseRA1); DelayMs(PulseRA1); RA1=0; // Wait for a while; The delay is shorter then the // original delay to account both 2ms pulses DelayMs(16); }

// This is one possible first sequence for the servos. Both servos will move at the same time// to the same positions. I want the full swing from left to right to take about 10 seconds; Since I'll// have 250 steps for each servo, the servo will need to hold each of those 250 steps for// (20 * 1000) / 250 = 40 ms. Since the "servopulse" routine takes about 20 ms I'll call that routine// twice for each of the 250 positions.

void sequence1(void) { unsigned char pos; // swing from left to right for(pos=0;pos<250;pos++) { servopulse(pos,pos); servopulse(pos,pos); } // swing from right to left for (pos=250;pos>0;pos--) { servopulse(pos,pos); servopulse(pos,pos); }}

// This second sequence will make the servos move in oposite directions. This time I want the swing// to take about 2.5 seconds; If I'd stop at each of the 250 possible positions along the way// I'd need to make each position hold for only 10ms. This is not possible because the "servopulse"// routine takes about 20ms to run, so I'll simply skip steps.

}Also, I should mention that when the LEDS pulse once per second, the servos turn a little on each pulse and pauses when the LED is off. But after those three pulses, it just turns and then the pulses gradually slow down.

Anyway, I also did the test for the LED to only blink once per second and it works fine.

This makes a servo move all the way to the left, then pauses. After the pause, it moves all the way to the right...then pauses...and the loop continues.

By changing the delay value in between the statement which makes the output high/low, you could make the servo go to different positions. By changing the value in the for statement, you change the duration of the pause (I think ).

I sincerely thank all who helped or participated in this thread, particulairly cosminprund, for all the explanations, code, and for bearing with my inexperience in C programming.