I’m very new in this domain,but I’m eager to learn in time.
My first project has an Arduino Micro,two servos,one LED and one push button.
I want that when I push the button once the first servo motor to go in a position,after that the second servo motor to go to his position and when both of them reached their positions,the LED to turn on.When I press the button again,the whole process to reverse.
I want to do it myself,but for now it seems that I haven’t learned enough,and I need to do this project ASAP.
//zoomkat servo button test 7-30-2011
//Powering a servo from the arduino usually *DOES NOT WORK*.
#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;
void setup()
{
pinMode(button1, INPUT);
servo1.attach(7);
digitalWrite(4, HIGH); //enable pullups to make pin high
}
void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
servo1.write(160);
}
else {
servo1.write(20);
}
}
I managed to get to the point in wich I everything works just fine,except that after the delay time everything goes back.
I want to push to momentary button once to move the servos in one position and stay there until I press again.
#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int pos = 0;
const byte led1 = 6;
Servo servo1;
Servo servo2;
void setup()
{
pinMode(button1, INPUT);
pinMode(6, OUTPUT);
servo1.attach(7);
servo2.attach(5);
digitalWrite(4, HIGH); //enable pullups to make pin high
}
void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
servo1.write(30);
delay(500);
servo2.write(30);
digitalWrite(6, LOW);
delay(8000);
}
else {
servo2.write(150);
delay(500);
servo1.write(150);
delay(2000);
digitalWrite(6, HIGH);
}
}
As you have written your code the servo will stay in position for 8000msecs. After that it will do whatever the state of the button tells it.
If you want it to stay in place until the next button press you need to drop the ELSE part and change the code so that the angle of the servo changes when the button is pressed, but not when it is released.
Instead of servo.write(30) use servo.write(servoPos) and change the value of servoPos to 30 or 150 as required.