yes
if(digitalRead(buttonPin) == HIGH)
{
i want { servo to run 180 degree }
}
If motor 1 is 'in mid cycle', then i press switch 2 then motor 1 will stop
only one servo will run at a time so it will not will damage Arduino
Okay, be forewarned bad things can happen.
You do know 'servos always draw power'.
You should 'never' power a motor from your Arduino!
If motor 1 is 'in mid cycle', then i press switch 2 then motor 1 will stop
Stop where it currently is or go to 0 degrees first?
And
If motor 2 is 'in mid cycle', then you press switch 1 then motor 1 will stop?
What is this project for?
#include <Servo.h>
// create servo object to control a servo
// twelve servo objects can be created on most boards
Servo myservo1;
Servo myservo2;
const byte switch1 = 2; //switch press = LOW
const byte switch2 = 3; //switch press = LOW
const byte heartBeatLED = 13;
byte cycleServo1Flag = 0; //= 0 stopped, = 1 going to 180, = 2 going to 0
byte cycleServo2Flag = 0; //= 0 stopped, = 1 going to 180, = 2 going to 0
byte lastSwitch1; //current switch state
byte lastSwitch2; //current switch state
byte servoDelay = 15; //15ms
int servo1Pos;
int servo2Pos;
//timing variables
unsigned long MillisHeartBeat;
unsigned long testSwitchMillis;
unsigned long servoMillis;
//**********************************************************************************************
void setup()
{
myservo1.attach(8);
myservo1.write(servo1Pos);
myservo2.attach(9);
myservo2.write(servo2Pos);
pinMode(heartBeatLED, OUTPUT);
pinMode(switch1, INPUT_PULLUP); //push = LOW
lastSwitch1 = digitalRead(switch1);
pinMode(switch2, INPUT_PULLUP); //push = LOW
lastSwitch2 = digitalRead(switch2);
} //END of setup()
//**********************************************************************************************
void loop()
{
//************************
//code to see if the sketch is blocking, LED will toggle every 500ms
if (millis() - MillisHeartBeat >= 500)
{
MillisHeartBeat = millis();
//toggle LED state
digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));
}
//************************
//time to check the switches?
if (millis() - testSwitchMillis >= 50)
{
testSwitchMillis = millis();
checkSwitches();
}
//************************
//run at loop speed
checkServos();
//************************
//none blocking code here
//************************
} //END of loop()
// c h e c k S e r v o s ( )
//**********************************************************************************************
void checkServos()
{
//************************
//servo #1
//cycleServo1Flag = 0 stopped, = 1 going to 180, = 2 going to 0
//if enabled, has servoDelay ms gone by?
if (cycleServo1Flag == 1 && millis() - servoMillis >= servoDelay)
{
//reset timer
servoMillis = millis();
myservo1.write(servo1Pos);
servo1Pos++;
//only go to 180
if (servo1Pos > 180)
{
//at 180 now get ready to go lower
servo1Pos = 179;
//change to other direction
cycleServo1Flag = 2;
} //finished going to 180
} //if
//if enabled, has servoDelay ms gone by?
else if (cycleServo1Flag == 2 && millis() - servoMillis >= servoDelay)
{
//reset timer
servoMillis = millis();
myservo1.write(servo1Pos);
servo1Pos--;
if (servo1Pos < 0)
{
//stop servo movement
cycleServo1Flag = 0;
} //finished going to 0
} //else if
//************************
//servo #2
//cycleServo2Flag = 0 stopped, = 1 going to 180, = 2 going to 0
//add your second servo code here
} //END of checkServos()
// c h e c k S w i t c h e s ( )
//**********************************************************************************************
void checkSwitches()
{
//************************
//switch #1
byte currentState = digitalRead(switch1);
//has this switch position changed?
if (lastSwitch1 != currentState)
{
//update to the new state
lastSwitch1 = currentState;
//was the switch just pushed?
if (lastSwitch1 == LOW)
{
//allow servo 1 movment
cycleServo1Flag = 1;
//start at 0 degrees
servo1Pos = 0;
}
} //if
//************************
//switch #2
currentState = digitalRead(switch2);
//has this switch position changed?
if (lastSwitch2 != currentState)
{
//update to the new state
lastSwitch2 = currentState;
//add your second switch code here
} //if
} //END checkSwitches()
//**********************************************************************************************
EDIT
Updated code