Toggle switch to move servo motor

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
}

"I haven't successfully made it", what do you mean by that?

however I havent successfully made it.

The code does something. What does it do?

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).

When the toggle switch is on, it run the servo up to 160 deg, but when the toggle is off, it endlessly run. from 0-180-0-180....

Can you help me with the pullup thing?

Also, I want to have 2 toggle switches which does two different modes in running the servo. How can I achieve that?

The pullup thing

How can I achieve that?

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.

I'm sorry for replying very general.

Anyway, when the toggle is off it runs from 0-180-0-180 where it should run 20deg according to code.

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
}

I did this pullup in the pinmode.

  digitalWrite(5, HIGH); //enable pullups to make pin high

What is connected to this pin? If nothing, why do you want to turn the internal pullup resistor on?

Is your switch wired the way your picture showed? If it is, with the correct pin mode set, the switch should be acting the way you want.

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);
}

I added the else with the "added part comment"

I tried this code but it does not work, it doesnt stop when I switch the toggle switch to off.

Put the servo in a drawer for now. That will stop it from moving.

Make loop() look like this:

void loop()
{
   press = digitalRead(button);
   if(press == LOW)
   {
      Serial.println("Switch is on");
   }
   else
   {
      Serial.println("Switch is off");
   }
   delay(100);
}

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.

Yea, I saw one post about stopping the servo. It stopped at value 1500. Now the servo does not slow I try to put delay

void loop()
{
   press = digitalRead(button);
   if(press == LOW)
   {
      Serial.println("Switch is on");
      servo.write(1500);
      delay(200);
   }
   else
   {
      Serial.println("Switch is off");
    servo.write(0);
    delay(200);
   }

Now the servo does not slow

Do you mean that it does not stop ? All the delay() will do is to stop program operation for one fifth of a second.

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? :frowning:

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.