Go Down

Topic: Toggling servo position (Read 692 times) previous topic - next topic


I wrote a sketch to toggle the servo position and my leds between 90 degrees and 180, and turn one led on and one off. But nothing is happening...Any help would be appreciated.

Code: [Select]
#include <Servo.h>

Servo myservo;
const int buttonPin = 2;
const int ledRED = 13; // the pin that the LED is attached to
const int ledGREEN =  12; //green led
int servopos = 180;

void setup() {
  myservo.attach(5); // attach servo to pin 5
  // initialize the LED pin as an output:
  pinMode(ledRED, OUTPUT); // led as ouput
  pinMode(ledGREEN, OUTPUT); //led as output
  pinMode(buttonPin, INPUT); // nitialize pushbuttonpin as input

void loop() {
  if(buttonPin == HIGH) {
    if(servopos == 180) {
       digitalWrite(ledGREEN, LOW);
       digitalWrite(ledRED, HIGH);
       servopos = 90;
    else {
      digitalWrite(ledRED, LOW);
      digitalWrite(ledGREEN, HIGH);
       servopos = 180;


buttonPin is never HIGH, its value is 2

You meant, perhaps
Code: [Select]
  if (digitalRead (buttonPin))
[ I won't respond to messages, use the forum please ]



You might try the below to see if it works.

Code: [Select]

//zoomkat servo-LED button toggle test 11-12-2012

#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)
      digitalWrite(13, HIGH);   // set the LED on
      toggle = !toggle;
      digitalWrite(13, LOW);    // set the LED off
      toggle = !toggle;
  delay(500);  //delay for debounce

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)


Mr. Zoomkat,
I need to mod this so that the servo toggles after a button press has been detected. In other words I don't want to hold the button down, but send a pulse to the arduino, move the servo 90 degrees, and then wait until another pulse is sent. Do I need to use the boolean command to do that? How do I toggle the state?
Thanks for any help!
Jim in Sweden

Go Up