Toggle switch and servo

Hi guys so I'm pretty new to arduino and for my new project I need to know how to control the servo with control switch... So I'm trying to know how to do when the toggle switch value is low, the servo val should be 0.And when the toggle switch value is high , how do I make servo rotate 180 degrees?

Plz help me :frowning: lol...

P.S ignore the led variables, I wanted it to be later inputed

Help_guys.ino (274 Bytes)

OP's code

#include <Servo.h>

Servo serv;
int button = 3;
int led = 4;
int ledval;
int butval;


void setup(){
  serv.attach(5);
  pinMode(button, INPUT);
  pinMode(led, OUTPUT);
  
}

void loop(){
  ledval = digitalRead(led);
  butval = digitalRead(button);

You don't seem to have got very far.

The Button example from the IDE will give you everything you need. Just replace the digitalWrites that switch the LED on or off with serv.write(0) or serv.write(180).

Steve

Wym???

Mata007:
Wym???

Can you try again in English.

Steve

Your code can be:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

// constants won't change. They're used here to set pin numbers:
const int BUTTON_PIN = 7;       // the number of the pushbutton pin
const int SERVO_PIN = 9;       // the number of the pushbutton pin

void setup() {
  // initialize the pushbutton pin as an pull-up input
  // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed.
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  myservo.attach(SERVO_PIN);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  // read the state of the switch/button:
  int buttonState = digitalRead(BUTTON_PIN);

  if(buttonState == LOW)
    myservo.write(0);
  else
    myservo.write(180);
}

This is a combination of codes in two tutorials: Button and Servo motor

The code for the switch is the same as code for the button.

You can see line-by-line explanation in the above tutorials

thx really helpful

IoT_hobbyist:
Your code can be:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

// constants won't change. They're used here to set pin numbers:
const int BUTTON_PIN = 7;       // the number of the pushbutton pin
const int SERVO_PIN = 9;       // the number of the pushbutton pin

void setup() {
 // initialize the pushbutton pin as an pull-up input
 // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed.
 pinMode(BUTTON_PIN, INPUT_PULLUP);
 myservo.attach(SERVO_PIN);  // attaches the servo on pin 9 to the servo object
}

void loop() {
 // read the state of the switch/button:
 int buttonState = digitalRead(BUTTON_PIN);

if(buttonState == LOW)
   myservo.write(0);
 else
   myservo.write(180);
}




This is a combination of codes in two tutorials: [Button](https://arduinogetstarted.com/tutorials/arduino-button) and [Servo motor](https://arduinogetstarted.com/tutorials/arduino-servo-motor)

The code for the switch is the same as code for the button.

You can see line-by-line explanation in the above tutorials

Hi there, I have a question. According to this code, will the servo move from 0 to 180 in increment of 1 degree by default if we don't specify the "steps" like they do in this code below?

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
#define servoPin 3 //~
#define pushButtonPin 2 

int angle =90;    // initial angle  for servo
int angleStep =5;
const int minAngle = 50;
const int maxAngle = 110;

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

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 <= minAngle || angle >= maxAngle) {
      angleStep = -angleStep;
       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
   }

  
}

Thanks

No. In the code you quoted the servo moves directly from 0 to 180 at maximum speed.

If you want to move in 1 degree steps have a look at the Sweep example in the IDE.

Steve

slipstick:
No. In the code you quoted the servo moves directly from 0 to 180 at maximum speed.

If you want to move in 1 degree steps have a look at the Sweep example in the IDE.

Steve

Hi Steve,
I looked into the sweep example and tried to combine it with IoT_hobbyist's code but the servo keep moving by itself. Here's the code:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

// constants won't change. They're used here to set pin numbers:
const int BUTTON_PIN = 7;       // the number of the pushbutton pin
const int SERVO_PIN = 9;       // the number of the pushbutton pin
int pos = 0;    // variable to store the servo position

void setup() {
  // initialize the pushbutton pin as an pull-up input
  // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed.
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  myservo.attach(SERVO_PIN);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  int buttonState = digitalRead(BUTTON_PIN); // read the state of the switch/button
  if(buttonState == LOW){
  for (pos = 0; pos <= 180; pos += 1){
    myservo.write(pos);
     delay(15);     
  }
  }
   if(buttonState == HIGH){
  for (pos = 180; pos >= 0; pos -= 1){
    myservo.write(pos);
     delay(15);     
  }
  }
}

Can you advise how to make IoT_hobbyist's code work with slower speed?
Much appreciated,
S.

"Can you advise how to make IoT_hobbyist's code work with slower speed?"

Since you are sort of tagging on to another post, can you describe just what you want to do with your servo?

You just need to check where the servo is now before starting to move it. When you're just doing a simple write(angle) you can keeping writing it over and over without problems. Not so when you're doing a Sweep.

The last place you sent it to is in 'pos' so try something like:

   if (buttonState == LOW && pos <= 0) {     // it is at 0 move it to 180
    for (pos = 0; pos <= 180; pos += 1) {
      myservo.write(pos);
      delay(15);                                             // the delay controls the speed of the move
    }
  }
  if (buttonState == HIGH && pos >= 180) { // if it is at 180 move it to 0
    for (pos = 180; pos >= 0; pos -= 1) {
      myservo.write(pos);
      delay(15);                                            // the delay controls the speed of the move
    }
  }

Steve

Hi Steve,

Thank you for clearing that up, I got it to work. Just one final problem. Whenever I turn on the arduino board, the servo puts itself in the position of 90 degree, then when I push the button, it goes back to 0 at full speed and go to 180, from there it works like I want it to (button pushed, servo go to 180, let go of button, it return to 0). What could be added so the starting position of the servo will be at 0 when the board first turned on?

This is how my code look like:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

// constants won't change. They're used here to set pin numbers:
const int BUTTON_PIN = 7;       // the number of the pushbutton pin
const int SERVO_PIN = 9;       // the number of the pushbutton pin
int pos = 0;    // variable to store the servo position

void setup() {
  // initialize the pushbutton pin as an pull-up input
  // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed.
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  myservo.attach(SERVO_PIN);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  int buttonState = digitalRead(BUTTON_PIN); // read the state of the switch/button
  if(buttonState == LOW && pos <= 0){
  for (pos = 0; pos <= 180; pos += 1){
    myservo.write(pos);
     delay(15);     
  }
  }
   if(buttonState == HIGH && pos >= 180){
  for (pos = 180; pos >= 0; pos -= 1){
    myservo.write(pos);
     delay(15);     
  }
  }
}

Many thanks,
Sonny