I got the code from @zoomkat in the other forum post and I tried if it works for me, however I havent successfully made it. What do you think is missing or problem?
#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
boolean toggle = true;
void setup()
{
pinMode(13, OUTPUT); //LED on pin 13
pinMode(button, INPUT); //arduino monitor pin state
servo.attach(7); //pin for servo control signal
digitalWrite(5, HIGH); //enable pullups to make pin high
}
void loop()
{
press = digitalRead(button);
if (press == LOW)
{
if(toggle)
{
digitalWrite(13, HIGH); // set the LED on
servo.write(160);
toggle = !toggle;
}
else
{
digitalWrite(13, LOW); // set the LED off
servo.write(20);
toggle = !toggle;
}
}
delay(500); //delay for debounce
}
Your switch is connected to ground and a digital pin, which would be reasonable if you were using the INPUT_PULLUP mode. But, you aren't. So, either you need to change the pin mode or you need to add an external pullup or pulldown resistor (and make the code reflect which configuration you select).
With careful coding and attention to detail. We have no idea what the two switches are to do with respect to "two different modes in running the servo". It really sounds like you don't either.
Anyway, when the toggle is off it runs from 0-180-0-180 where it should run 20deg according to code.
It will far easier to help you when you understand that servos do NOT "run". They move to specific positions.
The code you posted, if the switch is wired properly, will tell the servo to go to position 160, and then to go to position 20, and then to go to position 160, over and over as long as the switch is held down. If that is not what is happening, you need to explain what is really happening.
yes it happens, when the toggle switch is pressed (on) it goes 160 to 20 to 160 to 20 over and over again but the problem is when I switch the toggle switch to opposite(off) it does not stop. I would like to achieve that when I press off the servo will stop moving from 160 to 20 to 160 to 20.
yhankru:
yes it happens, when the toggle switch is pressed (on) it goes 160 to 20 to 160 to 20 over and over again but the problem is when I switch the toggle switch to opposite(off) it does not stop. I would like to achieve that when I press off the servo will stop moving from 160 to 20 to 160 to 20.
Have you re-wired the switch or changed the pinMode() call? If not, why not? If so, which one and how?
void setup()
{
pinMode(13, OUTPUT); //LED on pin 13
pinMode(button, INPUT_PULLUP); //arduino monitor pin state
servo.attach(7); //pin for servo control signal
digitalWrite(5, HIGH); //enable pullups to make pin high
}
Yes, it is properly wired. It is working according to the code now. This time I would like to ask if how will I stop the servo from moving. I tried this code but it does not work, it doesnt stop when I switch the toggle switch to off.
if (press == LOW)
{
if(toggle)
{
digitalWrite(13, HIGH); // set the LED on
servo.write(160);
toggle = !toggle;
}
else
{
digitalWrite(13, LOW); // set the LED off
servo.write(20);
toggle = !toggle;
}
}
else /added part
{
servo.write(0);
}
If the output to the serial monitor app does not match the way you set the switch, then, despite all your assurances to the contrary, the switch is NOT wired the way your picture shows.
If the output does match the state of the switch as you set it, add code to move the servo in the "Switch is on" block. Put NOTHING more in the "Switch is off" block.
If the servo moves when the switch is on, and continues to move when the switch is off, your servo is defective.
The switch worked fine without the servo motor, but I guess my problem is I am using a 360deg MG995 servo motor that's why it doesnt stop. Any ideas how to stop the motor when the switch is off? I tried leaving the switch off as blank (with no code for servo) but it just continously rotate.
Any ideas how to stop the motor when the switch is off?
There is a range of values that makes the not-really-a-servo run at varying speeds in one direction. There is a range of values that makes the not-really-a-servo run at varying speeds in the other direction. Between the low end of one range and the low end of the other range, the not-really-a-servo's speed will be zero. By most definitions of speed, that means that it is stopped.
It stopped already (that is my problem earlier, but now solved) now I need to make it slower than how it operates now. If the delay isnt the answer, what is it?
Do you understand that you can not control position with the not-really-a-servos that you have? Do you understand that all that you can control is the speed?
If you understand that all you can control is the speed, do you not understand that the not-really-a-servo will continue to run at that speed forever, unless you tell it to run at a different speed? There is no reason to use delay(), or any other technique, to make a non-really-a-servo stay still for extended periods of time. Tell it ONCE to stop, and it will stay stopped until you tell it to move at a different speed.