Hi all,
I have a simple idea in my mind and i want to make it.
The problem is that i do not know a lot about programming arduino
so I hope any one help me.
my project is to control 3 servo motors and control them using push button.
each button control one motor, when one of them is clinked the motor will rotate 90o
how I can do it ??
Start with this example code found in the IDE under examples and see what you can come up with to control the other servos
/*
Controlling a servo position using a potentiometer (variable resistor)
by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
modified on 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Knob
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
You want code ?.
//Created msureshkumar2610@gmail.com
#include <Servo.h> // include servo lib
Servo myservo1; // create servo objects to control a servos
Servo myservo2;
Servo myservo3;
const int button1 = 2; // input pin used to connect the buttons
const int button2 = 3;
const int button3 = 4;
int buttonState1 = 0; //buttonstatus
int buttonState2 = 0;
int buttonState3 = 0;
//configuration inside setup function
void setup() {
pinMode(button1,INPUT); //make this pins as input.
pinMode(button2,INPUT);
pinMode(button3,INPUT);
myservo1.attach(9); // attaches the servo on pin to the servo objects
myservo2.attach(10);
myservo3.attach(5);
myservo1.write(0);//initial position or home position of servos
myservo2.write(0);
myservo3.write(0);
}
void loop() {
buttonState1 = digitalRead(button1);
buttonState2 = digitalRead(button2);
buttonState3 = digitalRead(button3);
if(buttonState1 == LOW){
myservo1.write(90);
}
/*else if(buttonState1 == HIGH){
myservo1.write(0);
}*/
if(buttonState2 == LOW){
myservo2.write(90);
}
/*else if(buttonState2 == HIGH){
myservo2.write(0);
}*/
if(buttonState3 == LOW){
myservo3.write(90);
}
/*else if(buttonState3 == HIGH){
myservo3.write(0);
}*/
}
If you want servos automatically came back to position 0* when you release the button ?. then enable all else if statements in this code.
Make all the button pins tied with Vcc. (pull up)
Find the attachment the circuit I have attached.
For more subscribe my youtube channel
"Free Teaching Embedded Systems"