Go Down

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

swilliams1284

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
  myservo.write(servopos);
}

void loop() {
  if(buttonPin == HIGH) {
    if(servopos == 180) {
       digitalWrite(ledGREEN, LOW);
       digitalWrite(ledRED, HIGH);
       myservo.write(90);
       servopos = 90;
       delay(300);
    }
    else {
      digitalWrite(ledRED, LOW);
      digitalWrite(ledGREEN, HIGH);
       myservo.write(180);
       servopos = 180;
       delay(300);
    }
  }
}

MarkT

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 ]

swilliams1284


zoomkat

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

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

eatabean

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

holmes4

You need to get rid of the delays if you want the code to respond to the button quickly. Look at the blink without delay sketch.

You will also need to debounce the button, there is also an example sketch for that.

Mark

zoomkat


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


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

Go Up