Code for 3d printed Iron Man helmet MK5

Hi!
Quite new to Arduino. I have now used bout 8 hours trying to wrap my head around this, but to no avail. I have tried googling and chatgpt aswell.
I am working on a 3d print project from Thingiverse. It's an Iron Man helmet. The code is supplied, but it does not work as expected. I have added a diagram thats shows the wiring. It might be one of those Fritzing diagrams. I hope you bear with me, as I am in no position to make a scheme on my own.

When the button is triggered, it seems like all servos works as expected when they go to the open position. When it's set for closed position, the two MG90S servos works as expected, but the SG90 servos all just keeps spinning.

Hardware:
Arduino Nano
PCA 9658
2x MG90S servo
8x SG90 servo
A pair of flexible led panels for eyes.

Does anyone have a suggestion to where I should look for the problems?

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

// Adjust for global change in small sg90 servo movement //
#define SERVOMIN 150
#define SERVOMAX 450
/////////////////////////////////////////////////////
#define SERVO_FREQ 50

uint8_t servonum = 0;
int buttonPin = 2;
int ledPin = 6;
int buttonState = 0;
int globalPos = 1;
unsigned long previousMillis = 0;
unsigned long interval = 5000;
int a = 60;

int animDelay = 1;
int mainServoL = 0;
int mainServoR = 1;
int servoBrowCenter = 4;

// Adjust below numbers for individual servos
int mainServoClosed = 750;
int mainServoOpen = 1950;
int servoBrowCenterOpen = 40;
int servoBrowCenterClosed = 120;
int servoCheeksOpen = 20;
int servoCheeksClosed = 90;
int servoNoseSideOpen = 10;
int servoNoseSideClosed = 86;
int servoBrowSideOpen = 30;
int servoBrowSideClosed = 90;
int servoNoseCenterOpen = 1;
int servoNoseCenterClosed = 110;


void setup() {
  Serial.begin(9600);
  Serial.println("Boot Up");
  pwm.begin();
  pwm.setOscillatorFrequency(27000000);
  pwm.setPWMFreq(SERVO_FREQ);
  pinMode(buttonPin, INPUT_PULLUP);
  delay(50);
  pwm.sleep();
}

int getAngleToPulse(int angle) {
  return map(angle, 0, 180, SERVOMIN, SERVOMAX);
}

void loop() {

  buttonState = digitalRead(buttonPin);
  if (buttonState == 0) {
    Serial.println("Wake up");
    pwm.wakeup();

    if (globalPos > 0) {
      Serial.println("Opening");
      // CHEEKS
      for (uint16_t pulselen = servoCheeksClosed; pulselen >= servoCheeksOpen; pulselen--) {
        pwm.setPWM(9, 0, getAngleToPulse(servoCheeksClosed + servoCheeksOpen - pulselen));
        pwm.setPWM(8, 0, getAngleToPulse(pulselen));
      }
      delay(animDelay);
      Serial.println("1. Cheeks Open");
      // NOSE Side
      for (uint16_t pulselen = servoNoseSideClosed; pulselen >= servoNoseSideOpen; pulselen--) {
        pwm.setPWM(6, 0, getAngleToPulse(servoNoseSideClosed + servoNoseSideOpen - pulselen));
        pwm.setPWM(7, 0, getAngleToPulse(pulselen));
      }
      delay(animDelay);
      Serial.println("2. Nose Side Open");
      //BROW Center
      for (uint16_t pulselen = servoBrowCenterClosed; pulselen >= servoBrowCenterOpen; pulselen--) {
        pwm.setPWM(servoBrowCenter, 0, getAngleToPulse(pulselen));
      }
      delay(animDelay);
      Serial.println("3. Brow Center Open");
      // BROW Side
      for (uint16_t pulselen = servoBrowSideOpen; pulselen <= servoBrowSideClosed; pulselen++) {
        pwm.setPWM(2, 0, getAngleToPulse(pulselen));
        pwm.setPWM(3, 0, getAngleToPulse(servoBrowSideOpen + servoBrowSideClosed - pulselen));
      }
      delay(animDelay);
      Serial.println("4. Brow Side Open");
      // NOSE Center
      for (uint16_t pulselen = servoNoseCenterClosed; pulselen >= servoNoseCenterOpen; pulselen--) {
        pwm.setPWM(5, 0, getAngleToPulse(pulselen));
      }
      delay(animDelay);
      Serial.println("5. Nose Center Open");
      // EYES
      analogWrite(ledPin, 0); 
      // MAIN SERVOS
      for (uint16_t microsec = mainServoClosed; microsec < mainServoOpen; microsec +=5) {
        pwm.writeMicroseconds(mainServoL, microsec);
        pwm.writeMicroseconds(mainServoR, (mainServoOpen+mainServoClosed-microsec));
      }
      globalPos = 0;
      delay(animDelay);
      Serial.println("6. Main Open");
    } else {
      Serial.println("Closing");
      //MAIN SERVOS
      for (uint16_t microsec = mainServoOpen; microsec > mainServoClosed; microsec-=5) {
        pwm.writeMicroseconds(mainServoL, microsec);
        pwm.writeMicroseconds(mainServoR, (mainServoOpen+mainServoClosed-microsec));
      }
      delay(animDelay);
      //NOSE Center
      for (uint16_t pulselen = servoNoseCenterOpen; pulselen <= servoNoseCenterClosed; pulselen++) {
        pwm.setPWM(5, 0, getAngleToPulse(pulselen));
      }
      
      delay(animDelay);
      
      // BROW Side
      for (uint16_t pulselen = servoBrowSideClosed; pulselen >= servoBrowSideOpen; pulselen--) {
        pwm.setPWM(2, 0, getAngleToPulse(pulselen));
        pwm.setPWM(3, 0, getAngleToPulse(servoBrowSideOpen + servoBrowSideClosed - pulselen));
      }
      delay(animDelay);
      // BROW Center
      for (uint16_t pulselen = servoBrowCenterOpen; pulselen <= servoBrowCenterClosed; pulselen++) {
        pwm.setPWM(servoBrowCenter, 0, getAngleToPulse(pulselen));
      }
      delay(animDelay);
      // NOSE Side
      for (uint16_t pulselen = servoNoseSideOpen; pulselen <= servoNoseSideClosed; pulselen++) {
        pwm.setPWM(6, 0, getAngleToPulse(servoNoseSideClosed + servoNoseSideOpen - pulselen));
        pwm.setPWM(7, 0, getAngleToPulse(pulselen));
      }
      delay(animDelay);
      //CHEEKS
      for (uint16_t pulselen = servoCheeksOpen; pulselen <= servoCheeksClosed; pulselen++) {
        pwm.setPWM(9, 0, getAngleToPulse(servoCheeksClosed + servoCheeksOpen - pulselen));
        pwm.setPWM(8, 0, getAngleToPulse(pulselen));
      }
      delay(animDelay);
      //EYES
      analogWrite(ledPin, 255);
      globalPos = 1;
      delay(100);
      Serial.println("Sleep");
      pwm.sleep();
    }
    delay(500);
  }
  delay(10);
}

Welcome to the forum

Do you mean that they keep spinning through over 360 degrees and do not stop at a fixed angle ?

Yes, they keeps on spinning until i trigger det button again. Then they stop. When triggered, some of them actually makes a little movement in one direction, and then changes direcion and keeps on spinning.

I can also see that not all the pairs behave the same. For example servoBrowSide.
Pin 2: When triggered for closed it starts and spins at a constant speed. When triggered for open it maintains the same speed. After a very short time, it speeds up for about 120 degrees before it stops.

Pin 3: When triggered for closed it it starts and spins at a constant speed. When triggered for open it maintains the same speed. After a short time, it slows down for about 90 degrees before it stops.

Are these servos supposed to be continuous or not? Sounds like you have the wrong type of servo.

Please post a link to the Thingiverse project.

What you have are not servos at all. They are electronically controlled motors with no position feedback. Without that feedback all you can do us to control their rotation direction and speed

This is the link: The link for the project: Iron Man MK5 Helmet by Boxandloop - Thingiverse
I have bougt the same servos as in the part list. Exept for the wing servo. That is changed out with the MG90S servo. The MG90S works as expected.

Did you buy them from the page linked to in the parts list or from somewhere else ?

If it was somewhere else then please post a link to where you got them

Hmm. It says Micro servo 9g SG90 on it, and google says "SG90 is popular micro servo motor commonly used in hobbyist and DIY projects. It is a small, low-cost servo motor that can rotate 180 degrees".

Either way, this is what was included in the parts list of the project and I assume the code was written for them :confused:

I got them of Aliexpress. But they are identical.
https://www.aliexpress.com/item/1005006283358420.html?spm=a2g0o.order_list.order_list_main.59.25341802gRGmhl

Let's try a simple experiment

Disconnect everything from the Arduino except the USB connection
Connect one of your SG90 servos directly to the Arduino 5V and GND pins and connect the servo signal wire to pin 9
Run the Servo Sweep example and observe how the servo moves and report back

NOTE
It is not generally recommended to power servos from the Arduino but it is safe to do a short test like this as long as the servo output is free to move and the servo is not stalled

Well there you are. That listing include continuous (360) as well as regular ones (180). Whether by intent or accident, you have got the wrong ones.

You are absolutley right! I have indeed ordered the 360 one! Thank you som much for helping me out! I will order the 180 and see if I can get this thing going :smiley:

Bobocousins did in fact solve this mystery for me. I did order the 360 servo, and not the 180... I guess I have learned a bit out of this anyway! Thank you for helping me out!

I have build the same project but i have problem with brow center servo and nose center servo...they turning the opposite way ..any fix for this;;

I believe you can adjust that in the table that shows the angle of the individual servos? :blush:

Hi,

I am very new to Arduino stuff.
I am doing this same project from same source as above.

I am having a strange issue.
My servos are 180°. The are alright, after reading the forum.
The only thing is my MG wing servos are not working at all. Like they are dead.
I have got CS analogue MGs instead of DS digitals.
Do you think this is the reason? Are they wrong servos?

Cheers !

Welcome to the forum

How are your servos powered ?

What happens if you remove all connections to the Arduino except a single servo and upload the Sweep example ?

Thank you for reply !

I have not done the sweep thing you have mentioned and dont know how to do that to be honest.
I am following the same wiring diagram as uploaded at the start of forum.

Cheers

Looking back in the topic I see that the project uses the PWM servo driver board

How is your project powered ?

Its exactly the same project.
It uses PWM board, you are right