I've posted this in another sub forum, but I am hoping maybe THIS is the place for code specific help? If not, mods please put this where it needs to go.
So, I am pretty new and novice with arduino stuff. The most I have done is copy and modify code for LEDs, and a Halloween skull that follows people using phototransitors.
I am currently looking at a cosplay project that requires 8 servos (MG995), with 4 pairs all moving different directions and different angles.
So far I am seeing that the PAC6985 interface should help with the number of servos, but I am a little over my head with programming these.
Here is what the servos will need to do when a momentary button is pressed:
"Top left" - rotate CCW 34°
"Top right" - rotate CW 34°
"Bottom left" - rotate CCW 31°
"Bottom right" - rotate CW 31°
When button is not pressed, I think I can just have them all at 90.
They also need to initiate at the "not pressed" states, as the servos are mounted on their sides, and do have a restricted range of motion, with the arm/horn attached.
I hope that all made sense.
The code below is what I have attempted, but it won't compile because of "expected primary-expression before ) token", at the line "for(int angle =90; )"??
I am not sure what I am doing wrong or how to fix this. I attempted to grab and modify code from Robojax tutorials.
Would anyone be able to assist with the code for this? I am realllly lost with this interface. I could do this through a regular arduino, if I was using less servos, but this interface thing has be baffled.
Code: Select all
#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!
// Watch video V1 to understand the two lines below: http://youtu.be/y8X9X10Tn1k
#define SERVOMIN 125 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX 575 // 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();
}
// the code inside loop() has been updated by Robojax
void loop() {
//watch video for details: https://youtu.be/bal2STaoQ1M
for(int angle =90; )
delay(50);
{
if(digitalRead(2)==LOW){
pwm.setPWM(0, 0, angleToPulse(56) );
pwm.setPWM(4, 90, angleToPulse(56) );
delay(10)
pwm.setPWM(1, 0, angleToPulse(124) );
pwm.setPWM(5, 0, angleToPulse(124) );
delay(10)
pwm.setPWM(2, 0, angleToPulse(59) );
pwm.setPWM(6, 0, angleToPulse(59) );
delay(10)
pwm.setPWM(3, 0, angleToPulse(121) );
pwm.setPWM(7, 0, angleToPulse(121) );
delay(10);
}
else
pwm.setPWM(0, 0, angleToPulse(90) );
pwm.setPWM(4, 0, angleToPulse(90) );
delay(10)
pwm.setPWM(1, 0, angleToPulse(90) );
pwm.setPWM(5, 0, angleToPulse(90) );
delay(10)
pwm.setPWM(2, 0, angleToPulse(90) );
pwm.setPWM(6, 0, angleToPulse(90) );
delay(10)
pwm.setPWM(3, 0, angleToPulse(90) );
pwm.setPWM(7, 0, angleToPulse(90) );
delay(10);
}
// robojax PCA9865 16 channel Servo control
delay(1000);
}
/*
* angleToPulse(int ang)
* gets angle in degree and returns the pulse width
* also prints the value on seial monitor
* written by Ahmad Shamshiri for Robojax, Robojax.com
*/
int angleToPulse(int ang){
int pulse = map(ang,0, 180, SERVOMIN,SERVOMAX);// map angle of 0 to 180 to Servo min and Servo max
Serial.print("Angle: ");Serial.print(ang);
Serial.print(" pulse: ");Serial.println(pulse);
return pulse;
}