Go Down

Topic: Newbie Alert!! adafruit 16-channel 12-bit pwm problems (Read 418 times) previous topic - next topic

slugbut

Hi all,

Im just trying to get my head around arduino and managed, with help from here, to control my servos so that 1 worked, had a rest, then the next worked etc. All was good until I connected the nano to the adafruit.

I thought i used the same code!!

Anyway, the servos work following the example code. I've set the max and min sweep of the servos and then thats the limit of my experience!

Code: [Select]
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);

// Depending on your servo make, the pulse width min and max may vary, you
// want these to be as small/large as possible without hitting the hard stop
// for max range. You'll have to tweak them as necessary to match the servos you
// have!
#define SERVOMIN  200 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  550 // this is the 'maximum' pulse length count (out of 4096)


// our servo # counter
uint8_t servonum = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("16 channel Servo test!");

  pwm.begin();
 
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates

  yield();
}

// you can use this function if you'd like to set the pulse length in seconds
// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. its not precise!
void setServoPulse(uint8_t n, double pulse) {
  double pulselength;
 
  pulselength = 1000000;   // 1,000,000 us per second
  pulselength /= 60;   // 60 Hz
  Serial.print(pulselength); Serial.println(" us per period");
  pulselength /= 4096;  // 12 bits of resolution
  Serial.print(pulselength); Serial.println(" us per bit");
  pulse *= 1000;
  pulse /= pulselength;
  Serial.println(pulse);
  pwm.setPWM(n, 0, pulse);
}

void loop() {
  // Drive each servo one at a time
  Serial.println(servonum);
  for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) {
    pwm.setPWM(servonum, 0, pulselen);
  }

  delay(500);
  for (uint16_t pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen--) {
    pwm.setPWM(servonum, 0, pulselen);
  }

  delay(500);

  servonum ++;
  if (servonum > 7) servonum = 0;
}


1 - My servos start at the min value, can they start in the middle and be able to sweep from min to max returning to the middle again?

2 - Is there a dummies guide for newbies like me, I mean, a real dummies guide :D

Any help would be gratefully received.

Thanks,

Jon

MarkT

1 - My servos start at the min value, can they start in the middle and be able to sweep from min to max returning to the middle again?

Have you tried it - there's nothing to stop you starting from an example and trying things,
in fact it can be a good way to learn.
Quote
2 - Is there a dummies guide for newbies like me, I mean, a real dummies guide :D
For what exactly?  Servos?  Adafruit PWM board?  Arduino programming in general?
Quote
Any help would be gratefully received.

Thanks,

Jon
I'd say look at all the examples you can and try to understand them - reading code is
an important skill.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up