move servo with one push button
hi all
i don't know do
i move servo motor 1 push button
in beginning move x degree after each push button pressed to 160°after servo come same prossecus each pb pressed servo come x° x° reach initial position 0°
someone me, please ?
thx helping
#include <servo.h>
// constant variables used set servo angles, in degrees
const int straight = 0;
const int divergent = 160;
// constant variables holding ids of pins using
const int buttonpin = 2;
const int servopin = 9;
// servo movement step delay, in milliseconds
const int step_delay = 70;
// create servo object
servo myservo;
// global variables store servo position
int pos = straight; // current
int old_pos = pos; // previous
void setup()
{
// set mode digital pins in use
pinmode(buttonpin, input);
// setup servo
myservo.attach(servopin); // attach servo on pin 9
myservo.write(pos); // set initial servo position
}
void loop()
{
// start each iteration of loop reading button
// if button pressed (reads high), move servo
int button_state = digitalread(buttonpin);
if(button_state == high){
old_pos = pos; // save current position
// toggle position opposite value
pos = pos == straight ? divergent: straight;
// move servo new position
if(old_pos < pos){ // if new angle higher
// increment servo position oldpos pos
for(int = old_pos + 1; <= pos; i++){
myservo.write(i); // write next position servo
delay(step_delay); // wait
}
} else { // otherwise new angle equal or lower
// decrement servo position oldpos pos
for(int = old_pos - 1; >= pos; i--){
myservo.write(i); // write next position servo
delay(step_delay); // wait
}
}
}
}// end of loop
i don't know do
i move servo motor 1 push button
in beginning move x degree after each push button pressed to 160°after servo come same prossecus each pb pressed servo come x° x° reach initial position 0°
someone me, please ?
thx helping
#include <servo.h>
// constant variables used set servo angles, in degrees
const int straight = 0;
const int divergent = 160;
// constant variables holding ids of pins using
const int buttonpin = 2;
const int servopin = 9;
// servo movement step delay, in milliseconds
const int step_delay = 70;
// create servo object
servo myservo;
// global variables store servo position
int pos = straight; // current
int old_pos = pos; // previous
void setup()
{
// set mode digital pins in use
pinmode(buttonpin, input);
// setup servo
myservo.attach(servopin); // attach servo on pin 9
myservo.write(pos); // set initial servo position
}
void loop()
{
// start each iteration of loop reading button
// if button pressed (reads high), move servo
int button_state = digitalread(buttonpin);
if(button_state == high){
old_pos = pos; // save current position
// toggle position opposite value
pos = pos == straight ? divergent: straight;
// move servo new position
if(old_pos < pos){ // if new angle higher
// increment servo position oldpos pos
for(int = old_pos + 1; <= pos; i++){
myservo.write(i); // write next position servo
delay(step_delay); // wait
}
} else { // otherwise new angle equal or lower
// decrement servo position oldpos pos
for(int = old_pos - 1; >= pos; i--){
myservo.write(i); // write next position servo
delay(step_delay); // wait
}
}
}
}// end of loop
ok result
#include <servo.h>
// constant variables used set servo angles, in degrees
const int degre = 3.1; // on bouge de 5° en 5°
// constant variables holding ids of pins using
const int buttonpin = 2;
const int servopin = 9;
// servo movement step delay, in milliseconds
const int angle_min = 0;
const int angle_max = 20;
const int step_delay = 500;
// create servo object
servo myservo;
// global variables store servo position
int pos = angle_min; // current
bool vers_max = true;
void setup()
{
// set mode digital pins in use
pinmode(buttonpin, input_pullup);
// setup servo
myservo.attach(servopin); // attach servo on pin 9
myservo.write(pos); // set initial servo position
}
void loop()
{
// start each iteration of loop reading button
// if button pressed (reads high), move servo
int button_state = digitalread(buttonpin);
if(button_state == high){
if( vers_max ){
pos += degre;
}
else{
pos -= degre;
}
if( pos <= angle_min ){
pos = angle_min;
vers_max = true;
}
else if( pos >= angle_max ){
pos = angle_max;
vers_max = false;
}
myservo.write( pos );
delay(step_delay); // wait
/*old_pos = pos; // save current position
// toggle position opposite value
pos = pos == straight ? divergent: straight;
// move servo new position
if(old_pos < pos){ // if new angle higher
// increment servo position oldpos pos
for(int = old_pos + degre; <= pos; += degre){
// for(int = old_pos + 1; <= pos; i++){
myservo.write(i); // write next position servo
delay(step_delay); // wait
}
} else { // otherwise new angle equal or lower
// decrement servo position oldpos pos
for(int = old_pos - degre; >= pos; -= degre){
//for(int = old_pos - 1; >= pos; i--){
myservo.write(i); // write next position servo
delay(step_delay); // wait
}
}*/
}
}// end of loop
#include <servo.h>
// constant variables used set servo angles, in degrees
const int degre = 3.1; // on bouge de 5° en 5°
// constant variables holding ids of pins using
const int buttonpin = 2;
const int servopin = 9;
// servo movement step delay, in milliseconds
const int angle_min = 0;
const int angle_max = 20;
const int step_delay = 500;
// create servo object
servo myservo;
// global variables store servo position
int pos = angle_min; // current
bool vers_max = true;
void setup()
{
// set mode digital pins in use
pinmode(buttonpin, input_pullup);
// setup servo
myservo.attach(servopin); // attach servo on pin 9
myservo.write(pos); // set initial servo position
}
void loop()
{
// start each iteration of loop reading button
// if button pressed (reads high), move servo
int button_state = digitalread(buttonpin);
if(button_state == high){
if( vers_max ){
pos += degre;
}
else{
pos -= degre;
}
if( pos <= angle_min ){
pos = angle_min;
vers_max = true;
}
else if( pos >= angle_max ){
pos = angle_max;
vers_max = false;
}
myservo.write( pos );
delay(step_delay); // wait
/*old_pos = pos; // save current position
// toggle position opposite value
pos = pos == straight ? divergent: straight;
// move servo new position
if(old_pos < pos){ // if new angle higher
// increment servo position oldpos pos
for(int = old_pos + degre; <= pos; += degre){
// for(int = old_pos + 1; <= pos; i++){
myservo.write(i); // write next position servo
delay(step_delay); // wait
}
} else { // otherwise new angle equal or lower
// decrement servo position oldpos pos
for(int = old_pos - degre; >= pos; -= degre){
//for(int = old_pos - 1; >= pos; i--){
myservo.write(i); // write next position servo
delay(step_delay); // wait
}
}*/
}
}// end of loop
Arduino Forum > Using Arduino > Motors, Mechanics, and Power (Moderator: fabioc84) > move servo with one push button
arduino
Comments
Post a Comment