Entry #4 (Previous Progress: Opening/Closing Mechanism (Software + Conclusion)

The entire coding process for the opening/closing mechanism is based upon the fact that I have an Arduino as my microcontroller. Since this is the case, I must use the Arduino IDE which i based on C++, a fairly common programming language. If I have to be honest, my coding skill is the weakest aspect of my electronics hobby and I intend to improve it as time goes by.
 
The goal of the program is to open the gauntlet when the pushbutton is pressed down while turning on the led lights at the same time. When the button is unpressed, the gauntlet closes and the lights turn off.
 
Setup
I must first setup the sketch by stating any libraries that I’ll be using. In this case, I’ll be using the basic Servo Library that is provided by arduino. I do this by declaring the inclusion of the library: #include
 
Then I have to declare all of my constants and my servos. I do this by seeing which inputs and outputs I’ll have to consider for this program as well as the servos I am coding. There is a button and an LED that I must consider so I declare their pins by stating:
    const int buttonPin = 3;
    const int ledPin = 6;
 
For a button, I must also declare the initial state of the button as the program starts and this value (also known as an integer) will change as the program is running:
    int buttonState = 0
 
The servos are then declared by saying:
    Servo servoTop;
    Servo servoBottom;
 
By declaring these terms, pins, and values, I’m able to then effectively write the main section of my code.
 
Void Setup
Void setup is the aspect of the code that runs only during the booting process of the Arduino. This is when other constants, values, and terms are declared. For the servos, I have to attach them to a value therefore I type:
    servoTop.attach(9);
    servoBottom.attach(10);
 
I also have to declare whether or not the button and led pins are inputs or outputs:
    pinMode(buttonPin, INPUT);
    pinMode(ledPin, OUTPUT);
 
Void Loop
During the Void Loop, the program will be running over and over again serving as the main code of the Arduino. It is in this aspect of the program that contains the main part of the code.
 
Since the code is all based on a pushbutton, its important to base the entire basis of the void loop on the button example. When reading a button and giving it an action, its important to create a buttonstate since the code needs a term to keep the button value on. This is also accomplished by using an if/then statement:
    buttonState = digitalRead{buttonPin};
    if (buttonState == High)    {
        (action);
        (action);
    }
    else {
        (action);
        (action);
    }
 
For these (actions), I need to control the servo movement as well as the LED output. I made the code fairly simple by creating a separate loop for both the opening motion and the closed motion.
    void in() {
        servoTop.write(0);
        servoBottom.write(180);
    }
 
    void out() {
        servoTop.write(180);
        servoBottom.write(0);
    }
 
Therefore, I simply made the (action) either void in (for the if statement) and void out (for the then statement). Then the second action must dictate the action for the LED therefore I simply state:
    digitalWrite(ledPin, HIGH);
    or
    digitalWrite(ledPin, LOW);
 
This concludes the code needed to run the opening/closing mechanism. When the button is pressed, the gauntlet opens allowing me to insert my hand and when the button is released, the gauntlet closes on my arm.