I have put together a program to control a project I'm working on, but decided to add 1 more servo. I need it to move 180 degrees when I push a button and stay there. When the same button is pushed again, the servo will return to its original position. As of right now, the servo will go to 180 and return to 0 but won't stay at 180. The rest of the program works fine. Servo 4 and button 3 are what are being used for this function and those parts are all in RED in the code.
#include <Servo.h>
Servo myservo1; // Tension servo
Servo myservo2; // Angle servo
Servo myservo3; // Fire pin servo
Servo myservo4; // Reload servo
int potpin1 = 0; // Analog pin used to connect the potentiometer for angle
int potpin2 = 1; // Analog pin used to connect the potentiometer for tension
int buttonPin1 = 17; // Fire!
int buttonPin2 = 16; // Reset
int buttonPin3 = 18; // Reload
int buttonState1 = 0; // Set buttonState for fire
int buttonState2 = 0; // Set buttonState for reset
int buttonState3 = 0; // Set buttonState for reload
int pos = 0; // Variable to store the servo position
int directionState = 0; // Variable for reading the direction of servo
int val; // variable to read the value from the analog pin
int ledPin = 13; // Ready light, if you have one change pin number
void setup()
{
myservo1.attach(9); // Attaches the tension servo on pin 9 to the servo object
myservo2.attach(6); // Angle servo on pin 6
myservo3.attach(5); // Fire servo on Pin 5
myservo4.attach(3); // Reload servo on Pin 3
pinMode(buttonPin1, INPUT); // Set Fire button to input
pinMode(buttonPin2, INPUT); // Set Reset button to input
pinMode(buttonPin3, INPUT); // Set Reload button to input
pinMode(ledPin, OUTPUT); // LED for ready light
}
void loop()
{
// Reset program. When reset button is pressed this is what happens
if (buttonState2 = digitalRead(buttonPin2))
{
digitalWrite(ledPin, LOW); // turns light off
delay (1000); // wait for servo
myservo3.write(120); // makes sure fire pin is out of the way
delay(500); // wait for servo to move
myservo1.write(0); // release tension
delay(500); // another wait
myservo2.write(20); // hit the arm down if needed
delay(500);
myservo3.write(0); // locks fire pin
val = analogRead(potpin2); // reads the value of the potentiometer for angle
val = map(val, 1023, 0, 0, 100); // scale it
myservo2.write(val); // sets the servo position according to the scaled value
delay(100); // wait
digitalWrite(ledPin, HIGH); //turn on ready light!
}
// End reset
else
// Main program!
//If fire button pressed:
if (buttonState1 = digitalRead(buttonPin1))
{
digitalWrite(ledPin, LOW); // ready light off
delay (500);
val = analogRead(potpin1); // Reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 1023, 0, 0, 220); // Scales it to use it with the servo (value between 0 and 180)
myservo1.write(val);
delay (1000);
myservo3.write(120);
//Reloading!!!!!
delay (1000); // Waits for fire servo
myservo3.write(120); // Makes sure fire pin is out of the way
delay(500); // Wait for fire servo to move
myservo1.write(0); // Release tension
delay(500); // Another wait
myservo2.write(20); // Hit the arm down if needed
delay(500);
myservo3.write(0); // Firing Pin locks arm
val = analogRead(potpin2); // Reads the value of the potentiometer for angle
val = map(val, 1023, 0, 0, 100); // Scale it
myservo2.write(val); // Sets the servo position according to the scaled value
delay(100); // Wait
digitalWrite(ledPin, HIGH); // Turn on ready light
}
// End of fire button
else
{
// This controls the angle live via pot
val = analogRead(potpin2); // Reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 1023, 0, 0, 100); // Scale it for max angle and stop it from hitting things!
myservo2.write(val); // Sets the servo position according to the scaled value
delay(100); // Waits for the servo to get there
}
// Reload Ammo
{ (buttonState3 = digitalRead(buttonPin3));
// If reload button is pressed:
if (directionState == 0) {
if (buttonState3 == HIGH) {
directionState = 1; // The direction for Reload servo is clockwise
// Goes from 0 to 180 degreres in steps of 1 degree
for(pos = 0; pos < 180; pos=pos+5)
{
myservo4.write(pos); // Tell Servo to go to position in variable in "pos"
delay(15); // Waits 15 ms for the servo to reach the position
}
}
}
else if (directionState == 1); {
// The button is pushed
if (buttonState3 == HIGH) {
directionState = 0; // The direction for the servo is anti-clockwise
// Goes from 180 degrees to 0 degrees in steps of 1 degree
for(pos = 180; pos>=1; pos=pos-5)
{
myservo4.write(pos); // Tell servo to go to position in variable "pos"
delay(15); // Waits 15ms for the servo to reach the position
}
}
}
}
}