Need help with iron man helet code

hi im having trouble with the code for my iron man helmet ive managed to get it working but the servo speed is to fast and the faceplate slams shut in totaly new to arduino and codeing its took me a long time to peice things together to get it to open and close with a press of a button and light up the led eyes and could use help with my code thanks for any help you can give

const int servo1Pin = 2;
const int buttonPin = 3;
const int LEDPin = 4;
const int servo2Pin = 5;
int buttonread;
#include <Servo.h>

Servo visorServo1; 
Servo visorServo2;

void setup() {
  Serial.begin(9600);
  visorServo1.write(105);  // Initial position CLOSED
  visorServo1.attach(servo1Pin);

  visorServo2.write(150); // initial position CLOSED
  visorServo2.attach(servo2Pin);

  pinMode(buttonPin, INPUT_PULLUP);  // Connect button between pin and GROUND. LOW when pushed.

  pinMode(LEDPin, OUTPUT);
  digitalWrite(LEDPin, LOW);  // LED off
}

void loop() {
Serial.println(buttonread);
delay(200);
  static unsigned long lastPushedTime = 0;
  static boolean visorClosed = true;
  static boolean lastButtonState = HIGH;
  
  boolean newButtonState = digitalRead(buttonPin);

  
  // If the button is down and it has been a while since it was last down...
 if (newButtonState  == LOW && lastButtonState == HIGH && millis() - lastPushedTime > 100) {
    lastPushedTime = millis();
    if (visorClosed) {
    visorServo1.write(0); // Close visor
      visorServo2.write(43);
      visorClosed = false;
      digitalWrite(LEDPin, LOW); // Turn oFF light
    }
    else {  // Viso is open
      visorServo1.write(105); // Close visor
      visorServo2.write(150); 
      visorClosed = true;
      digitalWrite(LEDPin, HIGH); // Turn oN light
  }
  }
  
  lastButtonState = newButtonState;
}

Your commas and periods seem to be broken, also.

You need to use a 'for' loop.

Use VarSpeedServo.h instead of Servo.h. The write has a speed parameter which can slow servo movement right down.

Steve

Hello, like Missdrew, I also think you need to use a for loop. Kindly follow Arduino IDE's servo 'sweep' example. After opening Arduino IDE, follow this path:
File>Example>servo>sweep.

Or as I said use VarSpeedServo.h which does it all for you.

Steve

@alecc88 , your topic has been moved to a more suitable location on the forum as this has nothing to do with Avrdude, stk500 or Bootloader.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.