Atmega 1280 - 20PWM

I need generate 20PWM, 13 PWM i have(hardware)
I need More 7, I read arduino page soo many times but i dont understand.
How I generate 7 PWM, disable analogwrite how ? And activate the pins digital

Tanks

EDIT:
I modify ind i search this, please look and talk to me, if something is wrong

Good My frequency is 80hz~90
I need 50HZ HELP ME !

int servoPin = 22; // servo connected to digital pin 2
int myAngle; // angle of the servo roughly 0-180
int pulseWidth; // servoPulse function variable
void setup()
{
pinMode(servoPin, OUTPUT); // sets pin 2 as output
}
void servoPulse(int servoPin, int myAngle)
{
pulseWidth = (myAngle * 10) + 600; // determines delay
digitalWrite(servoPin, HIGH); // set servo high
delayMicroseconds(pulseWidth); // microsecond pause
digitalWrite(servoPin, LOW); // set servo low
}
void loop()
{
// servo starts at 10 deg and rotates to 170 deg
for (myAngle=10; myAngle<=170; myAngle++)
{
servoPulse(servoPin, myAngle); // send pin and angle
delay(20); // refresh cycle
}
// servo starts at 170 deg and rotates to 10 deg
for (myAngle=170; myAngle>=10; myAngle--)
{
servoPulse(servoPin, myAngle); // send pin and angle
delay(20); // refresh cycle
}
}

Hello jcrazy,

I think answering the question you asked isn't in the best interest, because from what I get from your code, you take the wrong approach. It would be a lot better, if you took a step back and describe your problem. From the code you posted, I assume you're dealing with hobby servos, from the amount I would guess it's some kind of robot. In this case, using PWM is the wrong approach. To handle servos (up to 48 on an Arduino Mega) you should use the Servo-library included with your Arduino software. It takes care in the background of refreshing all servos and you just have to set the target position. have a look at this and scrap the code you're trying to fix. It'll give you better results with less effort.

And please, if you paost code enclose them in code tags. You get those if you press the -button above the line with the smilies.

Korman

I have one hexapod, and my big problem is PWM, I need 20 PWM, but atmega have 14 pwm hardware
I need more, but i dont understand arduino, i have a old pic, and i make 18 pwm software(timmer0), but pic is slow very slow.
Now i switch to better plataform.

My question, how i take 20 pwm on my atmega 1280
I have 14 PWM hardware need more 6, its possible only software ?(this6)
I read something, desactivate pin 9 and 10(Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins. On the Mega, up to 12 servos can be used without interfering with PWM functionality; use of 12 to 23 motors will disable PWM on pins 11 and 12. )
How ?!

I need 20 PWM

Not if all you are doing is driving servos. Servos can be driven far simpler using any pin and the Servo library.

If you are NOT driving servos, what ARE you doing that needs 20 PWM pins?

Of interest, maybe?
http://code.google.com/p/rogue-code/wiki/SoftPWMLibraryDocumentation

jcrazy:
I have one hexapod, and my big problem is PWM, I need 20 PWM,

No, your big problem is that you try to solve the problem the wrong way. Forget about PWM if drive hobby servos. THIS IS THE WRONG WAY TO DO IT! Use like all others who build quadri-, hexa-, octo or other bots the Servo library. With it you can drive on your Arduino Mega up to 48 servos and don't need a single PWM.

And if you insist on being obstinate, look it up yourself in the ATmega1280 data sheet to find out why everyone else uses the Servo library and not hardware PWM for hobby servos.

Have a nice day.

Korman

Yes thats it.
Library servo uses timer for generate pwm right ?
count 0 to 255, its more accuracy..
I'm sure you told me ?

Tanks

jcrazy:
Library servo uses timer for generate pwm right ?
count 0 to 255, its more accuracy..
I'm sure you told me ?

Yes, but it drives 12 servos with every 16-bit timer. So using 4 of them you can control up to 48 servos. And on top of that, you don't have to worry about the timer management, the library does it for you to minimise the timer usage.

As for accuracy, don't worry either: Your hobby servo are mechanically far less accurate than the variations of the signals generated by the Servo library.

Korman

Hi, How i wirte in arduino program the code to generate 20 pwm..

Example:

#include <Servo.h>

Servo servo1,servo2,servo3..................servo20; // ******** This ?
// How i create more using the analogwrite() ???

int pos = 0;

void setup()
{
servo1.attach(9);
servo.......
servo20.attach(20)how pin digital ? ?// This mode ? Sorry I'm trying to understand
}

void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servo1.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
servo1.write(pos);
delay(15); //////And after i write the rest of the code
}
}

Hi, How i wirte in arduino program the code to generate 20 pwm..

You don't need to. Servos don't use PWM.

#include <Servo.h>
 
Servo servo1,servo2,servo3..................servo20;  // ******** This ?

Like that, yes, except for the ......... part...

  servo1.attach(9);
  servo.......
  servo20.attach(20)

If the first servo is associated with digital pin 9, it's unlikely that the 2th servo is associated with pin 20, but that is the general idea.

//////And after i write the rest of the code

Yes, but if you are trying to move 20 servos, the delay()s are going to kill you.

Servo motor use pwm, and now i understand, i need create one code with 2 timmers 16bit
To i generate pwm software....This a hard job
could someone please send me a code showing how to call two 16 bit timer?
and if possible the Presscaler.

Servo motor use pwm

No. No. No. Servo motor do NOT use PWM. Quit saying that they do. They use PPM (pulse position modulation) which is something completely different from PWM (pulse width modulation).

Use the Servo library and forget about PWM.

now i understand, i need create one code with 2 timmers 16bit
To i generate pwm software....This a hard job

So don't do it. You don't need to. The Servo library takes care of everything for you.

jcrazy:
and now i understand, i need create one code with 2 timmers 16bit

No, you don't understand. What you keep repeating is wrong. You don't need to create code with timers to drive servos.

jcrazy:
To i generate pwm software....This a hard job

No, this isn't hard job, this is all solved for you by the Servo library. Go to the documentation and try out the samples until they work and you understand them.

Also, if you have trouble making yourself understood in English, you might want to try the Portuguese or Spanish forum. This might be a little easier for you.

Korman

Ok now i see more, tanks guys !
I look servo library, and examples(knob and sweep)...And i look the code Servo.h
What will kill me is the delay.
I'll look up and research more about it

What will kill me is the delay.

Ask yourself why you need the delay at all. If you are making a hexapod that walks, and you are updating the position of each joint in a timely fashion, the angle that you are asking the servo to move to should not be significantly different from the position it is in. So, just tell the servo to go there. By the time you get done updating 20 servos, and getting the next set of positions computed or read, the servos will have already completed moving.

Unless you've figured out how to compute the new positions faster than anyone else ever has, and you are making the hexapod dance, that is.