1

I was trying to build a system that when the user pushes the button, the servo rotates 45 degrees and than returns back to zero. This system is going to be duplicated as there will be two servos and pushbuttons.

Here's my code:

#include <Servo.h>
Servo solservo; // create servo object to control a servo
Servo sagservo;
// twelve servo objects can be created on most boards
int possol = 0; // variable to store the servo position
int possag = 0;
int degsol=0;
int degsag=0;
int ledPin = 13; // choose the pin for the LED
int solPin = 7; // Pushbutton
int sagPin = 8; // variable for reading the pin status
int valsag = 0;
int valsol = 0;
void setup() {
 solservo.attach(9); // attaches the servo on pin 9 to the servo object
 sagservo.attach(10);
 pinMode(solPin, INPUT);
 pinMode(sagPin, INPUT);
 pinMode(ledPin, OUTPUT);
}
void loop() {
 digitalWrite(ledPin, HIGH); //Arka İsik
 valsag = digitalRead(sagPin); // read input value
 valsol = digitalRead(solPin);
//Sag Servo
 if (valsag == HIGH) { 
 sagservo.write(45); // tell servo to go to position in variable 'pos'
 delay(100); // waits 15ms for the servo to reach the position
 sagservo.write(-45);
 }
//Sol Servo 
 if (valsol == HIGH){
 solservo.write(45);
 delay(15);
 solservo.write(0);
 } 
} 

There'll probably be lots of small errors in this code, like unnecessary ints and such, but when I upload this program and push the button, it just rotates 45 deg, and doesn't come back. Is there a way that I can solve it?

Thanks in advance

asked Nov 24, 2016 at 18:32

1 Answer 1

0

Change sagservo.write(-45); to sagservo.write(0);.

answered Nov 24, 2016 at 18:33
2
  • When I stop pushing the button in the middle of the rotation (With the changes,) , it rotates back to 0, and does not complete the loop itself. Is it possible to keep it rotating and complete the loop even I stop pushing? Commented Nov 24, 2016 at 18:36
  • Change delay(100) to delay(1000) Commented Nov 24, 2016 at 18:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.