Hello Everyone i am in need of code for my project, I am very new to this stuff but want to create something and want a expert advice.
I have plan to create Pushbutton to on One servo motor which rotate to certain angle say (180) then smoothly turn back to its original position and stop. Until next Button is pushed.
Thank you in Advance any Suggestions, helps , friendly advice is appreciable.
Thank you for the quick reply but i tried for some three days continously struck with it and no solution. Can you help me with any code available or any website on which the code available.
Then you need to post your code and a diagram of how everything is connected. A photo of your wiring is also helpful. Tell us what does happen when you run the code and what should happen. If there are compile errors, post the entire text of the error message(s).
Read the forum guidelines to see how to properly post code and errors.
There are example about buttons and servos in the IDE, at File > Examples.
Kakashi459:
Until next Button is pushed.
Is "next button" a different button, or do you mean the same button, just pushed "next time" so to speak.
Also, servos have a natural speed (given in their data sheets, unladen) as the seconds it takes to move 60 degrees, like 0.1s/60 say. Do you want the servo to "flip" at that speed with a simple command to move to 180 then move back to 0, or does it need to "step" in some way? (I wondering at your use of the terms "rotate to" vs "smoothly turn back" as if they differ in your mind?)
//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);
}
}
That's odd because when I Googled "Arduino servo button" the first few entries all contained code that is almost exactly what you asked for. That took a couple of seconds not 3 days.
After that time you must have found (or written) some code that's close so post it here and tell us what problems you're having with it. Then we can help you to get it working.
Thank you for all the Replies for my queries and i searched for arduino servo button long time ago which obviously take couple of seconds but that doesnt full fill my idea that why i spent long time.
I just want the ----
1- Push button ( To Be Pushed One Time Released)
2- Then servo motor rotate (0 to 180 get delay and again 180 to 0) same cycle once
3- then stop for infinite time, Until the same push button again pressed
Thats what i want, Thank you all for helping me and i am sorry i didnt come up with any basic code it might helpfull if any code related to it already available which help me.
Servo myservo; // create servo object to control a servo #define servoPin 3 //~ #define pushButtonPin 2
int angle =90; // initial angle for servo (beteen 1 and 179)
int angleStep =10;
const int minAngle = 0;
const int maxAngle = 180;
const int type =2;//watch video for details. Link is at the top of this code (robojax)
int buttonPushed =0;
void setup() {
// Servo button demo by Robojax.com
Serial.begin(9600); // setup serial
myservo.attach(servoPin); // attaches the servo on pin 3 to the servo object
pinMode(pushButtonPin,INPUT_PULLUP);
Serial.println("Robojax Servo Button ");
myservo.write(angle);//initial position
}
void loop() {
if(digitalRead(pushButtonPin) == LOW){
buttonPushed = 1;
}
if( buttonPushed ){
// change the angle for next time through the loop:
angle = angle + angleStep;
// reverse the direction of the moving at the ends of the angle:
if (angle >= maxAngle) {
angleStep = -angleStep;
if(type ==1)
{
buttonPushed =0;
}
}
myservo.write(angle); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle); // print the angle
Serial.println(" degree");
delay(100); // waits for the servo to get there
}
i came up with this code but one thing wrong with this is when i pushed the button it start servo go from 0 to 180 degrees then 180 to 0 degrees and rotate on that particular direction continously i want the servo motor to be stopped at 0 degrees when complete the one cycle and wait till the button is pushed again
I'd tackle that with a switch..case state machine, all delay()-less so other stuff can happen at the same time:
state "servo idle", do nothing but monitor the button for a new press, ala the state change detect tutorial. When a new press is found, switch to...
state "servo going up", just increment the angle variable (don't write to servo, see below*) and check if it's at max. If it is, capture millis() as the start time for the pause**, reverse the angle sign, switch to...
state "servo paused", and monitor to see if the increasing millis() has gone far enough to switch to....
state "servo going down",just decrement the angle variable (don't write to servo, see below*) and check if it's at min. If it is, reverse angle sign and return to state "servo idle" for the next button press
*Outside of the switch..case, use blink without delay technique to see if a myservo.write() is due, using a variable that holds the time interval between write. Write or not, as approprite.
That way, you have a two-way control over the servo's speed:
degrees per write, and
seconds between writes.
** That is, the pause of the servo only, not the program. Whole thing should be delay()-less so you can perform other tasks if needed.
Please read the post at the start of any forum , entitled "How to use this Forum".
OR http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.