Go Down

Topic: Servo with toggle switch (Read 1 time) previous topic - next topic


I would like to control five servos with five toggle switches. I would like to change servo from 0 to 180 when flip switch then flip  back switch change servo from 180 to 0.  I can't find information on that.  Is there wire diagram for it? I hope we can use normal switch..


perhaps you can learn about Button from the example in the IDE.

someone recently was doing the same thing:

Code: [Select]
//Button Toggle Servo

#include <Servo.h>
Servo servo;  // create servo object to control a servo
                // twelve servo objects can be created on most boards
const int buttonPin = 53;
boolean lastState = LOW;//storage for last button state
boolean pos = true;
void setup()
  servo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(buttonPin, INPUT_PULLUP);//this time we will set the pin as INPUT
  Serial.begin(9600);//initialize Serial connection
void loop()
  boolean currentState = digitalRead(buttonPin);
  if (currentState == LOW && lastState == HIGH)
    Serial.print(pos ? "up" : "down");
    servo.write(pos ? 180 : 0);
    pos = !pos;
  lastState = currentState;

Go Up